What is Python?
Python is a high-level, interpreted language focused on readability and productivity. It supports multiple paradigms (procedural, OOP, functional).
# Hello World
print('Hello Python')Core data types
int, float, str, bool, list, tuple, dict, set.
user = {'id': 1, 'name': 'Ada'}
nums = [1,2,3]
tupled = (1, 'a')
unique = {1,2,2} # {1,2}List comprehensions
Concise way to build lists.
squares = [x*x for x in range(5) if x%2==0] # [0,4,16]
Generators
Lazily produce values using yield.
def gen():
for i in range(3):
yield i
for v in gen(): print(v)Decorators
Wrap functions to add behavior.
def log(fn):
def wrapper(*args, **kwargs):
print('calling', fn.__name__)
return fn(*args, **kwargs)
return wrapper
@log
def add(a, b): return a+bVirtual environments
Isolate dependencies using venv or conda.
python -m venv .venv # activate: Windows # .venv\Scripts\activate
Exceptions
Use try/except/finally to handle errors.
try:
1/0
except ZeroDivisionError as e:
print('error:', e)
finally:
print('cleanup')Asyncio basics
Cooperative concurrency with async/await and event loop.
import asyncio async def work(): await asyncio.sleep(1) return 'done' async def main(): res = await asyncio.gather(work(), work()) print(res) asyncio.run(main())
Dataclasses
Simplify class boilerplate with auto-generated __init__, __repr__, and comparisons.
from dataclasses import dataclass @dataclass class User: id: int name: str
Typing module
Add type hints for better tooling and optional static checks (mypy).
from typing import List, Optional def total(xs: List[int]) -> int: return sum(xs)
Context managers
Use with to manage resources safely; implement via __enter__/__exit__ or contextlib.
Multiprocessing vs threading
Threads share memory but are limited by the GIL for CPU-bound tasks; multiprocessing bypasses GIL at the cost of IPC overhead.
Packaging and dependency management
Use pip/poetry for dependencies; publish with build/twine; pin versions and lock to ensure reproducibility.