✅ 50 Python Interview Questions & Answers

 

✅ 50 Python Interview Questions & Answers 🐍💼

1. What is Python?

Python is a high-level, interpreted, dynamically typed language supporting OOP, procedural, and functional programming.


2. What are Python’s key features?

  • Easy syntax & readability

  • Interpreted & dynamically typed

  • Extensive standard library

  • Cross-platform

  • Large community support


3. Python’s built-in data types?

  • Numeric: int, float, complex

  • Sequence: str, list, tuple

  • Set types: set, frozenset

  • Mapping: dict

  • Boolean & None: bool, NoneType


4. Difference between list, tuple, and set?

TypeOrderedMutableExample
ListYesYes[1, 2, 3]
TupleYesNo(1, 2, 3)
SetNoYes{1, 2, 3}

5. What are *args and **kwargs?

  • *args → variable positional arguments

  • **kwargs → variable keyword arguments


6. Lambda function?

A one-line anonymous function:

square = lambda x: x**2

7. List comprehension?

Concise way to create lists:

[x**2 for x in range(5)]

8. Modules vs Packages?

  • Module: Single .py file

  • Package: Folder with multiple modules & __init__.py


9. Shallow copy vs Deep copy?

  • Shallow → copies references

  • Deep → copies nested objects


10. is vs ==?

  • == → checks values

  • is → checks object identity


11. What is Python’s GIL?

Global Interpreter Lock ensures only one thread executes Python bytecode at a time, limiting CPU-bound multithreading.


12. Python memory management?

  • Automatic memory management using garbage collection

  • Reference counting keeps track of object usage


13. What are Python decorators?

Functions that modify behavior of another function or class:

@decorator def func(): pass

14. Python iterators vs iterables?

  • Iterable: Object that can return an iterator (list, dict)

  • Iterator: Object with __next__() method


15. Generators?

  • Functions using yield to produce lazy sequences

  • Memory-efficient for large datasets


16. Python with statement?

Manages resources automatically (e.g., file handling):

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

17. Difference between deepcopy() and copy()?

  • copy() → shallow copy

  • deepcopy() → copies nested objects recursively


18. Python string methods?

Examples: upper(), lower(), split(), join(), replace(), strip()


19. Python list methods?

Examples: append(), extend(), insert(), remove(), pop(), sort(), reverse()


20. Python dictionary methods?

Examples: keys(), values(), items(), get(), update(), pop()


21. Difference between Python 2 and 3?

  • Python 3 uses Unicode by default

  • print() is a function in Python 3

  • Division / returns float in Python 3


22. Python set methods?

add(), remove(), union(), intersection(), difference()


23. Difference between mutable and immutable types?

  • Mutable: Can be changed (list, dict, set)

  • Immutable: Cannot be changed (tuple, str, int)


24. What is slicing in Python?

Extract part of a sequence:

s = "Hello" s[1:4] # 'ell' s[::-1] # Reverse

25. Python *args vs **kwargs?

  • *args → tuple of arguments

  • **kwargs → dict of keyword arguments


26. Python pass, break, continue?

  • pass → placeholder

  • break → exit loop

  • continue → skip current iteration


27. What is Python enumerate()?

Adds index to iterable elements:

for i, val in enumerate(['a','b']): print(i, val)

28. Python zip() function?

Combines iterables element-wise:

list(zip([1,2], ['a','b'])) # [(1,'a'),(2,'b')]

29. Python map(), filter(), reduce()?

  • map() → applies function

  • filter() → filters elements

  • reduce() → cumulative computation (from functools)


30. Python any() vs all()?

  • any([False, True]) → True

  • all([True, True]) → True


31. Difference between Python == and is?

  • == → value equality

  • is → object identity


32. What are Python exceptions?

Errors detected during runtime, handled using try-except


33. Custom exceptions?

class MyError(Exception): pass

34. Python super() usage?

Access parent class methods:

super().__init__()

35. Python class vs instance variables?

  • Class → shared across instances

  • Instance → unique to object


36. Python @staticmethod vs @classmethod?

  • @staticmethod → no self or cls

  • @classmethod → receives cls


37. Difference between Python isinstance() and type()?

  • isinstance(obj, cls) → True for subclasses too

  • type(obj) → exact class


38. Python __str__ vs __repr__?

  • __str__ → readable string

  • __repr__ → unambiguous, for dev


39. Python * operator uses?

  • Multiply numbers

  • Unpack iterables in function calls:

def add(a,b): return a+b nums = [1,2] add(*nums)

40. Python ** operator?

  • Power: 2**3 = 8

  • Unpack dictionaries in function calls


41. Python del statement?

Deletes variables, list elements, or dict keys:

del a[0]

42. Python id() function?

Returns memory address of an object


43. Python globals() vs locals()?

  • globals() → dictionary of global symbols

  • locals() → dictionary of local symbols


44. Difference between iterable and iterator?

  • Iterable → can produce an iterator

  • Iterator → has __next__()


45. Python __init__ method?

Constructor to initialize object attributes


46. Python __del__ method?

Destructor called when an object is garbage-collected


47. Python context manager?

Object supporting with statement using __enter__ and __exit__


48. Python dir() function?

Lists attributes and methods of object or module


49. Python help() function?

Shows documentation for modules, classes, functions


50. Difference between Python shallow and deep copies?

  • Shallow → copies references

  • Deep → copies nested objects recursively

Comments

Popular posts from this blog

🔁 Top 30 Python List Methods You Should Know

Python Project 7: Read & Write JSON Files

✅ Python – Quick Start Guide for Beginners