Learn / Beginner / Lesson 3

Variables

9 min Tested on Python 3.15Updated By
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

Python
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: total and Total are different.
  • Python style (PEP 8) uses snake_case: monthly_salary, not monthlySalary.
  • Pick names that say what the value means: price_eur beats x.
  • Don't reuse built-in names such as list, str or print.

Types are worked out for you

Python
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

Python
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

Python
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
Your code

Quick check

1. Which name follows Python style?

2. After a, b = 1, 2 and then a, b = b, a, what is a?

3. What does print(f"{2 * 5} items") show?

Use AI at work

Python is the base of AI. On AIMates you learn to use AI tools well in your daily job.

Visit AIMates →