What a variable is
A variable is a name attached to a value. In Python you create one simply by assigning with = — there is no separate declaration, and you never write the type yourself:
age = 27
price = 9.99
name = "Grace"
is_student = True
nothing = None
print(age, price, name, is_student, nothing)Python figures out the type from the value on the right. You can check it at any time with type():
print(type(age))
print(type(price))
print(type(name))
print(type(is_student))A variable can later point at a different value — even one of a different type. This is called dynamic typing and it is one of the reasons Python code is short.
Naming rules and conventions
- Names contain letters, digits and underscores, and cannot start with a digit:
total_2is fine,2totalis a syntax error. - Names are case-sensitive:
score,ScoreandSCOREare three different variables. - You cannot use Python's 35 keywords (
if,for,class,None…) as names. - Convention (PEP 8): use
snake_casefor variables and functions,UPPER_CASEfor constants,CamelCasefor classes. - Pick descriptive names.
seconds_per_day = 86400tells a reader more thans = 86400.
The core data types
| Type | Example | What it holds |
|---|---|---|
int | 42, -7, 10**100 | Whole numbers of any size — Python integers never overflow |
float | 3.14, 2.0, 1e-9 | Decimal numbers (64-bit double precision) |
str | "hi", 'hi', """multi-line""" | Text, Unicode by default |
bool | True, False | Truth values (a subclass of int: True + True == 2) |
NoneType | None | "No value" — the default return of a function that returns nothing |
list | [1, 2, 3] | Ordered, changeable sequence — see lists |
tuple | (1, 2) | Ordered, unchangeable sequence |
dict | {"a": 1} | Key → value mapping — see dictionaries |
set | {1, 2, 3} | Unordered collection of unique values |
Type conversion (casting)
Input from a user always arrives as a string. To do maths with it you convert explicitly:
raw = input("Enter your birth year: ") # e.g. 1999
year = int(raw)
print("You turn", 2026 - year, "this year")
print(float("3.5") * 2)
print(str(42) + " apples")
print(int(7.9)) # truncates, does not round
print(round(7.9)) # roundsConverting something that cannot be converted raises a ValueError — int("hello") for example. The lesson on errors and exceptions shows how to handle that gracefully.
Multiple assignment and swapping
x, y = 10, 20
x, y = y, x # swap without a temporary variable
print(x, y)
a = b = c = 0 # same value to several names
print(a, b, c)
first, *rest = [1, 2, 3, 4]
print(first, rest)Mutable vs immutable
Numbers, strings, booleans and tuples are immutable: once created they never change, so "modifying" one really creates a new object. Lists, dictionaries and sets are mutable: they change in place. The difference matters when two names refer to the same object:
a = [1, 2, 3]
b = a # b is the SAME list, not a copy
b.append(4)
print(a)
c = a.copy() # a real copy
c.append(5)
print(a, c)This catches almost every beginner once. Remembering that = copies a reference, not the data, will save you a long debugging session later.
Frequently asked questions
Do I have to declare variable types in Python?
No. Python is dynamically typed: the type comes from the value you assign. You can add optional type hints such as age: int = 27 for readability and tooling, but they are not enforced at runtime.
What is the difference between int and float?
int holds whole numbers of unlimited size; float holds decimals in 64-bit precision. Dividing two ints with / always gives a float; use // for whole-number division.
Why does 0.1 + 0.2 not equal 0.3 in Python?
Floats are stored in binary and cannot represent most decimals exactly, so 0.1 + 0.2 gives 0.30000000000000004. Use round(), or the decimal module for money.
What does None mean?
None is Python's null value. It signals 'no value here' and is what a function returns when it has no return statement.