Loops, Functions, Modularization and Strings
Construct Python solutions that combine substring manipulation, repetition, functions, modularization and scope.
What you need to be able to do
- construct programs that extract and manipulate substrings;
- choose and construct counted and conditional loops appropriately;
- use Boolean or relational conditions within loops;
- construct reusable functions with inputs and return values;
- explain how modularization and local/global scope support maintainable programs.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
Extract and manipulate substrings
code = "IBCS-HL-2027"
subject = code[0:4]
level = code[5:7]
year = code[8:12]
display = subject + " " + level + " " + year
updated = code.replace("HL", "SL")display statement uses concatenation to join string parts, while replace() alters part of the original string.Trace, then construct
2027 and one expression that produces "IBCS/HL/2027".Trace: subject = "IBCS", level = "HL", year = "2027", display = "IBCS HL 2027", updated = "IBCS-SL-2027".
Possible constructions:
code[8:12]
code.replace("-", "/")
Counted or conditional?
Counted loop
for i in range(5):
print(i)Use when the repetitions are known or can be generated from a count/range.
Conditional loop
attempts = 0
while attempts < 3:
print("Try")
attempts += 1Use when repetition continues while a condition remains True.
Choose the appropriate loop
Scenario A processes exactly 30 sensor readings. Scenario B repeatedly asks for a password until it is correct or three attempts have been used.
A: a counted loop is natural because exactly 30 readings must be processed. B: a conditional loop is natural because repetition depends on whether the password is still incorrect and attempts remain, for example while not correct and attempts < 3.
Selection inside repetition
scores = [72, 41, 88, 59, 95]
passes = 0
for score in scores:
if score >= 60 and score <= 100:
passes += 1
print(passes)Trace the loop
State the value of passes after each score is processed and the final printed output.
and.After 72 → 1; after 41 → 1; after 88 → 2; after 59 → 2; after 95 → 3. Final output: 3.
Modularize a repeated task
Construct a reusable function
Write percentage(part, whole) so it returns 0 when whole == 0; otherwise it returns the percentage value.
def percentage(part, whole):
if whole == 0:
return 0
return (part / whole) * 100Trace local and global variables
tax_rate = 0.08
def total_with_tax(price):
tax = price * tax_rate
total = price + tax
return total
answer = total_with_tax(50)
print(answer)Trace the call
Globals: tax_rate = 0.08 and, after the call, answer = 54.0. Locals: price = 50, tax = 4.0, total = 54.0. The program prints 54.0.
Combine strings, loops and functions
codes = ["IBCS-HL-2027", "IBDT-SL-2027", "IBCS-SL-2027"], return how many codes begin with the substring "IBCS". Do not use count().Construct count_ibcs(codes)
code[0:4] with "IBCS".def count_ibcs(codes):
count = 0
for code in codes:
if code[0:4] == "IBCS":
count += 1
return countFor the given data, the function returns 2.
Finish the learning cycle
IA — main task
Refine IA Criterion B/C planning so repeated tasks are decomposed into clear modules or functions where appropriate. Do not force modularization where it does not fit the solution.
Syllabus — short retrieval
Redo the integrated function on page 9 without Reveal, then change it so it counts codes ending in "2027".