Guide
From Zero to Exam-Ready: Core Concepts, Worked
Examples & Practice Questions
Original study material. Covers: data types, control flow, functions, OOP, file handling, errors,
modules, and exam-style practice with answers.
Part 1 — Foundations
1. Variables and Data Types
Python is dynamically typed: you never declare a type; the interpreter infers it.
name = "Ada" # str
age = 36 # int
pi = 3.14159 # float
is_valid = True # bool
nothing = None # NoneType
Key exam facts:
• int has arbitrary precision (no overflow like C/Java).
• float follows IEEE-754 double precision — 0.1 + 0.2 != 0.3 exactly.
• str is immutable; every "modification" creates a new string.
• None is a singleton; always test with is None, never == None.
Type conversion (casting)
int("42") # 42
float("3.5") # 3.5
str(42) # "42"
bool(0) # False — 0, "", [], {}, None are all falsy
2. Strings in Depth
s = "Python"
s[0] # 'P' (indexing)
s[-1] # 'n' (negative index = from the end)
s[1:4] # 'yth' (slice: start inclusive, end exclusive)
s[::-1] # 'nohtyP' (reverse idiom — favourite exam question)
Essential methods: upper(), lower(), strip(), split(","), join(list), replace(a, b), startswi
Page 1/7
, Python Programming - Complete Study Guide
th(), find() (returns -1 if missing; index() raises).
f-strings (Python 3.6+):
name, score = "Ada", 97.5
f"{name} scored {score:.1f}" # 'Ada scored 97.5'
3. Collections
Type Literal Mutable? Ordered? Duplicates?
list `[1, 2]` yes yes yes
tuple `(1, 2)` no yes yes
set `{1, 2}` yes no no
dict `{"a": 1}` yes insertion-ordered (3.7+)
keys unique
nums = [3, 1, 4, 1, 5]
nums.append(9); nums.sort(); nums.pop() # list idioms
t = (1, 2) # tuple unpacking:
a, b = t
s = {1, 2, 3}; s.add(4); s | {5} # set union
d = {"a": 1}; d.get("z", 0) # safe lookup with default
List comprehensions (very common in exams):
squares = [x**2 for x in range(10) if x % 2 == 0]
# dict comprehension:
word_len = {w: len(w) for w in ["hi", "hello"]}
Exam traps:
• list * n repeats references: [[0]] * 3 shares one inner list — use [ [0] for _ in
range(3) ].
• Default mutable arguments: def f(x=[]) keeps ONE list across calls — use x=None.
4. Control Flow
for i in range(5): # 0..4
if i % 2: continue
print(i)
n = 0
while n < 3:
n += 1
else: # runs if loop wasn't broken
print("done")
range(start, stop, step) — stop is exclusive. enumerate() gives index+value; zip() pairs
iterables (stops at shortest).
Page 2/7