Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Variables, data types and print in Python

After this lesson: I can store values in variables and print a result.

55 min lesson4 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

Four friends and one bill

A variable in Python is a name that holds a value, such as a price or an age. Programs use variables to remember things, so you can change one number and let the program redo every sum.

Four friends share a restaurant bill and add a tip. Doing it once is easy. A program does it for any bill, with no new work.

By the end of this lesson

Note

I can store values in variables and print a result.

Key words

WordWhat it meansExample
Variablea name that holds a valuepeople = 4
Integera whole number, called int in Python19
Floata number with a decimal point2.5
Stringtext inside quotes, called str in Python"Ana"
Booleaneither True or False, called bool in Python
f-stringa string that fills in values from variablesf"Hello, {name}"

The cells students fill in are left blank in this preview.

A variable is a name for a value

Write people = 4 and Python stores the number 4 under the name people. The sign = is called assignment. It means: work out the right side, then store it under the name on the left.

Later lines use the name instead of the number. Give a new value and the old one is gone. Choose names that say what the value is, such as total_price.

A bill split between friends

Code: python
# A bill for four people, with a tip.
bill = 45.0
tip = 5.0
people = 4
each = (bill + tip) / people
print("Each person pays", each)
# Change people to 5 and run again.

A common mistake

Warning

Many people read total = total + 5 as a false sum, because a number cannot equal itself plus 5. In Python, = does not mean "is equal to". It means "store". The right side is worked out first, then stored under the name on the left.

Every value has a type

Python keeps track of what kind of value each variable holds. This is its type. Whole numbers are int, numbers with a decimal point are float, text is str, and True or False is bool.

The type decides what you can do. You can add two numbers. Adding two pieces of text joins them.

Worked example: four values, four types

ValueTypeGood for
19intan age, a count of people
2.5floata price, a length
"Ana"stra name, a message
Trueboola yes or no answer
"19"strtext that looks like a number

The cells students fill in are left blank in this preview.

Ask Python for three types

Command
python3 -c "print(type(3), type(3.0), type('3'))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Four types in one program

Code: python
age = 19            # int: a whole number
price = 2.5         # float: has a decimal point
name = "Ana"        # str: text in quotes
is_student = True   # bool: True or False
print(type(age), type(price))
print(type(name), type(is_student))

Python does arithmetic with operators

An operator is a symbol that works on values. +, - and * add, take away and multiply. / divides and always gives a float.

// divides and keeps only the whole part. % gives the remainder, what is left over. ** raises a number to a power.

Worked example: seven operators on 7 and 2

OperatorMeaningExampleResult
+add7 + 29
-take away7 - 25
*multiply7 * 214
/divide7 / 23.5
//whole part of a division7 // 23
%remainder7 % 21
**power7 ** 249

The cells students fill in are left blank in this preview.

Three divisions of 7 by 2

Command
python3 -c "print(7 / 2, 7 // 2, 7 % 2)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Two to the power of ten

Command
python3 -c "print(2 ** 10)"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

An f-string puts values into text

Put the letter f before the opening quote and Python fills in anything inside curly brackets. print(f"Hello, {name}") prints the value held by name.

This is neater than joining pieces of text with +, and it works with numbers too.

Fill in a name and a total

Code: python
name = "Ana"
total = 12.5
print(f"{name} pays {total} pounds")
# Change name to your own name and run again.

The same idea on one line

Command
python3 -c "name = 'Ana'; print(f'Hello, {name}')"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further

In the terminal, type python3 -c "print(0.1 + 0.2)" and read the answer. Floats are stored as close approximations, so a sum can be a tiny bit out.

Wrap the sum in round(..., 2) and run it again. Money is the usual reason to round.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.