Top questions with answers asked in MNC on Python

Python interview questions commonly asked in top MNCs along with their answers:

  1. What is the difference between list and tuple in Python?
    • Answer: Lists are mutable, meaning their elements can be changed after creation, and they are defined using square brackets [ ]. Tuples are immutable, meaning their elements cannot be changed after creation, and they are defined using parentheses ( ).
  2. Explain the concept of list comprehension in Python.
    • Answer: List comprehension is a concise way to create lists in Python. It consists of square brackets containing an expression followed by a for clause, then zero or more for or if clauses. For example:

      python

       
      squares = [x**2 for x in range(10)]
  3. What is the purpose of __init__ in Python classes?
    • Answer: __init__ is a special method in Python classes used for initializing new objects. When a class is instantiated, the __init__ method is called automatically. It allows the class to initialize attributes and perform any necessary setup.
  4. What is the difference between == and is in Python?
    • Answer: == is used for value equality. It compares the values of two objects. is is used for identity equality. It checks if two variables refer to the same object in memory.
  5. Explain the difference between yield and return in Python.
    • Answer: return statement is used to return a value from a function. Once the return statement is executed, the function terminates. yield statement is used to return a generator object. When a function contains yield, it becomes a generator function. The state of the function is saved, and the function can be resumed from where it left off.