Unit 2: Programming - Review Notes (Python Focus)
This review is tailored specifically for Python programming, based on Unit 2: Programming
from the Pearson Edexcel International GCSE Computer Science syllabus, including
concepts tested in Paper 2: Application of Computational Thinking.
1. Developing Code
Variables and Constants
Variables store data that can change during program execution.
age = 25 # Integer
name = "Alice" # String
height = 5.8 # Float
is_student = True # Boolean
Constants (by convention) are written in uppercase.
PI = 3.14159
MAX_SCORE = 100
2. Operators in Python
Arithmetic Operators
Operator Description Example
+ Addition 5 + 3→8
- Subtraction 10 - 4 → 6
* Multiplication 7 * 2 → 14
/ Division (float) 9 / 2 → 4.5
// Floor Division 9 // 2 → 4
% Modulus (remainder) 9 % 2→1
** Exponentiation 2 ** 3 → 8
3. String Handling
Concatenation & Formatting
name = "Alice"
age = 20
print(name + " is " + str(age) + " years old.") # Concatenation
print(f"{name} is {age} years old.") # f-String Formatting
Common String Methods
text = "hello world"
print(text.upper()) # "HELLO WORLD"
print(text.lower()) # "hello world"
print(text.capitalize()) # "Hello world"
print(len(text)) # 11
print(text.replace("hello", "hi")) # "hi world"
4. Data Structures
Lists (Arrays)
Definition & Manipulation
numbers = [10, 20, 30, 40]
numbers.append(50) # [10, 20, 30, 40, 50]
numbers.remove(20) # [10, 30, 40, 50]
numbers.sort() # Sort in ascending order
print(numbers[1]) # Access second element
Dictionaries (Key-Value Pairs)
student = {"name": "Alice", "age": 20, "grade": "A"}
print(student["name"]) # Alice
student["age"] = 21 # Modify value
student["city"] = "London" # Add new key
5. Input and Output
Taking User Input
name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Convert to integer
Printing Output
print("Hello, " + name + "!")
print(f"You are {age} years old.")
6. Control Structures
If-Else Statements
num = int(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
7. Loops
For Loop
for i in range(5):
print(i) # Output: 0 1 2 3 4
While Loop
count = 0
while count < 5:
print(count)
count += 1
8. Subprograms (Functions)
Defining and Calling Functions
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
Functions with Return Values
def add(x, y):
return x + y
result = add(5, 3)
print(result) # Output: 8
9. Error Handling & Debugging
Types of Errors
Type Description
Syntax Error Mistake in code structure (e.g., missing :).
Logic Error Program runs but gives wrong output.
Runtime Error Program crashes (e.g., division by zero).
Try-Except Handling
try:
num = int(input("Enter a number: "))
print(10 / num)
except ZeroDivisionError:
print("Cannot divide by zero!")
except ValueError:
print("Invalid input!")
10. Algorithms and Problem Solving
Searching Algorithms
Linear Search
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1
Binary Search (Only for Sorted Lists)
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
Sorting Algorithms
Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]