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
python3on macOS/Linux ifpythonstill points at Python 2 (rare now, but check). - Create an isolated environment per project:
python -m venv .venvthen 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
| Type | Example | Notes |
|---|---|---|
int | 42 | Arbitrary precision — no overflow |
float | 3.14 | IEEE 754 double |
str | 'hi', "hi" | Immutable sequence |
bool | True | Capitalized |
list | [1, 2] | Mutable ordered |
dict | {'a': 1} | Key/value map |
tuple | (1, 2) | Immutable ordered |
NoneType | None | Absence 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
| Falsy | Truthy |
|---|---|
False, None, 0, 0.0 | any 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.Related
Strings Lists, dicts and comprehensions
Last refreshed 2026-09-17.