Python for Beginners.
// setup to first real project.
A practical guide you can keep open while learning. Focus on commands and habits you will actually use.
$ python --version
Python 3.12.x
$ python main.py
hello, world
$ _
// contents
Install Python
First script
Virtual environments
Packages with pip
Core syntax
Data structures
Functions
Files and JSON
Errors and debugging
Project layout
Practice plan
Cheat sheet
Install Python
Use Python 3.11+ if possible. Newer versions are faster and better supported.
fedora
sudo dnf install -y python3 python3-pip python3 --version
ubuntu / debian / mint
sudo apt update sudo apt install -y python3 python3-pip python3-venv python3 --version
arch
sudo pacman -S python python-pip python --version
// command name note
Some systems use python3, others use python. In this guide I use python. If that fails, swap it to python3.
First script
hello.py
print("hello, world") name = "steve" print(f"hi {name}")
run
python hello.py
Virtual environments
Always use a virtual environment per project. It avoids dependency conflicts between projects.
create and activate
mkdir python-demo cd python-demo python -m venv .venv # Linux/macOS source .venv/bin/activate # Windows PowerShell .venv\Scripts\Activate.ps1
deactivate
deactivate
// do this early
Create and activate .venv before installing packages.
Packages with pip
common commands
python -m pip install requests python -m pip install -U pip python -m pip list python -m pip freeze > requirements.txt python -m pip install -r requirements.txt
// why python -m pip
It guarantees pip belongs to the Python interpreter you are currently using.
Core syntax
Variables and types
name = "Ada"
age = 28
height = 1.72
is_admin = False
print(type(name))
Conditionals and loops
if age >= 18:
print("adult")
else:
print("minor")
for i in range(3):
print(i)
// biggest beginner gotcha
Indentation is syntax. Use consistent spaces (4 spaces per level is standard).
Data structures
list / dict / set / tuple
langs = ["python", "go", "rust"] user = {"name": "steve", "role": "dev"} tags = {"linux", "python"} point = (10, 20) langs.append("bash") print(user["name"])
Functions
functions.py
def greet(name: str) -> str:
return f"hello, {name}"
def area(width: float, height: float) -> float:
return width * height
print(greet("steve"))
print(area(3, 5))
Files and JSON
read and write text
with open("notes.txt", "w", encoding="utf-8") as f: f.write("learn python\n") with open("notes.txt", "r", encoding="utf-8") as f: print(f.read())
json
import json
data = {"name": "steve", "score": 99}
with open("data.json", "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
with open("data.json", "r", encoding="utf-8") as f:
loaded = json.load(f)
print(loaded["score"])
Errors and debugging
try/except
try:
value = int("42")
except ValueError as exc:
print("bad number:", exc)
else:
print(value)
debugging basics
python -m pdb app.py # step debugger python -m pip install ruff # fast linting ruff check .
Project layout
starter layout
my_project/
.venv/
src/
main.py
tests/
test_main.py
requirements.txt
README.md
.gitignore
// .gitignore minimum
Add .venv/, __pycache__/, and *.pyc so generated files do not get committed.
Practice plan (7 days)
- Day 1: Install Python and run
hello.py. - Day 2: Use
if,for, andwhilein a small script. - Day 3: Use lists and dictionaries to model simple data.
- Day 4: Write 3 functions with parameters and return values.
- Day 5: Read and write one text file and one JSON file.
- Day 6: Create a project with
.venvandrequirements.txt. - Day 7: Build a tiny CLI tool (todo list or unit converter).
Cheat sheet
daily python commands
python --version python file.py python -m venv .venv source .venv/bin/activate python -m pip install package python -m pip freeze > requirements.txt python -m pip install -r requirements.txt deactivate
// next step
After this guide, pick one small project and finish it end to end. That is where Python sticks.