Advanced Python Notes
Introduction to Python
Python is a high-level, interpreted programming language known for its simple syntax,
readability, and versatility. It supports multiple programming styles, including object-
oriented, functional, and procedural. These notes cover Python from beginner to advanced,
including syntax, examples, optimization techniques, and professional coding practices.
Key Features of Python
Easy Syntax and Readability
Interpreted and Dynamically Typed
Extensive Standard Libraries
Cross-Platform Compatibility
Large and Active Community
Part 1: Python Fundamentals
1. Variables and Data Types
Examples of different data types with syntax and usage:
String (str): message = "Hello, World"
Integer (int): count = 42
Float (float): price = 19.99
Boolean (bool): is_active = True
List (list): fruits = ["apple", "banana", "cherry"]
Tuple (tuple): coordinates = (10.5, 20.7)
Set (set): unique_colors = {"red", "green", "blue"}
Dictionary (dict): person = {"name": "Alice", "age": 25}
2. Operators in Python
Operators are used for mathematical, comparison, logical, and assignment operations.
Addition (+): Example: a + b
Subtraction (-): Example: a - b
Multiplication (*): Example: a * b
Division (/): Example: a / b
Floor Division (//): Example: a // b
Modulus (%): Example: a % b
Exponent (**): Example: a ** b
Part 2: Common Data Structures and Methods
1. String Methods
lower(): "HELLO".lower() → "hello"
upper(): "hello".upper() → "HELLO"
strip(): " hello ".strip() → "hello"
replace(): "apple".replace("a", "A") → "Apple"
split(): "a,b,c".split(",") → ['a', 'b', 'c']
join(): " ".join(["I", "love", "Python"]) → "I love Python"
2. List Methods
append(x): [1, 2].append(3) → [1, 2, 3]
extend(iterable): [1, 2].extend([3, 4]) → [1, 2, 3, 4]
insert(i, x): [1, 3].insert(1, 2) → [1, 2, 3]
remove(x): [1, 2, 3].remove(2) → [1, 3]
pop(i): [1, 2, 3].pop(1) → [1, 3]
sort(): [3, 1, 2].sort() → [1, 2, 3]
reverse(): [1, 2, 3].reverse() → [3, 2, 1]
Part 3: Essential Built-in Functions
len(obj): len('hello') → 5
type(obj): type(10) → <class 'int'>
range(n): list(range(5)) → [0,1,2,3,4]
enumerate(iterable): for i,v in enumerate(['a','b']): print(i,v)
zip(a,b): list(zip([1,2],['a','b'])) → [(1,'a'), (2,'b')]
map(func,iter): list(map(str.upper,['a','b'])) → ['A','B']
filter(func,iter): list(filter(lambda x:x>0,[-1,2,3])) → [2,3]
sorted(iter): sorted([3,1,2]) → [1,2,3]
Part 4: Problem Solving & Optimization Techniques
Example Problem: Find the squares of numbers 0–4
Solution 1 (Traditional Loop):
squares = []
for x in range(5):
squares.append(x**2)
Solution 2 (List Comprehension):
squares = [x**2 for x in range(5)]
Solution 3 (Map + Lambda):
squares = list(map(lambda x: x**2, range(5)))
✅ Professional Python developers usually prefer list comprehensions because they are
concise, readable, and efficient.
Part 5: Professional Python Practices
Prefer list comprehensions over manual loops for clarity and speed.
Use dictionary and set comprehensions for clean and fast code.
Avoid unnecessary deep nesting; use early returns.
Use built-in functions (any, all, sum, max, min) instead of writing manual loops.
Leverage Python standard libraries like collections, itertools, functools.
Write Pythonic code following PEP8 style guidelines.