Variables & the core types
A variable is a name pointing at a value. Python figures out the type from the value itself — you never declare int x. This lesson covers the handful of built-in types that everything else is built from.
Assignment: a name, not a box
score = 42 # name `score` now points at the integer 42
name = "Ada" # `name` points at a string
score = score + 8 # rebind `score` to a new value, 50Think of = as "make this name refer to this value," not "put this value in this box." Names are labels you can freely re-stick onto new values. A name can be re-pointed at a different type at any time (Python is dynamically typed) — legal, but usually a smell.
The core built-in types
- int — whole numbers, unlimited size (Python won't overflow:
2 ** 1000just works). - float — decimals. Beware:
0.1 + 0.2gives0.30000000000000004(binary floating point — a universal gotcha, not a Python bug). - str — text in single or double quotes. Immutable (can't change in place).
- bool —
True/False. Capitalized. Secretly ints:True == 1. - None — the "nothing" value. A function with no
returngives backNone.
Checking and converting types
type(42) # <class 'int'>
type(3.14) # <class 'float'>
int("100") # 100 — string to int
str(100) # "100" — int to string
float("3.5") # 3.5
int(3.9) # 3 — truncates toward zero, does NOT round
bool("") # False — empty string is "falsy" (more in section 2)Converting is explicit — Python won't silently turn "5" into 5 for you. "5" + 3 raises a TypeError, not 8. This strictness catches real bugs.
Naming rules and conventions
- Must start with a letter or
_, then letters/digits/underscores. Case-sensitive. - Convention:
snake_casefor variables and functions,UPPER_CASEfor constants. - Names should say what the value means:
user_count, notnorx2.
A TypeError means you mixed incompatible types — the single most common beginner error. Read it as "you tried an operation the types don't support," then check what each side actually is with type().