For IT interviews (AWS Data Engineer, Data Engineer, Cloud Engineer, AI Engineer, Solutions Architect, MLOps Engineer, and Senior Software Engineer), Python is one of the most heavily tested skills.
Interviewers typically assess:
- Python Fundamentals
- Object-Oriented Programming (OOP)
- Data Structures & Algorithms
- File Handling
- Exception Handling
- Multithreading & Multiprocessing
- Generators & Iterators
- Decorators
- Lambda Functions
- Memory Management
- Python Internals
- Database Programming
- API Development
- Data Engineering Concepts
- Pandas & NumPy
- AWS + Python Integration
- Machine Learning & AI Python Questions
- System Design using Python
Python Interview Questions and Answers (100+)
Section 1: Python Fundamentals
1. What is Python?
Python is a high-level, interpreted, object-oriented programming language known for simplicity and readability.
Features:
- Easy syntax
- Dynamically typed
- Cross-platform
- Huge ecosystem
- Supports OOP and Functional Programming
Example:
print("Hello World")2. What are Python’s key features?
Answer:
- Interpreted
- Dynamically Typed
- Object-Oriented
- Open Source
- Extensive Libraries
- Portable
- Automatic Memory Management
3. Difference between Compiled and Interpreted Languages?
Compiled:
- Entire code converted before execution
- Example: C++
Interpreted:
- Executes line by line
- Example: Python
Python first compiles to bytecode then executes using PVM.
4. What is PEP 8?
PEP = Python Enhancement Proposal
PEP 8 provides coding standards.
Examples:
def calculate_salary():
passRecommendations:
- 4 spaces indentation
- Max line length 79
- Meaningful variable names
5. What are Python Data Types?
Built-in Data Types:
int
float
str
bool
list
tuple
set
dictExample:
age = 30
salary = 50000.5
name = "John"Section 2: List vs Tuple
6. Difference between List and Tuple?
| Feature | List | Tuple |
|---|---|---|
| Mutable | Yes | No |
| Speed | Slower | Faster |
| Syntax | [] | () |
Example:
mylist = [1,2,3]
mytuple = (1,2,3)7. Why are tuples faster?
Tuples are immutable.
Python optimizes memory allocation.
Suitable for:
- Configuration values
- Database records
Section 3: Dictionary Questions
8. What is Dictionary?
Key-value data structure.
employee = {
"name":"John",
"age":30
}9. Difference between Dictionary and List?
Dictionary:
- Key-value access
- O(1) lookup
List:
- Index-based access
10. How are dictionaries implemented?
Using Hash Tables.
Average complexity:
Search: O(1)
Insert: O(1)
Delete: O(1)Section 4: OOP
11. What is OOP?
Object-Oriented Programming organizes code into classes and objects.
Principles:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
12. What is a Class?
Blueprint of objects.
class Employee:
pass13. What is an Object?
Instance of class.
emp = Employee()14. What is Constructor?
Automatically called during object creation.
class Employee:
def __init__(self,name):
self.name = name15. What is Encapsulation?
Binding data and methods together.
class Account:
def __init__(self):
self.__balance = 100016. What is Inheritance?
Child class inherits parent properties.
class Animal:
pass
class Dog(Animal):
pass17. Types of Inheritance
- Single
- Multiple
- Multilevel
- Hierarchical
- Hybrid
18. What is Polymorphism?
Same interface different behavior.
class Dog:
def sound(self):
print("Bark")
class Cat:
def sound(self):
print("Meow")19. What is Abstraction?
Hiding implementation details.
from abc import ABC20. Difference between Abstraction and Encapsulation?
Abstraction:
- Hides complexity
Encapsulation:
- Hides data
Section 5: Exception Handling
21. What is Exception?
Runtime error.
Examples:
- ZeroDivisionError
- ValueError
- IndexError
22. Exception Handling Syntax
try:
x = 10/0
except ZeroDivisionError:
print("Error")
finally:
print("Always Executes")23. Difference between Exception and Error?
Exception:
- Recoverable
Error:
- Non-recoverable
24. What is Finally Block?
Executes whether exception occurs or not.
Section 6: Memory Management
25. What is Garbage Collection?
Automatic memory cleanup.
Python uses:
- Reference Counting
- Generational Garbage Collection
26. What is Reference Counting?
Tracks references.
x = []
y = xReference count becomes 2.
27. What is Deep Copy vs Shallow Copy?
Shallow:
copy.copy()Deep:
copy.deepcopy()Deep copy copies nested objects.
Section 7: Iterators and Generators
28. What is Iterator?
Object implementing:
__iter__()
__next__()29. What is Generator?
Produces values lazily.
def nums():
yield 1
yield 2Advantages:
- Memory efficient
30. Yield vs Return
Yield:
- Pauses execution
Return:
- Ends execution
Section 8: Decorators
31. What is Decorator?
Function modifying another function.
def logger(func):
def wrapper():
print("Running")
func()
return wrapper32. Real-world Decorator Uses
- Logging
- Authorization
- Retry Logic
- Metrics Collection
Section 9: Lambda Functions
33. What is Lambda?
Anonymous function.
square = lambda x: x*x34. map()
nums=[1,2,3]
list(map(lambda x:x*2,nums))Output:
[2,4,6]35. filter()
list(filter(lambda x:x>5,nums))36. reduce()
from functools import reduceCombines values into one result.
Section 10: Multithreading
37. What is Thread?
Lightweight execution unit.
38. What is GIL?
Global Interpreter Lock.
Only one thread executes Python bytecode at a time.
Important AWS interview question.
39. When to use Multithreading?
I/O-bound tasks:
- API calls
- Database queries
- File operations
40. When to use Multiprocessing?
CPU-bound tasks:
- ML calculations
- Data transformations
Section 11: Python Internals
41. How Python Executes Code?
Flow:
Source Code
↓
Bytecode
↓
Python Virtual Machine
↓
Execution42. What is name ?
if __name__ == "__main__":Entry point of execution.
43. What are *args and **kwargs?
def func(*args, **kwargs):
passargs:
- Positional
kwargs:
- Keyword arguments
44. What is Monkey Patching?
Changing behavior at runtime.
45. What are Context Managers?
with open("file.txt") as f:
passAutomatic resource cleanup.
Section 12: File Handling
46. How to Read File?
with open("sample.txt") as f:
data=f.read()47. Difference read(), readline(), readlines()
read()
- Entire file
readline()
- Single line
readlines()
- List of lines
48. How to Write File?
with open("a.txt","w") as f:
f.write("Hello")Section 13: Database Questions
49. Connect Python to MySQL
import mysql.connector50. Prevent SQL Injection
Use parameterized queries.
cursor.execute(
"SELECT * FROM users WHERE id=%s",
(user_id,)
)Section 14: Advanced Python
51. What is Metaclass?
Class that creates classes.
class MyMeta(type):
pass52. What is MRO?
Method Resolution Order.
Class.mro()Used in multiple inheritance.
53. What is Duck Typing?
“If it walks like a duck and quacks like a duck…”
Focus on behavior rather than type.
54. What are Magic Methods?
Examples:
__init__
__str__
__repr__
__len__55. Difference str vs repr
str
- User friendly
repr
- Developer friendly
Section 15: Pandas Questions
56. What is Pandas?
Data analysis library.
import pandas as pd57. DataFrame vs Series
Series:
- One-dimensional
DataFrame:
- Two-dimensional
58. Handling Null Values
df.fillna()
df.dropna()59. Merge vs Join vs Concat
Merge:
- SQL Join
Join:
- Index-based
Concat:
- Append data
60. GroupBy
df.groupby("department").sum()Section 16: NumPy
61. Why NumPy?
Fast numerical operations.
62. Advantages over List
- Faster
- Less memory
- Vectorized operations
63. Vectorization
arr*2No loops needed.
Section 17: AWS + Python
64. What is Boto3?
AWS SDK for Python.
import boto365. Upload file to S3
s3.upload_file(
"a.txt",
"bucket-name",
"a.txt"
)66. Read from DynamoDB
table.get_item()67. Invoke Lambda using Python
lambda_client.invoke()Section 18: Data Engineering Questions
68. Read Large Files Efficiently
for line in file:Avoid loading entire file.
69. Generator for ETL Pipelines
yield rowReduces memory usage.
70. Process Millions of Records
Approaches:
- Chunk Processing
- Multiprocessing
- Spark
- Dask
Section 19: Coding Questions Frequently Asked
71. Reverse String
s[::-1]72. Check Palindrome
s == s[::-1]73. Fibonacci
def fib(n):74. Find Duplicate Elements
Use Set.
75. Count Character Frequency
collections.CounterSenior-Level Python Questions
76. Why is Python Slow?
Because:
- Dynamic typing
- Interpreted execution
- GIL
77. Asyncio vs Threading
Asyncio:
- Single-threaded concurrency
Threading:
- Multiple threads
78. What is Async Await?
async def main():Non-blocking execution.
79. Design High Performance ETL Framework
Answer:
Use:
- S3
- Lambda
- Glue
- Spark
- Python Generators
- Thread Pools
80. Design Log Processing System
Components:
- Kinesis
- S3
- Glue
- Spark
- Redshift
FAANG / Amazon Senior Questions
81. Mutable Default Arguments Problem
Bad:
def func(items=[]):Good:
def func(items=None):82. Difference Between is and ==
isIdentity
==Value comparison
83. Why Python Uses Indentation?
Improves readability and enforces structure.
84. What Happens During Import?
- Module loaded
- Bytecode generated
- Namespace created
85. Explain Python Memory Model
Memory Areas:
- Stack
- Heap
- Object Pool
Must-Know Python Topics for AWS Interviews
- OOP
- Decorators
- Generators
- Context Managers
- Lambda
- Multithreading
- Multiprocessing
- Asyncio
- Pandas
- NumPy
- Boto3
- API Development
- ETL Design
- Error Handling
- Memory Optimization
- Python Internals
- Data Structures
- SQL Integration
- Logging
- Unit Testing
For AWS Data Engineer, Cloud Engineer, AI Engineer, and Solutions Architect interviews in the U.S. market, a target of 100–150 Python interview questions, plus hands-on coding practice around ETL pipelines, APIs, Pandas, multithreading, and Boto3 automation, is typically sufficient to clear most mid-level and senior rounds.
fundamentals to advanced system design, including common patterns and coding challenges.
Part 1: Core Python & Internals (The “Must-Know” Fundamentals)
These questions test your deep understanding of the language, not just syntax.
1. How is Python different from Java/C++ in terms of execution?
- Answer: Python is an interpreted, dynamically-typed language. Code is first compiled to bytecode (
.pycfiles), which is then executed by the Python Virtual Machine (PVM). This differs from Java (compiled to bytecode, runs on JVM, statically-typed) and C++ (compiled directly to machine code, statically-typed). No explicit compilation step is needed by the user.
2. What is the Global Interpreter Lock (GIL)? How do you work around it?
- Answer: The GIL is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecodes at once. This means CPU-bound multithreaded Python programs won’t see performance gains.
- Workarounds:
- Multiprocessing: Use the
multiprocessingmodule instead ofthreading. Each process gets its own Python interpreter and memory space, bypassing the GIL. - C Extensions: Write performance-critical sections in C, where you can release the GIL.
- Async Programming: Use
asynciofor I/O-bound concurrency, which doesn’t rely on multiple threads. - Alternative Interpreters: Use Jython or IronPython, which have no GIL.
- Multiprocessing: Use the
3. Explain mutable vs. immutable types with memory implications.
- Answer:
- Immutable:
int,float,str,tuple,frozenset. Once created, they cannot be changed. Operations create a new object. - Mutable:
list,dict,set, user-defined objects. Can be modified in-place.
- Immutable:
- Memory Implication:pythona = “hello” b = a a += ” world” # a now points to a NEW string “hello world” # b is still “hello”For mutable types:pythona = [1, 2, 3] b = a a.append(4) # a and b both point to [1, 2, 3, 4]This is a critical concept for function argument passing (pass-by-object-reference).
4. What are *args and **kwargs?
- Answer:
*args: Allows a function to accept any number of positional arguments. It’s a tuple inside the function.**kwargs: Allows a function to accept any number of keyword arguments. It’s a dictionary inside the function.- They are also used for unpacking:
func(*my_list)passes list elements as separate args,func(**my_dict)passes key-value pairs as named args.
5. Deep Copy vs. Shallow Copy
- Answer:
- Shallow Copy (
copy.copy()orlist[:]): Creates a new compound object, but inserts references into it to the objects found in the original. If the original contains nested objects, changes to those nested objects will be reflected in the copy. - Deep Copy (
copy.deepcopy()): Creates a new compound object and recursively inserts copies of the original objects. Fully independent.
- Shallow Copy (
6. What is a decorator? Write one that times a function’s execution.
- Answer: A decorator is a callable that takes another function and extends its behavior without explicitly modifying it. It’s syntactic sugar for
time_calc = timer_decorator(my_func).pythonimport time def timer_decorator(func): def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) end = time.time() print(f”{func.__name__} took {end-start:.4f} seconds”) return result return wrapper @timer_decorator def slow_function(): time.sleep(1) slow_function()
7. Generators vs. Iterators. What’s the yield keyword?
- Answer:
- Iterator: An object with a
__next__()method and__iter__()method returning itself. - Generator: A simple way to create an iterator using a function with
yield. It automatically implements the iterator protocol and is memory-efficient as it pauses execution, yielding one value at a time instead of building a whole list. yieldreturns a value and pauses the function’s state, resuming from the next line upon the next__next__()call.
- Iterator: An object with a
Part 2: Object-Oriented Programming (OOP)
8. What are @staticmethod, @classmethod, and instance methods?
- Answer:
- Instance Method: Takes
self(the instance). Has access to instance state. @classmethod: Takescls(the class). Can modify class state that applies across all instances. Often used as alternative constructors (e.g.,datetime.fromtimestamp()).@staticmethod: Takes neitherselfnorcls. It’s a regular function that lives in the class’s namespace for logical grouping. It cannot modify class or instance state.
- Instance Method: Takes
9. What is Method Resolution Order (MRO) in inheritance?
- Answer: It’s the order in which Python searches for a method in a hierarchy of classes. It uses the C3 Linearization algorithm. You can view it with
ClassName.mro()orClassName.__mro__. The rule is depth-first, left-to-right, but ensuring a child class precedes its parents and the order in which parents are listed is preserved.
10. What are Dunder (Magic) Methods? Explain __init__, __new__, __str__, __repr__, __call__.
- Answer: They are special methods with double underscores, allowing your objects to interact with language syntax.
__new__(cls): Class method that creates the object. Called before__init__. Used for singletons or immutable types.__init__(self): Instance method that initializes the object.__str__(self): For user-friendly string representation (print(obj)).__repr__(self): For unambiguous, developer-friendly string representation, ideally one you caneval().__call__(self): Allows an instance to be called as a function.
Part 3: Data Structures & Algorithms (LeetCode-Style)
These are the bread and butter of coding interviews.
11. How does a Python dictionary work? What are its time complexities?
- Answer: It’s a hash table under the hood. Keys are hashed (using
hash()), and the hash value determines the index in an internal array. Collisions are handled by open addressing. Average-case complexity for insertion, lookup, and deletion is O(1). Worst-case (many collisions) is O(n). Keys must be hashable (immutable and implement__hash__and__eq__).
12. List vs. Tuple: When to use which?
- Answer:
- List: Mutable, dynamic array. Use for homogeneous, mutable sequences.
- Tuple: Immutable, static array. Use for heterogeneous data (like a record), dictionary keys, or when immutability guarantees data integrity. Slightly more memory-efficient.
13. Common Coding Challenge: Two Sum
- Question: Given a list of integers
numsand an integertarget, return the indices of the two numbers that add up totarget. - Optimal Answer (O(n) time, O(n) space):pythondef two_sum(nums, target): seen = {} # value -> index for i, num in enumerate(nums): complement = target – num if complement in seen: return [seen[complement], i] seen[num] = i return []
14. Common Coding Challenge: Valid Parentheses
- Question: Given a string
scontaining'(',')','{','}','[',']', determine if the input is valid. - Optimal Answer:pythondef is_valid(s): stack = [] mapping = {“)”: “(“, “}”: “{“, “]”: “[“} for char in s: if char in mapping: top = stack.pop() if stack else ‘#’ if mapping[char] != top: return False else: stack.append(char) return not stack
15. Implement a Custom Context Manager (with statement)
- Two Ways:python# 1. Class-based class ManagedFile: def __init__(self, filename, mode): self.filename = filename self.mode = mode def __enter__(self): self.file = open(self.filename, self.mode) return self.file def __exit__(self, exc_type, exc_val, exc_tb): self.file.close() # Return True to suppress exceptions # 2. Generator-based (using contextlib) from contextlib import contextmanager @contextmanager def managed_file(filename, mode): f = open(filename, mode) try: yield f finally: f.close()
Part 4: Advanced Python & System Design
16. asyncio in Depth: Event Loop, Coroutines, Tasks
- Answer:
asynciois for concurrent I/O-bound tasks. An event loop manages the execution of coroutines (defined withasync def).awaitsuspends the coroutine, giving control back to the event loop to run something else until the result is ready. A Task wraps a coroutine and schedules its execution on the event loop.asyncio.gather()runs multiple tasks concurrently. This is single-threaded, cooperative multitasking.
17. Memory Management and Garbage Collection
- Answer: Memory management is handled by a private heap. A reference counter tracks how many references point to each object. When it hits 0, the memory is deallocated immediately. The Garbage Collector (GC) detects and cleans up cyclic references (e.g., two objects referencing each other), which reference counting can’t handle.
gcmodule allows manual control.
18. System Design: Design a URL Shortener (like Bitly) in Python
- Key Components:
- API Framework: FastAPI or Flask.
- Algorithm: Generate a short unique ID (e.g., using hash of URL + timestamp, or a base-62 encoded counter).
- Database: A key-value store like Redis (for speed) or PostgreSQL.
short_code -> original_url. - Caching: Redis cache in front of the DB for frequent reads.
- Rate Limiting: Use a library or middleware to limit API requests per user.
- Deployment: Use Gunicorn with Uvicorn workers behind an Nginx reverse proxy.
19. Testing: Unit Tests vs. Integration Tests. unittest vs. pytest.
- Answer:
- Unit Tests: Test a single component in isolation (e.g., a function). Use
unittest.mockto patch dependencies. - Integration Tests: Test how multiple components work together (e.g., API endpoint writing to a test database).
pytestis the de facto standard due to its simple syntax (plainassert), powerful fixtures, and rich plugin ecosystem, althoughunittestis in the standard library.
- Unit Tests: Test a single component in isolation (e.g., a function). Use
20. What is Metaclass? When would you use one?
- Answer: A metaclass is a class of a class; it defines how a class behaves. A class is an instance of a metaclass (default is
type). You use them for powerful, but often complex, framework-level patterns like enforcing coding standards, automatic attribute validation, or ORM base classes (like Django models). 99% of developers don’t need to write them.


