On this page
  1. Practice questions
  2. Section A
  3. Section B
  4. Section C
  5. Section D
  6. Quizzes
  7. Python lab

Year 7 · Python

Practice questions

Work through Sections A–D in order. They get harder as you go. Try each question yourself, then press Show solution to check.

Work through these in order. They get harder as you go. Solutions are hidden — try each question yourself, then click Show solution to check.

Section A: Variables and data types

A1. What data type is each of these values?

  • (a) 17
  • (b) "17"
  • (c) 17.0
  • (d) False
  • (e) "True"

A2. Which of these are valid variable names? For the invalid ones, say why.

  • (a) player_name
  • (b) 2nd_place
  • (c) total score
  • (d) levelUp
  • (e) for

A3. What will this code print?

x = 4
y = 10
x = x + y
print(x)
print(y)

A4. Work out the result of each expression:

  • (a) 15 / 4
  • (b) 15 // 4
  • (c) 15 % 4
  • (d) 2 ** 3
  • (e) 3 + 2 * 5

A5. What will this code print?

a = "3"
b = "4"
print(a + b)
print(int(a) + int(b))

A6. This code gives an error. Explain why and fix it.

coins = 50
print("You have " + coins + " coins")

A7. Write a program that stores your favourite food in a variable called food and the number of times you eat it per week in a variable called times. Then print a sentence like:

Output:

I eat pizza 2 times a week

A8. Write a program that asks the user for the length and width of a rectangle (whole numbers) and prints its area.

Use input() and convert with int().

Section B: Conditionals

B1. What will each expression give, True or False? Assume n = 8.

  • (a) n > 5
  • (b) n == 10
  • (c) n != 8
  • (d) n >= 8 and n < 10
  • (e) n < 0 or n > 7
  • (f) not n == 8

B2. What will this code print?

points = 45

if points > 50:
    print("Gold")
elif points > 30:
    print("Silver")
else:
    print("Bronze")

B3. What will this code print when points = 75? Why might this not be what the programmer wanted?

points = 75

if points > 30:
    print("Silver")
elif points > 50:
    print("Gold")

B4. Find and fix three errors in this code:

number = int(input("Enter a number: "))
if number = 0
print("Zero")

B5. Write a program that asks the user for a number and tells them if it is positive, negative or zero.

Use if / elif / else.

B6. Write a program that asks for a whole number and tells the user if it is even or odd. (Hint: use %.)

B7. A cinema charges:

  • £5 for children under 12
  • £8 for anyone aged 12 to 64
  • £6 for anyone aged 65 or over

Write a program that asks for the customer's age and prints the ticket price.

B8. Write a program that asks the user to enter a username and password. If the username is "admin" and the password is "secret", print "Welcome!", otherwise print "Login failed".

Section C: For loops

C1. Write down the numbers produced by each range():

  • (a) range(4)
  • (b) range(2, 7)
  • (c) range(1, 10, 3)
  • (d) range(10, 4, -2)

C2. How many times will "Hi" be printed?

for i in range(3, 8):
    print("Hi")

C3. What will this code print?

for i in range(1, 4):
    print(i * 10)

C4. Complete a trace table for this code and say what is printed at the end.

total = 0
for n in range(2, 9, 2):
    total = total + n
print(total)

C5. Write a program that prints the numbers from 1 to 20.

C6. Write a program that asks the user which times table they want, then prints it from 1 to 12. For example, if they enter 3:

Output:

1 x 3 = 3 2 x 3 = 6 ... 12 x 3 = 36

C7. Write a program that prints a countdown from 10 to 1 and then prints "Happy New Year!".

C8. Write a program that asks the user for a number, then adds up all the whole numbers from 1 up to and including that number. For example, if the user enters 4, the program prints 10 (because 1 + 2 + 3 + 4 = 10).

Section D: Challenge questions

D1. FizzBuzz. Print the numbers 1 to 20, except:

  • If the number is divisible by 3, print "Fizz" instead
  • If it's divisible by 5, print "Buzz" instead
  • If it's divisible by both 3 and 5, print "FizzBuzz" instead

D2. Mark collector. Write a program that asks the user for 5 test marks (one at a time, using a loop). At the end, print the total and the average.

D3. Star triangle. Write a program that asks for a number and prints a triangle of stars. For example, if the user enters 4:

Output:

* ** *** ****

(Hint: remember that "*" * 3 gives "***".)

D4. Guessing game. The secret number is 7. Give the user 3 guesses using a for loop. After each guess, tell them "Correct!", "Too high" or "Too low".

Extra challenge: can you stop the game from asking more questions after they guess correctly? Hint: look up the break keyword.

Year 7 tutorial · Quizzes · Python lab