GCSE Python Reference Guide

Python GCSE Guide

Student Reference Guide

A quick cheat-sheet for core Python concepts required for GCSE Computer Science.

01 Sequences

Sequences involve writing code that runs in order, line by line. This includes getting data into the system (Input) and showing data to the user (Output).

# Outputting text to the screen
print("Hello, World!")

# Getting input from the user (always comes in as a string)
name = input("What is your name? ")

# Using the input
print("Nice to meet you, ", name)

02 Arithmetic Operators

Python can perform mathematical calculations. Pay attention to Integer Division (DIV) and Modulus (MOD).

Basic Operators

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division (creates decimal)

Advanced Operators

  • // Integer Division (DIV - whole number only)
  • % Modulus (MOD - remainder only)
  • ** Exponentiation (Power)
num1 = 10
num2 = 3

total = num1 + num2       # Result: 13
div_result = num1 / num2  # Result: 3.333...
whole_div = num1 // num2  # Result: 3 (DIV)
remainder = num1 % num2   # Result: 1 (MOD)

03 Concatenation & Casting

Concatenation is joining strings together. You cannot join a number to a string directly without 'casting' (converting) it first.

first_name = "Ada"
surname = "Lovelace"
age = 36

# Joining strings with +
full_name = first_name + " " + surname

# Casting: Converting an integer (age) to a string (str)
print("She is " + str(age) + " years old.")

# Casting Input: Converting string input to integer
user_age = int(input("Enter your age: "))

04 Selection (If/Elif/Else)

Selection allows the program to make decisions based on conditions. Indentation is critical in Python.

score = 75

if score >= 90:
    print("Grade: 9")
elif score >= 70:
    print("Grade: 7")
elif score >= 50:
    print("Grade: 5")
else:
    print("Fail")

05 Iteration (Loops)

Iteration is repeating code. Use For Loops when you know how many times to loop. Use While Loops when you want to loop until a condition changes.

For Loop (Count-controlled)

# Loops 5 times (0, 1, 2, 3, 4)
for i in range(5):
    print("Loop number:", i)

# Loops from 1 to 10 (stops before 11)
for x in range(1, 11):
    print(x)

While Loop (Condition-controlled)

password = ""

while password != "secret":
    password = input("Enter password: ")
    
print("Access Granted")

06 Relational & Boolean Operators

Used to compare values (Relational) and combine conditions (Boolean Logic).

Symbol Meaning Example
==Equal toif x == 10:
!=Not Equal toif x != 0:
>Greater thanif age > 18:
<Less thanif cost < 50:
age = 15
has_ticket = True

# Using AND (Both must be True)
if age > 12 and has_ticket == True:
    print("Welcome to the movie.")

# Using OR (Only one needs to be True)
day = "Saturday"
if day == "Saturday" or day == "Sunday":
    print("It is the weekend!")

07 Arrays (Lists)

Arrays (called Lists in Python) allow you to store multiple values in a single variable. Counting starts at 0.

# Creating a list
fruits = ["Apple", "Banana", "Orange"]

# Accessing data (Index 0 is Apple)
print(fruits[1])  # Prints 'Banana'

# Adding data
fruits.append("Grape")

# Changing data
fruits[0] = "Pear"

# Iterating through a list
for item in fruits:
    print(item)

08 Subroutines

Subroutines block code together so it can be reused. They can be Procedures (just do a job) or Functions (return a value).

Procedure (with Parameters)

def greet_user(name):
    print("Hello, " + name)

# Main Program
greet_user("Alice")
greet_user("Bob")

Function (Returns Data)

def calculate_area(width, height):
    area = width * height
    return area

# Main Program
room_size = calculate_area(5, 4)
print(room_size)  # Prints 20

© 2024 GCSE Computer Science Resources