Python Interview Questions and Answers

Last updated: Wednesday, December 18, 2024

Python is a widely-used general-purpose, high-level programming language. It was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code.

Applications of Python:

  • System Scripting
  • Web Development
  • Game Development
  • Software Development
  • Complex Mathematics

2. What are the benefits of using Python language as a tool in the present scenario?

Benefits of Python:

  • Object-Oriented Language
  • High-Level Language
  • Dynamically Typed Language
  • Extensive Support for Machine Learning Libraries
  • Presence of Third-Party Modules
  • Open Source and Community Development
  • Portable and Interactive
  • Portable Across Operating Systems

3. Is Python a compiled language or an interpreted language?

Python is a partially compiled and partially interpreted language. When we execute Python code, it is first compiled into bytecode, which the Python Virtual Machine (PVM) then interprets based on the underlying platform (machine + operating system).


4. What does the ‘#’ symbol do in Python?

The # symbol is used to add comments in Python. Everything after # on that line is ignored by the interpreter.


5. What is the difference between a Mutable datatype and an Immutable data type?

  • Mutable Data Types: Can be modified at runtime. Example: List, Dictionary
  • Immutable Data Types: Cannot be modified at runtime. Example: String, Tuple

6. How are arguments passed in Python: by value or by reference?

Arguments in Python are passed by object reference (also called "pass by assignment"):

  • Mutable Objects (like lists or dictionaries): Can be modified within the function.
  • Immutable Objects (like integers or strings): Cannot be changed. Reassigning them inside the function doesn’t affect the original object.

7. What is the difference between a Set and a Dictionary?

  • Set: An unordered collection of unique, iterable, and mutable items.
  • Dictionary: An ordered collection of key-value pairs used for mapping and storing data.

8. What is List Comprehension? Give an Example.

List comprehension is a concise way to create lists based on existing iterables.

Example:

my_list = [i for i in range(1, 10)]

9. What is a lambda function?

A lambda function is an anonymous function that can have any number of parameters but only one expression.

Example:

a = lambda x, y: x * y
print(a(7, 19))

10. What is pass in Python?

pass is a placeholder in a compound statement where no action is required. It indicates "do nothing" and is often used as a temporary code block.

11. What is the purpose of the __init__ method in Python classes?

The __init__ method is a special method that is called when an object of a class is created. It is used to initialize the attributes of the object. The self parameter is a reference to the current instance of the class, and it is used to access variables that belong to the class.

Example:

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age