Python Interview Cheatsheet
1. Python Basics
• Variables: Dynamic typing, no need to declare type.
x = 5
name = "John"
• Data Types: int, float, str, bool, list, tuple, dict, set.
• Type Conversion:
int("5"), str(123), float("3.14"), list((1, 2))
2. Strings
• Concatenation: 'Hello' + 'World'
• Formatting:
f"Hello {name}"
"Hello {}".format(name)
• Common methods: .upper() , .lower() , .strip() , .split() , .replace(a, b)
• Slicing: s[1:4] , s[::-1]
3. Lists
• Create: [1, 2, 3]
• Methods: .append() , .extend() , .insert() , .remove() , .pop() , .sort() , .reverse()
• List comprehension:
squares = [x**2 for x in range(10)]
4. Tuples
• Immutable: (1, 2, 3)
• Tuple unpacking: a, b = (1, 2)
5. Dictionaries
• Create: {'a': 1, 'b': 2}
1
• Access: dict['a'] , .get('a')
• Methods: .keys() , .values() , .items()
6. Sets
• Create: {1, 2, 3}
• Operations: union ( | ), intersection ( & ), difference ( - )
7. Conditionals
if x > 0:
print("Positive")
elif x < 0:
print("Negative")
else:
print("Zero")
8. Loops
for i in range(5):
print(i)
while condition:
# do something
• Loop control: break , continue , pass
9. Functions
def add(a, b=0):
return a + b
• *args , **kwargs for variable arguments.
10. Lambda Functions
square = lambda x: x**2
2
11. Modules & Packages
import math
from math import sqrt
12. File Handling
with open('file.txt', 'r') as f:
data = f.read()
13. Exceptions
try:
result = 10 / 0
except ZeroDivisionError as e:
print(e)
finally:
print("Done")
14. OOP
class Person:
def __init__(self, name):
self.name = name
def greet(self):
print(f"Hello, {self.name}")
• Inheritance: class Student(Person):
• Special methods: __str__ , __repr__ , __len__ , __call__
15. Useful Built-ins
• map(func, iterable)
• filter(func, iterable)
• zip(a, b)
• enumerate(iterable)
• sorted(iterable, key=..., reverse=...)
3
16. Iterators & Generators
def gen():
yield 1
yield 2
17. Decorators
def decorator(func):
def wrapper(*args, **kwargs):
print("Before function")
return func(*args, **kwargs)
return wrapper
@decorator
def say_hello():
print("Hello")
18. Common Interview Tips
• Be ready to explain time complexity.
• Know mutable vs immutable types.
• Understand list vs tuple vs set vs dict.
• Practice coding without IDE autocompletion.
• Read and explain Python’s data model and GIL.