Skip to content
Study Hall
Lesson 1 of 3Coding with Python
Unit 4 · Lesson 1 of 3

Variables, Expressions and Output in Python

Write short Python programs that store values in variables, evaluate expressions with the correct order of operations, print results, and use parameters you can change and rerun.

8 min read6 quick checks11 practice questionsInteractiveAsk AI about this →

Key things to remember

  1. 1An assignment name = expression works out the right side using the current values, then stores the result in the variable on the left. The left side must be a variable name, not a calculation.
  2. 2= stores a value; == compares two values and gives True or False.
  3. 3Operators: +, -, * (multiply), / (divide, decimal answer), // (divide and round down), % (remainder), ** (power).
  4. 4Order of operations: brackets, then **, then multiplication and division from left to right, then addition and subtraction from left to right. Python has no implied multiplication.
  5. 5A variable keeps its value until a new assignment changes it. Changing one variable does not recalculate variables that were worked out from it earlier.
  6. 6A parameter is a value that stays fixed while the program runs but can be edited to model a new situation, like and in .

A bike rental shop charges 15 dollars plus 4 dollars per hour, so the cost is . To make a price list for 1 to 12 hours you would substitute into that expression 12 times. A computer can do it in an instant, but only if you tell it exactly what to do in a language it understands. Python is one of those languages, and many of its ideas come straight from algebra: a Python variable is a name for a value, an expression is worked out with the same order of operations you already know, and a formula becomes a single line of code. A program is a list of instructions that Python carries out from the top down, one line at a time. The simplest instruction is print, which shows a result on the screen. This unit uses Python 3; every program here runs in IDLE (the editor that comes with Python) or in an online Python editor.

print("Hello")
print(3 + 4)
print("3 + 4")
print("Total:", 3 + 4)
Output, one line per print: Hello, then 7, then 3 + 4, then Total: 7.

Each print starts a new line of output. Text inside quotation marks is a string: Python shows it exactly as written and does not calculate anything inside it, which is why print("3 + 4") shows 3 + 4. Without quotation marks, Python evaluates the expression first and prints the answer, 7. When you separate several items with commas, print shows them on one line with a single space between them, so print("Total:", 3 + 4) shows Total: 7.

Variables: named boxes for values

h = 3
cost = 15 + 4 * h
print(cost)
h = 5
print(h, cost)
Output: 27 on the first line, then 5 27 on the second.
Program1h = 32cost = 15 + 4 * h3print(cost)4h = 55print(h, cost)Output275 27from line 3from line 5After line 2h3cost2715 + 4 × 3 = 27After line 4h353 replaced by 5cost27still 27(not recalculated)=store the right side's value in the box==compare two values: True or False
Fig 1The five-line program drawn as labelled boxes. After line 2, h holds 3 and cost holds 27. After line 4, h holds 5 but cost still holds 27. The output is 27, then 5 27.

Think of a variable as a labelled box. The line h = 3 is an assignment: it means "put 3 in the box named h". Python always works out the right side of = first, using the values the boxes hold at that moment, and then stores the result in the variable on the left. So line 2 calculates and stores 27 in cost. Line 4 replaces the 3 in h with 5, but cost keeps 27: it was calculated once, back when h was 3, and nothing tells Python to calculate it again. That is a real difference from algebra, where is a relationship that stays true for every value of . A variable name can use letters, digits and underscores (like bike_cost), cannot start with a digit, contain spaces or be one of Python's own keywords (such as for or if), and is case-sensitive, so Cost and cost are two different boxes.

One equals sign stores, two equals signs compare
In Python, x = 5 is an instruction (store 5 in x), not a statement that two things are equal. To ask whether x equals 5, write x == 5; Python answers True or False, so print(x == 5) prints True when x holds 5. This is why x = x + 1 makes sense in code even though no number equals itself plus 1: it means "take the value in x, add 1, and store the result back in x". The left side of a single = must be a variable name, not a calculation, so 15 + 4 * h = cost stops the program with a SyntaxError.
Quick checkTry it before you read on
1
What does this program print?n = 2 n = n + 5 n = n - 1 print(n)
Quick checkTry it before you read on
2
Which of these can be used as a Python variable name?
VideoKhan Academy's Python course shows how an assignment statement stores a value under a name, and how the stored value changes when the variable is assigned again.Watch on YouTube

Arithmetic and the order of operations

OperatorMeaningExampleOutput
+addition7 + 29
-subtraction7 - 25
*multiplication7 * 214
/division (the answer is a decimal, even when it divides evenly)7 / 2 and 6 / 23.5 and 3.0
//division, rounded down to an integer7 // 23
%remainder after whole-number division7 % 21
**power (exponent)7 ** 249
Python's arithmetic operators. Multiplication always needs its own symbol in Python.
print(2 + 3 * 4)
print((2 + 3) * 4)
print(-3 ** 2)
print((-3) ** 2)
print(7 / 2)
print(6 / 2)
Output, one per line: 14, 20, -9, 9, 3.5 and 3.0.

Python follows the same order of operations as math class (BEDMAS): brackets first, then powers, then multiplication and the three kinds of division from left to right, then addition and subtraction from left to right. So the first line is , while the brackets in the second line force . The power is done before the negative sign, exactly like in math; to square negative 3 you need brackets: . Two habits from algebra do not carry over. Python has no implied multiplication, so must be typed with a * between the 4 and the h, and needs a * before the bracket too. And a fraction bar groups its whole numerator, so must be typed (a + b) / 2 with brackets.

BEDMAS order, one step at a time1. Brackets(2 + 3) * 4 - 2 ** 32 + 3 = 52. Power5 * 4 - 2 ** 32 ** 3 = 83. Multiply5 * 4 - 85 × 4 = 204. Subtract20 - 820 − 8 = 12Result12what print shows7 dots in groups of 23 full groups1 left over7 / 2gives3.53 groups and half a group7 // 2gives3full groups only (rounded down)7 % 2gives1the remainder: dots left over
Fig 2(2 + 3) * 4 − 2 ** 3 worked out one step at a time in BEDMAS order: the brackets give 5, the power gives 8, the multiplication gives 20, and the subtraction gives 12. Beside it, 7 dots split into groups of 2 show why 7 / 2 is 3.5, 7 // 2 is 3 and 7 % 2 is 1.
Quick checkTry it before you read on
1
What does print(10 - 2 ** 3) print?
Quick checkTry it before you read on
2
Which line of Python correctly calculates ?
VideoKhan Academy traces how Python evaluates a longer arithmetic expression one operation at a time, following the order of operations.Watch on YouTube

Parameters: change one value and rerun

In the line , the letters and are variables: they change as you move along the line. The letters and are parameters: they stay fixed for one particular line, but changing them gives a different line. Programs use the same idea. Put each parameter on its own line at the top of the program; then to model a new situation you edit one line and run the program again, instead of rewriting the formula.

m = 3
b = 2
x = 5
y = m * x + b
print("When x =", x, "y =", y)
Output: When x = 5 y = 17. Change the first line to m = -2 and rerun: When x = 5 y = -8.
Run 11m = 32b = 23x = 54y = m * x + b5print("When x =", x, "y =", y)Output:When x = 5 y = 173(5) + 2 = 17Run 2: edit line 1 only1m = -22b = 23x = 54y = m * x + b5print("When x =", x, "y =", y)Output:When x = 5 y = -8-2(5) + 2 = -8-10-5051015200123456xyy = 3x + 2y = −2x + 2(5, 17)(5, −8)
Fig 3The same program run twice. With m = 3 it prints y = 17; after editing only the first line to m = -2 it prints y = -8. The graph shows the two lines y = 3x + 2 and y = -2x + 2, which share the y-intercept b = 2, with the printed points (5, 17) and (5, -8) marked.
Loading interactive…
Pick a program, step through it one line at a time and watch the value and type of each variable change, then change the marked numbers and run it again.
Quick checkTry it before you read on
1
This program prints 27.fee = 15 rate = 4 h = 3 cost = fee + rate * h print(cost) If only the second line is changed to rate = 6, what does the program print?
Quick checkTry it before you read on
2
In a program for , which letters are the parameters?

Worked examples

Reveal one step at a time and try to beat it to the next line.

Example 1
Predict what this program prints, then check each line.length = 8 width = 5 area = length * width perimeter = 2 * (length + width) print(area, perimeter)
Example 2
Predict what this program prints.x = 4 y = x * 3 x = x + 5 print(x, y)
Example 3
The formula converts a temperature from degrees Celsius to degrees Fahrenheit. Write a program with the Celsius temperature as a parameter, predict its output for 25 degrees Celsius, then change the parameter to and predict again.
Example 4
A hoodie costs 24.99 dollars before the 13% HST. Write a program that finds the total and prints it rounded to the nearest cent.

Where marks get lost

  • Writing 4h or 2(x + 1) as you would in algebra. Python needs the multiplication sign: type a * between the 4 and the h, and between the 2 and the bracket.
  • Writing the assignment backwards, as in 15 + 4 * h = cost. The variable being stored must be on the left: cost = 15 + 4 * h.
  • Using = when you mean to compare. To check whether x equals 5, write x == 5, which gives True or False.
  • Expecting a variable to update by itself. If cost was calculated from h, changing h later leaves cost unchanged until you run the calculation line again.
  • Typing ^ for a power. In Python, 2 ^ 3 is a different operation and prints 1, not 8; powers use a double star, so write 2 ** 3.

Flashcards 8

Variable (in Python)
= versus ==
/ versus // versus %
Power in Python
+ 4 more

Practice

1
In Python, what does the line x = 7 do?
2
What does this program print?a = 5 b = a * 2 + 1 print(b)
3
What does print(20 - 4 * 3 + 2) print?
4
What does print(9 / 3) print?
5
What does this program print?x = 4 y = x + 6 x = 20 print(y)
6
Which Python expression calculates , the average of a and b?
7
Match each Python operator to what it does.
*
/
//
%
**
8
This program prints When x = 5 y = 17.m = 3 b = 2 x = 5 y = m * x + b print("When x =", x, "y =", y) If you change only the first line to m = -4 and run it again, what value of y is printed?
9
What does print(5 ** 2 - 4) print?
10
Put these lines in an order that runs without errors and prints the price of a 40 dollar item with 13% HST added.
  1. 1tax = price * 0.13
  2. 2print(total)
  3. 3total = price + tax
  4. 4price = 40
11
The variable h already holds 3. Which line stops the program with an error?

Spot a mistake on this page? Let us know