Python · Lesson 1.1

Variables

Give values names, update them, and pick names that make your code easy to read.

1Store a value under a name

A variable is a name that points to a value. You create one with =, then use the name wherever you need the value.

city = "Lisbon"
visitors = 1200
print(city, visitors)
Python

Practice

Create a variable product holding the text Coffee beans and a variable stock holding the number 48. Then print both with one print().

# Create the two variables, then print them
Python

2Update a value

You can point a name at a new value at any time. += is a short way to add to a variable.

orders = 10
orders = orders + 5
orders += 1
print(orders)
Python

Practice

A wallet starts with 100. Add 25, then subtract 40, then print the balance.

balance = 100
# Add 25, then subtract 40

print(balance)
Python

3Good names

Names can use letters, digits and underscores, but can't start with a digit or contain spaces. Python style uses lowercase words joined by underscores, like total_sales.

total_sales = 980
average_order_value = 24.5
print(total_sales, average_order_value)
Python

Common mistake

Names are case-sensitive: Sales and sales are two different variables. Pick one style and stick to it.

Practice

These names break Python's rules, so the code won't run. Rename them to first_name and year_2026_target, then run it.

first name = "Ana"
2026_target = 500
print(first name, 2026_target)
Python

4Several names at once

You can assign several variables on one line. This also gives you an easy way to swap two values.

low, high = 3, 9
low, high = high, low
print(low, high)
Python

Practice

The two values are the wrong way round. Swap this_month and last_month in one line, then print them in the order this_month, last_month.

this_month = 310
last_month = 420
# Swap them here

print(this_month, last_month)
Python