Cheat sheets, interview questions, and recommended tools — all in one place!
Quick reference cards — check here whenever you need!
| Concept | Syntax | Example |
|---|---|---|
print() |
print("Hello") |
|
| Input | input() |
name = input("Name: ") |
| Variable | name = value |
age = 20 |
| f-String | f"text {var}" |
f"Age: {age}" |
| If/Else | if: elif: else: |
if x>0: print("+") |
| For Loop | for i in range(): |
for i in range(5): |
| While Loop | while condition: |
while x>0: x-=1 |
| Function | def name(): |
def add(a,b): return a+b |
| List | [item1, item2] |
nums = [1,2,3] |
| Dictionary | {key: value} |
d = {"a": 1} |
| Tuple | (item1, item2) |
t = (1,2,3) |
| Set | {item1, item2} |
s = {1,2,3} |
| Class | class Name: |
class Dog: pass |
| Try/Except | try: except: |
try: x=1/0 |
| Import | import module |
import math |
| Lambda | lambda x: expr |
sq = lambda x: x**2 |
| List Comp | [expr for x in list] |
[x**2 for x in range(5)] |
| File Read | with open() as f: |
with open("f.txt") as f: |
lst = [3, 1, 4, 1, 5]
lst.append(9) # Add at end → [3,1,4,1,5,9]
lst.insert(0, 2) # Insert at index → [2,3,1,4,1,5,9]
lst.remove(1) # Remove first 1 → [2,3,4,1,5,9]
lst.pop() # Remove last → [2,3,4,1,5]
lst.pop(0) # Remove at index → [3,4,1,5]
lst.sort() # Sort → [1,3,4,5]
lst.reverse() # Reverse → [5,4,3,1]
lst.index(3) # Find index → 2
lst.count(1) # Count → 1
len(lst) # Length → 4
lst.copy() # Shallow copy
lst.clear() # Empty list
d = {"a": 1, "b": 2, "c": 3}
d["d"] = 4 # Add/Update
d.get("x", 0) # Get with default → 0
d.keys() # All keys
d.values() # All values
d.items() # Key-value pairs
d.pop("a") # Remove key → 1
d.update({"e": 5}) # Merge dict
"b" in d # Check key → True
del d["c"] # Delete key
s = "Hello World"
s.upper() # "HELLO WORLD"
s.lower() # "hello world"
s.title() # "Hello World"
s.strip() # Remove spaces
s.split() # ["Hello", "World"]
s.replace("o", "0") # "Hell0 W0rld"
s.startswith("He") # True
s.endswith("ld") # True
s.find("World") # 6
s.count("l") # 3
s.isdigit() # False
s.isalpha() # False (space)
"-".join(["A","B"]) # "A-B"
s[::-1] # "dlroW olleH"
Important interview questions with answers!
Q1. Python mein List aur Tuple mein kya difference hai?
List: Mutable (can change), created with [].
Tuple: Immutable (cannot change), created with (). Tuple is
faster and can be a dictionary key.
Q2. Python mein == aur is mein kya fark hai?
== compares value, is compares identity
(memory location). a = [1,2]; b = [1,2] →
a == b is True, a is b is False.
Q3. *args aur **kwargs kya hain?
*args = Multiple positional arguments (received as tuple). **kwargs
= Multiple keyword arguments (received as dictionary).
Q4. Python mein Decorator kya hota hai?
A Decorator is a function that modifies another function without changing its code.
Used with @decorator_name syntax. Example: logging, timing, authentication.
Q5. Python mein GIL (Global Interpreter Lock) kya hai?
GIL is a mutex that allows only one thread to execute Python bytecode at a time in CPython. So for truly parallel CPU-bound tasks, multiprocessing is used.
Q6. List Comprehension kya hai?
Short way to create lists: [expression for item in iterable if condition]. Example:
[x**2 for x in range(10) if x%2==0] = squares of even numbers.
Q7. Python mein shallow copy aur deep copy mein kya fark hai?
Shallow copy: Creates new object but shares nested objects.
Deep copy: Creates completely independent copy.
import copy; copy.deepcopy(obj)
Q8. self kya hai Python classes mein?
self is a reference to the current object. It is the first parameter of every
method.
We use it to access object attributes and methods.
Q9. Python mein Generator kya hai?
A Generator is a function that uses yield keyword (instead of return). It performs
lazy
evaluation — values are generated one by one, not loaded in memory all at once.
Q10. Python mein __init__ method kya karta hai?
__init__ is a constructor — called automatically when an object is created.
Used to set initial attributes of the object.
Q11. Python mein mutable aur immutable types kya hain?
Mutable: list, dict, set (can change). Immutable: int, float, str, tuple, frozenset (cannot change).
Q12. map(), filter(), reduce() kya
hain?
map(fn, list) = apply function to every element. filter(fn, list) =
filter elements matching condition. reduce(fn, list) = combine all elements
into single value.
Q13. Python mein with statement kya hai?
with is a context manager — automatically cleans up resources (files, connections).
with open("file") as f: — file closes automatically.
Q14. __name__ == "__main__" ka matlab kya hai?
Checks if file is run directly or imported. If run directly,
__name__ = "__main__". If imported, it has module name.
Q15. Python mein exception handling kaise karte hain?
try = code that might error. except = handle error.
else = run if no error. finally = always run (cleanup).
raise = throw your own error.
Q16. pass, continue, break mein kya
fark hai?
pass = do nothing (placeholder). continue = skip current iteration,
go to next. break = exit the loop completely.
Q17. Python mein inheritance kya hai?
Child class inherits properties and methods of parent class.
class Dog(Animal): — Dog gets all methods of Animal. Best way for code reuse.
Q18. staticmethod aur classmethod mein kya fark
hai?
@staticmethod — independent of class/instance, no self/cls parameter.
@classmethod — takes cls parameter, can modify class state.
Q19. Python mein virtual environment kya hota hai?
Isolated Python environment — each project has its own packages.
Created using python -m venv myenv. Avoids dependency conflicts.
Q20. pip kya hai?
pip = Python package manager. Use pip install package_name to install
packages. Use pip freeze > requirements.txt to save dependencies.
Q21. Python 2 vs Python 3 main differences?
Print: print "hi" (Py2) vs print("hi") (Py3). Division:
5/2=2 (Py2) vs 5/2=2.5 (Py3). Python 2 officially dead since 2020,
always use Python 3.
Q22. zip() function kya karta hai?
Combines multiple iterables in parallel. zip([1,2,3], ['a','b','c']) →
[(1,'a'), (2,'b'), (3,'c')]
Q23. Python mein enumerate() kya hai?
Provides both index and value in a loop. for i, val in enumerate(["a","b","c"]): →
i=0,val="a" etc.
Q24. Abstract class kya hoti hai?
Cannot create object of abstract class. from abc import ABC, abstractmethod. Child
class must implement abstract methods. Acts like a blueprint/contract.
Q25. Python mein memory management kaise hoti hai?
Python uses automatic garbage collection (reference counting + cyclic garbage collector). When there are no references to an object, memory is freed.