Variables
In short
A variable is a name that points to a value, created the first time you assign to it with = — for example age = 30. Python works out the type automatically, and you can point the same name at a new value at any time.
Creating variables
name = "Asha"
age = 30
height_m = 1.68
is_student = False
print(name, age, height_m, is_student)Output
Asha 30 1.68 False
There's no separate declaration step. = means "make this name point to this value", not "equals" in the maths sense.
Naming rules and style
- Use letters, digits and underscores. A name can't start with a digit.
- Names are case-sensitive:
totalandTotalare different. - Python style (PEP 8) uses
snake_case:monthly_salary, notmonthlySalary. - Pick names that say what the value means:
price_eurbeatsx. - Don't reuse built-in names such as
list,strorprint.
Types are worked out for you
score = 10
print(type(score))
score = "ten"
print(type(score))Output
<class 'int'> <class 'str'>
Python is dynamically typed: the value has a type, the name doesn't. That's flexible, but changing what a variable holds halfway through a program makes it harder to read — keep one meaning per name.
Assigning several variables
x, y = 3, 4
print(x, y)
x, y = y, x # swap without a temporary variable
print(x, y)Output
3 4 4 3
Constants
Python has no locked constants. By convention, names in UPPER_CASE mean "don't change this": VAT_RATE = 0.21.
Putting variables in text with f-strings
name = "Asha"
age = 30
print(f"{name} is {age} years old.")
print(f"Next year {name} will be {age + 1}.")Output
Asha is 30 years old. Next year Asha will be 31.
Practice
Create city = "Brussels" and days = 3, then print exactly: 3 days in Brussels.
Expected output:
3 days in Brussels