← Tools
Computing · Lesson 5

Python Recap

A recap of the key Python concepts from last term — print, input, variables, data types, if / elif, loops, random and Turtle. Each section has an explanation, an example to try and a quick demo. Then test yourself with the 25-question quiz.

This Lesson

Learning objective

  • What can I recall from programming in Python?

What we will cover

  • A recap of the key Python concepts from last term.

Try every example yourself in the online compiler:

Open Programiz Python →

Do now

What apps use Python? Try to guess 3 that you have come across in your daily life.
Python is used behind the scenes by YouTube, Instagram, Spotify, Netflix, Reddit and Dropbox — as well as in AI, science and games.

1Print

The print statement in Python is used to display messages or values on the screen. It helps output information that can be seen by the user. For example, print("Hello, world!") would display the message Hello, world! when the program is run.

Example
print("Hello World")
print("This is my first program")
print("Author: Your Name")
Output
Hello World
This is my first program
Author: Your Name

Using print for calculations

Do not use speech marks for calculations — anything in speech marks is printed exactly as it is typed, so print("6*7") shows 6*7, but print(6*7) works out the answer.

OperatorMeaningExampleOutput
+Addprint(45+97)142
-Subtractprint(199-84)115
*Multiplyprint(6*7)42
/Divideprint(9/2)4.5
**To the power ofprint(4**2)16
//Integer division (whole number part only)print(6//2)3
%Modulus (the remainder)print(7%2)1

2Input

The input function in Python is used to take user input during program execution. It allows interaction with the user by prompting them to enter values. For example, name = input("Enter your name: ") captures the user's name, and the entered value is stored in the variable name.

Example
firstName = input("Please enter your name: ")
print("Hello", firstName)
Try it — type a name and run

Inputting numbers

input always gives back text (a string). To do maths with what the user types, wrap it in int() to turn it into a whole number.

Example
year = int(input("What year is it? "))
age = int(input("How old are you? "))
answer = year - age
print("You were born in", answer)
Output (user types 2026 and 13)
What year is it? 2026
How old are you? 13
You were born in 2013

3Variables

Variables are like containers that store data in a program. They have names and hold different types of information, such as numbers or text. For instance, score = 100 assigns the value 100 to the variable named score, making it accessible and modifiable in the program.

Example
num1 = 67
num2 = 91

totalAnswer = num1 + num2
subtractAnswer = num1 - num2
multiplyAnswer = num1 * num2
divisionAnswer = num1 / num2

print("Calculation Results")
print(totalAnswer)
print(subtractAnswer)
print(multiplyAnswer)
print(divisionAnswer)
Output
Calculation Results
158
-24
6097
0.7362637362637363
How can you tell it's a variable? It has a name with no speech marks around it, and it is given a value with a single =. Names can't contain spaces or start with a number — use player_score or playerScore.
Changing a variable: score = score + 5 takes the old value, adds 5 and stores the result back in score.

4Data Types

Data types in Python define the type of data a variable can hold. Common types include integers (whole numbers), floats (decimal numbers), and strings (text). Understanding data types helps in performing appropriate operations on variables, ensuring accurate and meaningful results.

Data typePython nameHoldsExamples
IntegerintWhole numbers42, -7, 100
FloatfloatDecimal numbers3.14, 0.5, 9.99
StringstrText — always inside speech marks"Hello", "Year 9", "42"
BooleanboolOnly True or FalseTrue, False
Example
age = 13
height = 1.62
name = "Harry"
print(type(age))
print(type(height))
print(type(name))
print("5" + "5")
print(5 + 5)
Output
<class 'int'>
<class 'float'>
<class 'str'>
55
10
Why does it matter? "5" + "5" joins two strings to make 55, but 5 + 5 adds two integers to make 10. The data type changes what the program does.

5If Statement

An if statement allows a program to make decisions based on conditions. If a specified condition is true, certain code blocks are executed. For example, if score > 90: print("Excellent!") prints Excellent! if the variable score is greater than 90.

Example
mark = int(input("What was your mark? "))

if mark >= 50:
    print("Pass")
else:
    print("Fail")
    print("You will have to retake this test.")
Try it — drag the mark
Remember: end the condition line with a colon : and indent the code underneath. else runs when the condition is false.

Comparison operators

OperatorMeaningOperatorMeaning
==Equal to!=Not equal to
>Greater than<Less than
>=Greater than or equal to<=Less than or equal to

A single = stores a value in a variable. A double == compares two values.

6Else If Statement (elif)

An else if statement (elif) extends the decision-making capability of an if statement. If the initial condition is false, it checks additional conditions. For instance, if x > 0: print("Positive") elif x < 0: print("Negative") checks whether the variable x is positive or negative.

Example
answer = input("What is the answer? a, b or c. ")

if answer == "a":
    print("wrong, close though")
elif answer == "b":
    print("wrong, by a loooong way")
elif answer == "c":
    print("Correct")
else:
    print("You didn't answer a, b or c. Were you not listening?")
Try it — pick an answer

        
Python checks each condition from the top down and runs only the first one that is true. If none are true, the else runs.

7While Loops

While loops repeat a block of code as long as a specified condition is true. They're useful for tasks that need to be performed repeatedly. For example, while count < 5: print("Count:", count) prints the count value as long as it's less than 5.

Example 1 — counting
count = 0
while count < 5:
    print("Count:", count)
    count = count + 1
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Example 2 — keep asking
reply = input("Will you take us to Mount Splashmore? ")

while reply != "Yes":
    reply = input("Will you take us to Mount Splashmore? ")

print("Thanks dad!")
This loop ends only when the user types exactly Yes. A while loop is used when you don't know how many times it will repeat.
Watch out: in Example 1, if you forget count = count + 1, count stays at 0, the condition is always true and the loop runs forever (an infinite loop).

8For Loops

For loops iterate over a sequence (such as a range of numbers) and execute a block of code for each iteration. They're handy for tasks that involve a fixed number of repetitions. For instance, for i in range(5): print("Iteration:", i) prints the iteration number five times.

Example
for i in range(5):
    print("Iteration:", i)
Output
range(5) gives 0, 1, 2, 3, 4 — five numbers, starting at 0 and stopping before 5. range(0, 100) repeats 100 times (0 to 99). Use a for loop when you know how many times to repeat.

9Random Function

The random function in Python generates random numbers. It's useful for introducing unpredictability in programs. For instance, random_number = random.randint(1, 10) assigns a random integer between 1 and 10 to the variable random_number.

Example
import random

random_number = random.randint(1, 10)
print("Your number is", random_number)

names = ["Alice", "Bob", "Charlie"]
print("Chosen:", random.choice(names))
Try it — run it a few times
Press Run…
Remember: you must write import random at the top first. randint(1, 10) can give any whole number from 1 to 10 — including 1 and 10.

10Turtle in Python

The Turtle graphics library in Python is designed to introduce programming through simple drawing. It provides a turtle that can move around the screen, making it an engaging way for beginners to learn programming concepts. Commands like forward(), left(), and penup() control the turtle's actions.

Example

        
What you get
CommandWhat it does
import turtleLoads the turtle library so you can use it
turtle.forward(100)Moves forward 100 steps, drawing a line
turtle.backward(50)Moves backwards 50 steps
turtle.left(90)Turns left by 90 degrees
turtle.right(90)Turns right by 90 degrees
turtle.penup()Lifts the pen — the turtle moves without drawing
turtle.pendown()Puts the pen down so it draws again
turtle.color("red")Changes the pen colour
Shape rule: to draw a regular shape with n sides, turn 360 ÷ n degrees each time — 90° for a square, 120° for a triangle, 60° for a hexagon.

Test Yourself — 25 Questions