■ Python A Level 9618 (Paper 4) – Full Revision
Guide
■ Section 1: Python Basics
■ Input & Output
name = input("Enter name: ")
age = int(input("Enter age: "))
print("Hello", name, "you are", age)
■ Variables & Data Types
x = 10 # int
y = 3.14 # float
z = "hello" # str
flag = True # bool
■ Operators
Arithmetic: + - * / // % **
Comparison: == != < > <= >=
Logical: and or not
■ Selection
if score >= 90:
print("A")
elif score >= 50:
print("Pass")
else:
print("Fail")
■ Iteration
for i in range(5): # 0–4
print(i)
while i < 10:
i += 1
■ Lists & Strings
nums = [10, 20, 30]
nums.append(40)
print(nums[0]) # 10
word = "Python"
print(len(word)) # 6
print(word.upper()) # PYTHON
■ Subroutines
def greet(name): # procedure
print("Hi", name)
def square(x): # function
return x * x
■ File Handling
file = open("data.txt", "w")
file.write("Hello\n")
file.close()
file = open("data.txt", "r")
for line in file:
print(line.strip())
file.close()
■ 2D Lists (Arrays)
grid = [[0 for c in range(3)] for r in range(3)]
grid[0][1] = 5
■ Section 2: Algorithms
■ Linear Search
def linear_search(arr, item):
for i in range(len(arr)):
if arr[i] == item:
return i
return -1
■ Binary Search
def binary_search(arr, item):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == item:
return mid
elif arr[mid] < item:
low = mid + 1
else:
high = mid - 1
return -1
■ Bubble Sort
def bubble_sort(arr):
for i in range(len(arr)):
for j in range(len(arr)-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
■ Insertion Sort
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and arr[j] > key:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
■■ Section 3: Common Mistakes & Tips
• Always convert input to int or float if doing calculations (e.g., int(input())).
• Remember Python indexes start at 0, not 1.
• Use == for comparison, not = (assignment).
• Watch out for off-by-one errors in loops with range().
• Close files after reading/writing, or use 'with open(...) as f'.
• Check indentation carefully – Python is sensitive to it!
• Use return in functions if a value is needed; otherwise it returns None.
• In bubble sort, inner loop runs up to len(arr)-1, not len(arr).
■ Section 4: Practice Questions (with solutions)
■ Q1: Read 5 numbers from input and print the average
nums = []
for i in range(5):
n = int(input("Enter number: "))
nums.append(n)
print("Average =", sum(nums)/len(nums))
■ Q2: Store student names and marks in a 2D list, then print them
students = []
for i in range(3):
name = input("Enter name: ")
mark = int(input("Enter mark: "))
students.append([name, mark])
for s in students:
print(s[0], "got", s[1])
■ Q3: Bubble sort a list of numbers
arr = [5,2,9,1,7]
bubble_sort(arr)
print("Sorted:", arr)
■ Q4: Linear search for a name in a list
names = ["Ali", "Sara", "John"]
target = input("Search name: ")
pos = linear_search(names, target)
if pos != -1:
print("Found at index", pos)
else:
print("Not found")
■ Q5: Write to a file and then read from it
file = open("students.txt", "w")
file.write("Ali 90\nSara 85\n")
file.close()
file = open("students.txt", "r")
for line in file:
print(line.strip())
file.close()