Back to Home

Python Interview Questions

Excel in Python interviews with these commonly asked questions. From basic syntax to advanced concepts like decorators and generators, we've got you covered. Practice interactively with SwipeInterview.

Easy

What is the difference between a list and a tuple in Python?

Answer: Lists are mutable (can be modified) and use square brackets []. Tuples are immutable (cannot be modified after creation) and use parentheses (). Lists are slower but more flexible, while tuples are faster and can be used as dictionary keys. Use lists when you need to modify data, tuples when data shouldn't change.

Easy

Explain Python's GIL (Global Interpreter Lock)

Answer: The GIL is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecode simultaneously. This means Python threads can't achieve true parallelism for CPU-bound tasks. For I/O-bound tasks, threads work well. For CPU-bound tasks, use multiprocessing instead.

Medium

What are Python decorators?

Answer: Decorators are functions that modify the behavior of other functions or classes. They use the @decorator syntax and are implemented using closures. Common use cases include logging, authentication, caching, and timing. Decorators wrap a function, adding functionality before and/or after the original function runs.

Medium

Explain list comprehensions vs generator expressions

Answer: List comprehensions [x*2 for x in range(10)] create a full list in memory immediately. Generator expressions (x*2 for x in range(10)) create an iterator that generates values on-demand. Use list comprehensions when you need all values at once. Use generators for large datasets or when you only need values once, saving memory.

Hard

What are metaclasses in Python?

Answer: Metaclasses are classes of classes that define how classes behave. Just as a class defines how instances behave, a metaclass defines how classes behave. The default metaclass is 'type'. Metaclasses are used for: API design, ORM implementations, automatic property registration, and enforcing coding standards. They're powerful but rarely needed.

Hard

Explain Python's memory management and garbage collection

Answer: Python uses reference counting as its primary memory management technique - each object tracks how many references point to it. When count reaches zero, memory is freed. Python also has a cyclic garbage collector to handle reference cycles (objects referencing each other). Memory is managed in pools for efficiency. The gc module provides control over garbage collection.

Master 320+ Python Questions

From basics to advanced concepts, practice Python interview questions with SwipeInterview.

Start Learning Python

More Interview Topics

JavaScript Questions React Questions AWS Questions