Python - Practical Handbook
Python is a strong fit for automation, CLI tools, backends, testing and data work. In practice, virtual environments, explicit dependencies and project documentation matter more than memorizing a particular interpreter version.
Related topics: APIs and System Integrations, JSON, YAML, TOML and XML, SQL and PostgreSQL for Developers, Software Testing and Shell Scripting.
1. What Python is
Python is a high-level interpreted language focused on readability and a large ecosystem.
2. How Python works
Source is compiled to bytecode and executed by an interpreter such as CPython.
3. Python 2 vs Python 3
Use Python 3. Python 2 is obsolete.
4. Installation
Debian / Ubuntu
sudo apt install python3 python3-venv python3-pip
FreeBSD
pkg install python
Windows
Install Python 3 from python.org or approved package manager; enable launcher/PATH as needed.
5. Interactive interpreter
python3
6. Running a file
python3 script.py
7. Python as executable script
#!/usr/bin/env python3
print('hello')
8. Indentation is syntax
Blocks are defined by indentation. Use consistent spaces.
9. Comments
# comment
Comments should explain intent, not restate obvious code.
10. Variables
name = 'Ada'
count = 3
11. Variable names
Use snake_case for variables/functions and clear descriptive names.
12. Basic data types
int
value = 42
float
value = 3.14
str
value = 'hello'
bool
value = True
None
value = None
13. Checking type
type(value)
isinstance(value, str)
14. Type conversions
int('42')
str(42)
float('3.14')
bool(value)
15. Arithmetic operators
a + b
a - b
a * b
a / b
a // b
a % b
a ** b
16. Comparisons
a == b
a != b
a < b
a >= b
17. Logical operators
a and b
a or b
not a
18. Strings
text = 'hello'
text.upper()
text.strip()
19. f-string
name = 'Ada'
print(f'Hello {name}')
20. Accessing characters
text[0]
text[-1]
text[1:4]
21. Lists
items = [1, 2, 3]
items.append(4)
22. Tuple
point = (10, 20)
23. Dictionary - dict
user = {'name': 'Ada', 'age': 30}
user['name']
24. Set
tags = {'go', 'python'}
tags.add('js')
25. if / elif / else
if x > 0:
print('positive')
elif x == 0:
print('zero')
else:
print('negative')
26. Values treated as False
Examples: False, None, 0, 0.0, empty string/list/dict/set.
27. for loop
for item in items:
print(item)
28. enumerate
for i, item in enumerate(items):
print(i, item)
29. zip
for name, age in zip(names, ages):
print(name, age)
30. while loop
while count > 0:
count -= 1
31. break and continue
break exits a loop; continue skips to the next iteration.
32. Functions
def add(a, b):
return a + b
33. return
Returns a value and exits the function.
34. Default argument
def greet(name='world'):
print(name)
35. Named arguments
greet(name='Ada')
36. *args
def f(*args):
print(args)
37. **kwargs
def f(**kwargs):
print(kwargs)
38. Optional typing - type hints
def add(a: int, b: int) -> int:
return a + b
39. List comprehension
squares = [x*x for x in range(10)]
40. Modules
import json
from pathlib import Path
41. Standard library
Python ships with batteries included: pathlib, json, argparse, logging, sqlite3, subprocess, asyncio and more.
42. __name__ and main
def main():
...
if __name__ == '__main__':
main()
43. Packages
A package groups modules, typically in directories and optionally with __init__.py.
44. pip
python3 -m pip install requests
45. Why not install everything globally
Global installs create dependency conflicts and make projects harder to reproduce.
46. venv - virtual environment
python3 -m venv .venv
source .venv/bin/activate
47. .venv in Git
Do not commit the virtual environment. Add .venv/ to .gitignore.
48. requirements.txt
python3 -m pip freeze > requirements.txt
python3 -m pip install -r requirements.txt
49. pyproject.toml
Modern standard place for project metadata/build-system/tool configuration.
50. Installing a project
python3 -m pip install .
python3 -m pip install -e .
51. Reading files
text = Path('file.txt').read_text()
52. Classic open()
f = open('file.txt', 'r', encoding='utf-8')
53. with
with open('file.txt', encoding='utf-8') as f:
text = f.read()
54. JSON
import json
data = json.loads(text)
text = json.dumps(data)
55. Exceptions
Exceptions represent error conditions that can propagate until handled.
56. try / except / else / finally
try:
value = int(text)
except ValueError:
...
else:
...
finally:
...
57. raise
raise ValueError('bad value')
58. Classes
class User:
def __init__(self, name):
self.name = name
59. self
Reference to the current instance in instance methods.
60. Inheritance
class Admin(User):
pass
61. dataclass
from dataclasses import dataclass
@dataclass
class User:
name: str
age: int
62. Lambda
key = lambda x: x['name']
63. import os
import os
os.getenv('HOME')
64. pathlib
from pathlib import Path
Path('data').mkdir(exist_ok=True)
65. sys
import sys
print(sys.version)
print(sys.argv)
66. argparse
Standard library CLI argument parser.
67. subprocess
import subprocess
subprocess.run(['git', 'status'], check=True)
68. requests
import requests
r = requests.get('https://example.com', timeout=10)
69. Web backend
Flask
Minimal traditional web framework.
FastAPI
Modern API framework with typing and OpenAPI integration.
Django
Full-stack framework with ORM, admin, auth and strong conventions.
70. SQLite
import sqlite3
con = sqlite3.connect('app.db')
71. Logging
import logging
logging.basicConfig(level=logging.INFO)
logging.info('started')
72. Tests
python3 -m unittest
pytest
73. Debugging
Use prints/logging, debugger, IDE integration and focused tests.
74. Traceback
Read from the bottom for the final exception, then trace upward through the call stack.
75. Code formatting
Use Black or Ruff formatter, or project-standard tooling.
76. Ruff
ruff check .
ruff format .
77. mypy
mypy .
78. Typical simple project structure
project/
├── pyproject.toml
├── src/app/
├── tests/
└── README.md
79. How to run someone else's project
Read README, identify pyproject/requirements, create venv, install dependencies, run tests, then run app.
80. Typical workflow with requirements.txt
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt
81. Typical workflow with pyproject.toml
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -e .
82. Running a module with -m
python3 -m package.module
83. python -m pip
Ensures pip belongs to the interpreter you are invoking.
84. Checking interpreter
which python3
python3 -c 'import sys; print(sys.executable)'
85. Python versions
python3 --version
86. __pycache__
Contains cached bytecode. Do not commit it.
87. Environment variables
import os
token = os.getenv('API_TOKEN')
88. .env
Convenient local config format, usually loaded by a library. Never commit real secrets.
89. Decorators
Functions/classes that wrap or modify other callables/classes using @decorator syntax.
90. Generator and yield
def numbers():
yield 1
yield 2
91. Iterator
Object implementing iteration protocol; iter() returns an iterator and next() advances it.
92. async / await
Syntax for cooperative asynchronous I/O.
93. await
Suspends the current coroutine until an awaitable completes.
94. threading
Useful mainly for I/O-bound concurrency in CPython because of the GIL.
95. multiprocessing
Runs multiple processes and can use multiple CPU cores for CPU-bound work.
96. Popular libraries
HTTP
requests, httpx.
Backend
Flask, FastAPI, Django.
Data
pandas, polars, numpy.
Charts
matplotlib, plotly.
AI / ML
PyTorch, transformers, scikit-learn.
CLI
Typer, Click.
Tests
pytest.
Browser automation
Playwright, Selenium.
97. Where Python is a weaker choice
Very low-latency systems, tiny static binaries, hard real-time, some memory-constrained environments.
98. Python vs JavaScript
Python dominates scripting/data/backend; JavaScript is native to browsers and strong across web stacks.
99. Python vs Go
Python is faster to write dynamically; Go gives simpler deployment, stronger static typing and predictable concurrency.
100. Common beginner errors
Bad indentation
IndentationError.
Typo in name
NameError.
Wrong type
TypeError.
Invalid value
ValueError.
Missing key
KeyError.
Missing list element
IndexError.
Missing file
FileNotFoundError.
Missing module
ModuleNotFoundError.
101. Check where a module is loaded from
import requests
print(requests.__file__)
102. help()
help(str.split)
103. dir()
dir(object)
104. Function documentation
Use docstrings and help().
105. dir + type + help
A useful REPL trio for exploring unfamiliar objects.
106. Installing CLI tools - pipx
pipx install TOOL
107. Poetry, uv and other tools
Higher-level dependency/project tools manage environments, lockfiles and packaging.
108. uv
Fast modern Python project/package manager that can replace several pip/venv workflows.
109. Docker and Python
Use slim base images, virtualenv/build isolation as appropriate, pinned dependencies and non-root runtime users.
110. Example .gitignore
Python
__pycache__/
*.pyc
virtualenv
.venv/
secrets
.env
tests / tools
.pytest_cache/
.mypy_cache/
.ruff_cache/
IDE
.idea/
111. Minimal CLI program
import argparse
p = argparse.ArgumentParser()
p.add_argument('name')
args = p.parse_args()
print(args.name)
112. Simple file-processing script
from pathlib import Path
text = Path('input.txt').read_text()
Path('output.txt').write_text(text.upper())
113. Simple API request
import requests
r = requests.get('https://api.example.com/items', timeout=10)
data = r.json()
114. response.raise_for_status()
r.raise_for_status()
115. Reading Python code - order
Start with pyproject/requirements, entry point, package structure, key classes/functions and tests.
116. How to recognize the entry point
Look for if __name__ == '__main__', console scripts in pyproject, framework commands or executable modules.
117. How to find dependencies
Check pyproject.toml, requirements files and lockfiles.
118. How to inspect imports
standard library
Modules shipped with Python.
external libraries
Installed third-party packages.
project code
Imports from your own package/module tree.
119. How to read a traceback
Identify final exception and line, then inspect preceding stack frames to find the call path.
120. Useful commands
python3 --version
python3 -m venv .venv
python3 -m pip install -r requirements.txt
python3 -m pytest
ruff check .
121. Syntax cheat sheet
variable
x = 1
text
s = 'hello'
list
xs = [1,2]
dict
d = {'a': 1}
if
if x: ...
for
for x in xs: ...
while
while x: ...
function
def f(x): return x
class
class C: ...
exception
try: ... except Exception: ...
import
import json
file
Path('x').read_text()
122. Minimal workflow for a new project
mkdir app && cd app
python3 -m venv .venv
source .venv/bin/activate
123. Minimal workflow for a downloaded project
git clone REPO
cd PROJECT
python3 -m venv .venv
source .venv/bin/activate
python3 -m pip install -r requirements.txt
124. Most important things to remember
Indentation matters, use venv, prefer python -m pip, read tracebacks, keep dependencies explicit, use type hints/tests where useful.
125. Commands worth memorizing
python3 script.py
python3 -m venv .venv
python3 -m pip install ...
python3 -m pytest
python3 -m module
Official references
- Python documentation: https://docs.python.org/3/
- Python downloads and supported releases: https://www.python.org/downloads/
- venv: https://docs.python.org/3/library/venv.html
- Packaging guide: https://packaging.python.org/