Lesson 2 of 14 · 6 min

Variables and types

A variable is a name for a value. Create one with =:

city = "Bulawayo"
population = 650000
population = population + 1000
print(city, population)    # Bulawayo 651000

= means "store this value under this name". It does not mean "equals" like in maths: population = population + 1000 reads as "take population, add 1000, and store the result back".

Variable names use lowercase letters and underscores, like first_name. They cannot start with a number or contain spaces.

Types

Every value has a type:

Value Type What it is
"Tendai" str text (a string)
21 int a whole number
1.75 float a number with a decimal point
True / False bool yes or no

"21" with quotes is text, not a number. type(value) tells you the type.

print can take several values separated by commas, and puts a space between them.

Your task

  1. Create name with the value "Tendai".
  2. Create age with the value 21.
  3. On a new line, add 1 to age.
  4. Print Tendai is 22 using your variables.
main.py
# 1. name


# 2. age


# 3. add 1 to age


# 4. print the sentence

Python 3.14 runs right here in your browser. The first run downloads about 10 MB; after that it is instant.

Discussion

Stuck? Ask here. Be kind, and share hints rather than full answers.

Loading the discussion…