1. Python Philosophy & Execution Model
1.1 Python Philosophy
Python emphasizes:
- Readability
- Simplicity
- Minimal boilerplate
- Developer productivity
Example comparison:
C#
public int Add(int a, int b) { return a + b; }
Python
def add(a, b): return a + b
No types. No braces. Indentation defines scope.
1.2 Interpreted vs Compiled
Python is:
- Interpreted
- Dynamically typed
- Compiled to bytecode internally
- Executed by CPython interpreter
Flow:
.py file ā bytecode (.pyc) ā Python Virtual Machine
1.3 Indentation Is Syntax
In Python:
if True: print("Hello")
This will break:
if True: print("Hello") # IndentationError
Indentation defines blocks (like
{} in C#).2. Basic Syntax & Data Types
2.1 Variables (Dynamic Typing)
x = 10 x = "hello"
Type changes dynamically.
Check type:
type(x)
2.2 Core Data Types
Integers
x = 10
Float
y = 3.14
Boolean
flag = True
String
name = "Vivek"
None
x = None
Equivalent to
null in C#.2.3 Type Casting
int("10") float("3.14") str(123)
3. Mutability & Memory Model
This is VERY important for interviews.
3.1 Immutable Types
- int
- float
- bool
- str
- tuple
Example:
x = "hello" x[0] = "H" # Error
3.2 Mutable Types
- list
- dict
- set
lst = [1,2,3] lst[0] = 10 # Valid
3.3 Assignment Behavior
a = [1,2,3] b = a b.append(4) print(a) # [1,2,3,4]
Because both reference same object.
3.4 Copying
Shallow copy:
import copy b = copy.copy(a)
Deep copy:
b = copy.deepcopy(a)
4. Core Data Structures
4.1 Lists (Dynamic Array)
nums = [1,2,3] nums.append(4) nums.pop() nums.insert(0, 10)
Time Complexity
Operation | Complexity |
Append | O(1) |
Pop (end) | O(1) |
Insert (middle) | O(n) |
Search | O(n) |
4.2 Tuples (Immutable List)
t = (1,2,3)
Use when:
- Data should not change
- Faster than list
- Used as dictionary keys
4.3 Sets (HashSet Equivalent)
s = {1,2,3} s.add(4) s.remove(2)
Properties:
- No duplicates
- Unordered
- O(1) average lookup
4.4 Dictionaries (HashMap Equivalent)
d = {"a": 1, "b": 2} d["c"] = 3
Access safely:
d.get("a")
Loop:
for key, value in d.items(): print(key, value)
Time Complexity:
Operation | Complexity |
Lookup | O(1) avg |
Insert | O(1) avg |
5. Control Flow
5.1 Conditionals
if x > 10: print("Big") elif x == 10: print("Equal") else: print("Small")
5.2 Loops
For loop
for i in range(5): print(i)
While loop
while x > 0: x -= 1
5.3 enumerate()
for index, value in enumerate(nums): print(index, value)
5.4 zip()
for a, b in zip(list1, list2): print(a, b)
6. Functions
6.1 Basic Function
def add(a, b): return a + b
6.2 Default Arguments
def greet(name="Guest"): print(f"Hello {name}")
6.3 Keyword Arguments
greet(name="Vivek")
6.4 *args
Variable positional arguments:
def sum_all(*args): return sum(args)
6.5 **kwargs
Variable keyword arguments:
def print_info(**kwargs): for key, value in kwargs.items(): print(key, value)
6.6 Lambda Functions
square = lambda x: x * x
Used in sorting & functional operations.
7. List & Dictionary Comprehensions
7.1 List Comprehension
squares = [x*x for x in range(5)]
With condition:
evens = [x for x in range(10) if x % 2 == 0]
7.2 Dictionary Comprehension
d = {x: x*x for x in range(5)}
8. Iterators & Generators (Intro Level)
8.1 Iterator
Any object implementing:
__iter__()
__next__()
8.2 Generator
Uses
yield.def count_up(n): i = 0 while i < n: yield i i += 1
Usage:
for num in count_up(5): print(num)
Benefits:
- Memory efficient
- Lazy evaluation
9. Sorting & Built-ins
9.1 sorted() vs sort()
nums = [3,1,2] sorted_nums = sorted(nums) # returns new list nums.sort() # modifies original
9.2 Custom Sorting
nums.sort(key=lambda x: -x)
Sort objects:
students.sort(key=lambda s: s.age)
š„ Interview Quick Recap
- Python is dynamically typed.
- Lists are dynamic arrays.
- Dict & set use hashing.
- Functions are first-class objects.
- Indentation defines blocks.
- Mutable vs immutable matters.
- Comprehensions are preferred over loops.
- Generators are memory-efficient.
