Variables & Data Types
Variables
A variable is a name that points to a value. You create one with =, and you never declare a type: Python works it out from the value (dynamic typing).
Built-in types you'll use every day
| Type | Example | Meaning |
|---|---|---|
int | 42 | whole number (no size limit) |
float | 3.14 | decimal number |
str | "Asha" | text |
bool | True / False | yes / no |
NoneType | None | "no value yet" |
Checking types
type(x)tells you the type.isinstance(x, int)checks a type in conditions. Prefer it overtype(x) == int.
Naming rules
- Letters, digits and
_; it can't start with a digit. - Use snake_case:
total_marks,is_logged_in. - Constants are written in CAPITALS by convention:
MAX_USERS = 100.
Variables can change type
x = 5 then x = "five" is allowed, which is flexible but can hide bugs.
💡 Multiple assignment: a, b = 1, 2 and swapping a, b = b, a.
▶️ Runs in the app.
Example
name = "Alice"
age = 30
height = 5.6
is_student = False
middle_name = None
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(isinstance(age, int)) # True
# Multiple assignment and swapping
a, b = 1, 2
a, b = b, a
print(a, b) # 2 1
# Big integers just work
print(2 ** 100) # 1267650600228229401496703205376