Loading ...

Python Interview Questions & Answers (2025 Edition)

1. What is Python?

Python is a high-level, interpreted, dynamically typed language known for readability and rapid development.

print("Hello, Python!")

2. Difference between list, tuple, and set

  • list: mutable, ordered

  • tuple: immutable, ordered

  • set: mutable, unordered, unique elements

lst = [1,2,3] tpl = (1,2,3) st = {1,2,3}

3. Difference between == and is

  • ==: checks value equality

  • is: checks object identity

a = [1,2] b = [1,2] print(a == b) # True print(a is b) # False

4. Mutable vs Immutable types

  • Mutable: list, dict, set (can change in place)

  • Immutable: int, float, str, tuple (cannot change)


5. What is a Python function?

Defined using def, functions encapsulate reusable logic.

def greet(name): return f"Hello, {name}!" print(greet("Alice"))

6. What are Python’s data types?

  • Numeric: int, float, complex

  • Sequence: list, tuple, range

  • Text: str

  • Set: set, frozenset

  • Mapping: dict

  • Boolean: bool


7. What is None in Python?

Represents the absence of a value.

x = None print(x) # None

8. Explain Python’s indentation

Python uses indentation to define code blocks instead of braces.

if True: print("Indented block")

9. What is the difference between append() and extend()?

lst = [1,2] lst.append([3,4]) print(lst) # [1,2,[3,4]] lst.extend([5,6]) print(lst) # [1,2,[3,4],5,6]

10. How to swap variables in Python?

a, b = 5, 10 a, b = b, a print(a, b) # 10, 5

Medium Questions (11–20)

11. Explain closures in Python

A closure remembers variables from its enclosing scope.

def outer(x): def inner(): print(x) return inner fn = outer(10) fn() # 10

12. What are decorators?

Functions that modify the behavior of other functions.

def decorator(func): def wrapper(): print("Before") func() print("After") return wrapper @decorator def greet(): print("Hello") greet()

13. Difference between *args and **kwargs

  • *args: variable number of positional arguments

  • **kwargs: variable number of keyword arguments

def demo(*args, **kwargs): print(args, kwargs) demo(1,2, a=3, b=4)

14. Python’s LEGB rule

Variable lookup order: Local → Enclosing → Global → Built-in

x = "global" def outer(): x = "enclosing" def inner(): x = "local" print(x) inner() outer() # local

15. What are Python modules and packages?

  • Module: a .py file containing code

  • Package: folder containing multiple modules and __init__.py

import math from mypackage import module1

16. Explain map(), filter(), reduce()

  • map(): transforms elements

  • filter(): selects elements

  • reduce(): aggregates elements

from functools import reduce nums = [1,2,3] print(list(map(lambda x: x*2, nums))) # [2,4,6] print(list(filter(lambda x: x>1, nums))) # [2,3] print(reduce(lambda a,b: a+b, nums)) # 6

17. Python list comprehension

Concise way to create lists.

squares = [x*x for x in range(5)]

18. Explain with statement

Automatically handles resource management, e.g., file closing.

with open("file.txt","r") as f: content = f.read()

19. Difference between shallow and deep copy

  • Shallow copy: copies references

  • Deep copy: copies objects fully

import copy lst = [[1,2]] shallow = copy.copy(lst) deep = copy.deepcopy(lst)

20. Python’s pass, break, continue

  • pass: does nothing

  • break: exits loop

  • continue: skips iteration


Difficult Questions (21–30)

21. Python’s GIL (Global Interpreter Lock)

Prevents multiple native threads from executing Python bytecodes simultaneously; affects CPU-bound threading.


22. Difference between @staticmethod and @classmethod

class Example: @staticmethod def static_method(): print("Static") @classmethod def class_method(cls): print(cls) Example.static_method() Example.class_method()

23. Generators in Python

Yield values lazily to save memory.

def gen(): for i in range(3): yield i for val in gen(): print(val)

24. Python’s __init__ vs __new__

  • __init__: initializes object after creation

  • __new__: creates a new instance


25. Python memory management

Uses reference counting and garbage collection. Manual collection with gc is also possible.

import gc gc.collect()

26. Explain Python’s MRO (Method Resolution Order)

Determines order of class method lookup in multiple inheritance.


27. Python’s @property decorator

Allows method access as attribute.

class Person: def __init__(self, name): self._name = name @property def name(self): return self._name p = Person("Alice") print(p.name) # Alice

28. Python’s exception handling

Use try, except, finally.

try: 1/0 except ZeroDivisionError: print("Cannot divide by zero") finally: print("Done")

29. What is monkey patching?

Dynamically modifying classes or modules at runtime.

class A: pass A.new_attr = 100 print(A.new_attr)

30. Python’s asyncio

Supports asynchronous programming using async and await.

import asyncio async def main(): print("Hello") await asyncio.sleep(1) print("World") asyncio.run(main())

✍️ By Not available | 2025-10-21T08:58:25.823Z

Call Our Course Advisors

IND: +91-98018 30173 / +91-70429 28331

US: +1-252 490 1033

UK: +44-121 3871751