Unit 2- programming
Unit 2- programming
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
age = 25 # Integer
name = "Alice" # String
height = 5.8 # Float
is_student = True # Boolean
PI = 3.14159
MAX_SCORE = 100
2. Operators in Python
Arithmetic Operators
name = "Alice"
age = 20
print(name + " is " + str(age) + " years old.") # Concatenation
print(f"{name} is {age} years old.") # f-String Formatting
4. Data Structures
Lists (Arrays)
6. Control Structures
If-Else Statements
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)
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
result = add(5, 3)
print(result) # Output: 8
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!")
Linear Search
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]