Python interview questions commonly asked in top MNCs along with their answers:
- What is the difference between
list
andtuple
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( )
.
- Answer: Lists are mutable, meaning their elements can be changed after creation, and they are defined using square brackets
- 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 morefor
orif
clauses. For example:python
squares = [x**2 for x in range(10)]
- Answer: List comprehension is a concise way to create lists in Python. It consists of square brackets containing an expression followed by a
- 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.
- Answer:
- What is the difference between
==
andis
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.
- Answer:
- Explain the difference between
yield
andreturn
in Python.- Answer:
return
statement is used to return a value from a function. Once thereturn
statement is executed, the function terminates.yield
statement is used to return a generator object. When a function containsyield
, it becomes a generator function. The state of the function is saved, and the function can be resumed from where it left off.
- Answer: