TutorSyntax • Free revision resources

GCSE Python Cheat Sheet: Examples, Errors & Practice

Use this page to refresh syntax, then close it and try the questions. In an exam you need to understand what code does, predict output and sometimes write or correct your own program.

Variables, input and conversion

The `input()` function returns text. Convert the result before arithmetic and check whether the user supplied a valid value when reliability matters.

name = input("Your name: ")
age = int(input("Your age: "))
print(name, age + 1)

Selection and Boolean expressions

score = 67
if score >= 70:
    print("Target met")
else:
    print("Keep practising")

`=` assigns a value; `==` checks equality. Use `and` when both conditions must be true, `or` when either may be true, and `not` to reverse a Boolean expression.

Loops and lists

scores = [4, 7, 3, 9]
total = 0
for number in scores:
    total += number
print(total)

A `for` loop is useful when you know the items or range to process. A `while` loop repeats while a condition remains true; ensure the condition can eventually become false. List indexes begin at zero.

Try without notes: What does the loop above print?

Check an example answer and feedback

It prints 23, because 4 + 7 + 3 + 9 = 23.

Functions with parameters and return values

def largest(a, b):
    if a > b:
        return a
    return b

print(largest(12, 18))

A parameter receives data into a function. A returned value can be stored or used elsewhere. Printing inside the function is not the same as returning a value.

Try without notes: Predict the output and explain how return is used.

Check an example answer and feedback

The program prints 18. The function compares both arguments and returns the larger value to the print call.

Reading text files

with open("scores.txt", "r", encoding="utf-8") as file:
    for line in file:
        print(line.strip())

The `with` statement closes the file automatically. The file must exist for read mode, and `strip()` removes the newline from the end of each line. File handling may require exception handling in more robust programs.

A five-minute independent task

  1. Write a list of four test marks.
  2. Use a function to return their arithmetic mean.
  3. Add selection that displays whether the mean is at least 50.
  4. Predict the result before running the program.
  5. Test the code with a different list, including all zeroes.

Continue with Python Foundations, GCSE Python lessons and interactive code tracing.

Continue learning

Choose your examination specification, practise a question and check your answer. Revisit weaker topics after a few days.

Independently written TutorSyntax content; not endorsed by OCR or AQA. Always check your school’s specification and mock coverage.