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.
Try every example yourself in the online compiler:
Open Programiz Python →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.
print("Hello World")
print("This is my first program")
print("Author: Your Name")Hello World This is my first program Author: Your Name
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.
| Operator | Meaning | Example | Output |
|---|---|---|---|
| + | Add | print(45+97) | 142 |
| - | Subtract | print(199-84) | 115 |
| * | Multiply | print(6*7) | 42 |
| / | Divide | print(9/2) | 4.5 |
| ** | To the power of | print(4**2) | 16 |
| // | Integer division (whole number part only) | print(6//2) | 3 |
| % | Modulus (the remainder) | print(7%2) | 1 |
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.
firstName = input("Please enter your name: ")
print("Hello", firstName)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.
year = int(input("What year is it? "))
age = int(input("How old are you? "))
answer = year - age
print("You were born in", answer)What year is it? 2026 How old are you? 13 You were born in 2013
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.
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)Calculation Results 158 -24 6097 0.7362637362637363
=. Names can't contain spaces or start with a number — use player_score or playerScore.score = score + 5 takes the old value, adds 5 and stores the result back in score.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 type | Python name | Holds | Examples |
|---|---|---|---|
| Integer | int | Whole numbers | 42, -7, 100 |
| Float | float | Decimal numbers | 3.14, 0.5, 9.99 |
| String | str | Text — always inside speech marks | "Hello", "Year 9", "42" |
| Boolean | bool | Only True or False | True, False |
age = 13
height = 1.62
name = "Harry"
print(type(age))
print(type(height))
print(type(name))
print("5" + "5")
print(5 + 5)<class 'int'> <class 'float'> <class 'str'> 55 10
"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.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.
mark = int(input("What was your mark? "))
if mark >= 50:
print("Pass")
else:
print("Fail")
print("You will have to retake this test."): and indent the code underneath. else runs when the condition is false.| Operator | Meaning | Operator | Meaning |
|---|---|---|---|
| == | 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.
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.
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?")else runs.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.
count = 0
while count < 5:
print("Count:", count)
count = count + 1
Count: 0 Count: 1 Count: 2 Count: 3 Count: 4
reply = input("Will you take us to Mount Splashmore? ")
while reply != "Yes":
reply = input("Will you take us to Mount Splashmore? ")
print("Thanks dad!")
count = count + 1, count stays at 0, the condition is always true and the loop runs forever (an infinite loop).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.
for i in range(5):
print("Iteration:", i)
range(0, 100) repeats 100 times (0 to 99). Use a for loop when you know how many times to repeat.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.
import random
random_number = random.randint(1, 10)
print("Your number is", random_number)
names = ["Alice", "Bob", "Charlie"]
print("Chosen:", random.choice(names))Press Run…
import random at the top first. randint(1, 10) can give any whole number from 1 to 10 — including 1 and 10.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.
| Command | What it does |
|---|---|
| import turtle | Loads 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 |