Python Programming Notes (Beginner to Advanced)
1. Basic Syntax and Structure
- Case-sensitive language.
- Code blocks are defined by indentation, not braces.
Example:
print("Hello, World!")
2. Variables and Data Types
- No need to declare variable types.
Example:
x=5 # int
name = "John" # str
is_happy = True # bool
price = 12.5 # float
3. Data Structures
Lists:
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits[1]) # banana
Tuples:
coordinates = (10, 20)
print(coordinates[0])
Sets:
unique_items = {1, 2, 3, 3}
unique_items.add(4)
Dictionaries:
person = {"name": "Alice", "age": 25}
print(person["name"])
person["age"] = 26
4. Operators
Arithmetic: + - * / % // **
Comparison: == != > < >= <=
Logical: and or not
Assignment: = += -= *= /=
Membership: in, not in
Identity: is, is not
5. Control Flow
if, elif, else:
age = 18
if age >= 18:
print("Adult")
elif age > 13:
print("Teen")
else:
print("Child")
for loops:
for i in range(5):
print(i)
while loops:
x=0
while x < 5:
print(x)
x += 1
break, continue, pass:
for i in range(10):
if i == 5:
break
if i == 3:
continue
print(i)
6. Functions
def greet(name):
return "Hello " + name
print(greet("Amantle"))
Lambda:
square = lambda x: x * x
print(square(5))
7. Object-Oriented Programming (OOP)
Class and Object:
class Car:
def __init__(self, brand):
self.brand = brand
def drive(self):
print(f"{self.brand} is driving")
my_car = Car("Toyota")
my_car.drive()
Inheritance:
class ElectricCar(Car):
def charge(self):
print("Charging...")
e_car = ElectricCar("Tesla")
e_car.drive()
e_car.charge()
8. Error Handling
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero!")
finally:
print("Done")
9. File Handling
# Write
with open("file.txt", "w") as f:
f.write("Hello File")
# Read
with open("file.txt", "r") as f:
content = f.read()
print(content)
10. Modules and Packages
import math
print(math.sqrt(16))
from datetime import datetime
print(datetime.now())
pip install requests
11. List Comprehension
squares = [x*x for x in range(5)]
12. Useful Built-in Functions
len(), type(), str(), int(), float(), sum(), min(), max(), input(), print()
13. Advanced Topics
Generators:
def count_up_to(n):
i=0
while i <= n:
yield i
i += 1
Decorators:
def decorator(func):
def wrapper():
print("Before function")
func()
print("After function")
return wrapper
@decorator
def greet():
print("Hello")
greet()
14. Common Libraries
Data Science: pandas, numpy, matplotlib, seaborn, scikit-learn
Web Dev: Flask, Django
Automation: selenium, pyautogui, os, shutil
APIs & Web: requests, httpx, beautifulsoup4, scrapy
Games: pygame
ML: tensorflow, keras, sklearn
15. Virtual Environments
python -m venv env
source env/bin/activate (macOS/Linux)
env\Scripts\activate (Windows)
16. Good Practices
- Use meaningful variable names.
- Follow PEP8.
- Comment your code.
- Write reusable functions.