Python Interview Questions & Answers (100+)
Python Basics
Q: What are the key features of Python?
A: Python is interpreted, dynamically typed, object-oriented, easy to learn, and supports multiple
paradigms with a vast library.
Q: What are Python’s data types?
A: int, float, complex, str, list, tuple, set, dict, frozenset, bool, NoneType.
Q: What is the difference between mutable and immutable objects?
A: Mutable objects (list, dict, set) can change after creation. Immutable objects (tuple, str, int)
cannot.
Q: What is the difference between ‘is’ and ‘==’?
A: ‘is’ checks object identity, ‘==’ checks value equality.
Q: What is Python’s id() function?
A: Returns the unique identity (memory address) of an object.
Functions & Control Flow
Q: What are *args and **kwargs?
A: *args passes variable number of positional arguments, **kwargs passes variable keyword
arguments.
Q: What are Python’s lambda functions?
A: Anonymous functions defined using `lambda` keyword.
Q: What is recursion in Python?
A: A function calling itself to solve smaller instances of a problem.
Q: What is the difference between return and yield?
A: return exits a function with a value, yield produces a generator that yields values one by one.
OOP in Python
Q: What is a class in Python?
A: A blueprint for creating objects with attributes and methods.
Q: What is self in Python?
A: Represents the instance of the class used to access variables and methods.
Q: What is inheritance?
A: The process of deriving a new class from an existing class.
Q: What are @staticmethod and @classmethod?
A: @staticmethod does not use self or cls; @classmethod takes cls as the first argument.
Advanced Python
Q: What is a decorator in Python?
A: A function that modifies another function or method, used for logging, security, caching.
Q: What is a generator?
A: A function that uses yield to return an iterator one value at a time.
Q: What is an iterator?
A: An object that implements __iter__() and __next__() methods.
Q: What is the Global Interpreter Lock (GIL)?
A: A mutex in CPython that allows only one thread to execute bytecode at a time.
Q: What is duck typing in Python?
A: Type checking based on object behavior rather than class type (if it walks like a duck…).
File Handling
Q: How do you open a file in Python?
A: Using open(filename, mode). Common modes: r, w, a, b.
Q: What is with open() used for?
A: Context manager for handling files safely, automatically closes files.
Q: How do you handle CSV in Python?
A: Using csv.reader and csv.writer modules.
Q: How do you handle JSON in Python?
A: Using json.load(), json.dump(), json.loads(), json.dumps().
Libraries & Frameworks
Q: What is NumPy?
A: A library for numerical computing with support for arrays and matrix operations.
Q: What is Pandas?
A: A library for data analysis with Series and DataFrame structures.
Q: What is Flask?
A: A lightweight web framework for building APIs and web applications.
Q: What is Django?
A: A full-stack web framework with ORM, templates, authentication.
Q: What is FastAPI?
A: A modern web framework for building high-performance APIs with async support.
Concurrency
Q: What is multithreading?
A: Running multiple threads within a process for concurrent execution.
Q: What is multiprocessing?
A: Running multiple processes for true parallelism.
Q: What is asyncio?
A: Python library for asynchronous programming using coroutines and event loops.
Data Structures & Algorithms
Q: How do you implement a stack in Python?
A: Using a list with append() and pop().
Q: How do you remove duplicates from a list?
A: Convert list to set: list(set(mylist)).
Q: What is the complexity of sort()?
A: Timsort algorithm, O(n log n).
Testing & Debugging
Q: What is unittest in Python?
A: Built-in framework for unit testing.
Q: What is pytest?
A: A popular testing framework with simple syntax and fixtures.
Q: What is logging in Python?
A: Module for capturing logs, supports different levels like INFO, DEBUG, ERROR.
Real World & Best Practices
Q: What are Python best practices?
A: Follow PEP8, use virtual environments, modular code, testing, logging.
Q: What is garbage collection in Python?
A: Automatic memory management using reference counting and cyclic garbage collector.
Q: How do you optimize Python code?
A: Use built-in functions, avoid loops with comprehensions, use NumPy, caching, profiling.