Python: getting started

Running Python, indentation as syntax, variables, and the built-in types you touch in every script.

Running code

python --version
python hello.py

# interactive REPL - the fastest way to experiment
python
>>> 2 + 3
5
  • Use python3 on macOS/Linux if python still points at Python 2 (rare now, but check).
  • Create an isolated environment per project: python -m venv .venv then activate it.
  • source .venv/bin/activate (macOS/Linux) or .venv\Scripts\activate (Windows).
πŸ’‘
Never install packages globally with sudo pip install. Virtual environments prevent one project's pins from breaking another's.

Indentation is syntax

Python uses indentation instead of braces to delimit blocks. Four spaces per level is the convention β€” and mixing tabs with spaces is a syntax error, so configure your editor to insert spaces.

score = 85

if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
else:
    grade = 'C'

print(grade)  # B
⚠️
Inconsistent indentation raises IndentationError before your code runs at all. If a block looks right but fails, check for a stray tab.

Core types

TypeExampleNotes
int42Arbitrary precision β€” no overflow
float3.14IEEE 754 double
str'hi', "hi"Immutable sequence
boolTrueCapitalized
list[1, 2]Mutable ordered
dict{'a': 1}Key/value map
tuple(1, 2)Immutable ordered
NoneTypeNoneAbsence of a value
x, y = 1, 2          # multiple assignment
x, y = y, x          # swap without a temp

n = 10
print(f'n is {n}')   # f-strings: the modern way to format
print(type(n).__name__)
⚠️
There is no ++ in Python. n++ is silently parsed as two unary plus operators, doing nothing β€” write n += 1.

Truthiness and None

FalsyTruthy
False, None, 0, 0.0any non-zero number
'', [], {}, (), set()any non-empty container

Check for None with is None, never == None. Use is not None when 0 or '' are legitimate values you must not skip.

FAQ

Python 2 or 3?
Python 3, always. Python 2 reached end of life in 2020 and receives no security updates.
How do I format strings?
f-strings: f'Hello {name}'. They are readable and fast; % and .format() still work for legacy code.

Strings Lists, dicts and comprehensions

Last refreshed 2026-09-17.