๐ŸŒ€ SGCA Year 5 โ€” Coding Reference Sheet

Syntax Station ยท Loops & Variables ยท Python 3

Canon Authority: Katarzyna Kalina vel Kalinowska

v3.0 MASTER CANON LOCKED

๐Ÿ”„ FOR Loops โ€” range()
Basic range:
for i in range(5): # 0,1,2,3,4 print(i)
With start and stop:
for i in range(1, 6): # 1,2,3,4,5 print(i)
With step:
for i in range(0, 10, 2): # 0,2,4,6,8 print(i)
Countdown:
for i in range(10, 0, -1): # 10,9..1 print(i)
๐Ÿ’ก range(stop) = range(0, stop, 1)
๐Ÿ“Š Accumulators
Sum accumulator:
total = 0 # start at 0 for i in range(1, 6): total = total + i print(total) # 15
Product accumulator:
result = 1 # start at 1! for i in range(1, 6): result = result * i print(result) # 120 (5!)
Counter:
count = 0 for i in range(1, 11): if i % 2 == 0: count += 1 print(count) # 5 (even numbers)
๐Ÿ’ก For addition: start=0. For multiplication: start=1
โณ WHILE Loops
Basic WHILE:
x = 1 while x <= 5: print(x) x += 1 # MUST update!
User input loop:
answer = "" while answer != "quit": answer = input("Enter: ") print("You said:", answer)
Doubling:
x = 1 while x <= 100: x = x * 2 print(x) # 128
โš ๏ธ Always update the variable โ€” or infinite loop!
โšก break and continue
break โ€” exit loop:
for i in range(10): if i == 5: break # stop at 5 print(i) # prints: 0 1 2 3 4
continue โ€” skip iteration:
for i in range(10): if i % 2 == 0: continue # skip evens print(i) # prints: 1 3 5 7 9
KeywordEffect
breakExit loop completely
continueSkip to next iteration
๐Ÿ“‹ Lists
Create and access:
planets = ['Earth', 'Mars', 'Venus'] planets[0] # 'Earth' (first) planets[-1] # 'Venus' (last) len(planets) # 3
List methods:
planets.append('Jupiter') # add end planets.insert(1, 'Moon') # add at 1 planets.remove('Mars') # remove item planets.sort() # sort A-Z planets.reverse() # reverse planets.count('Earth') # count item planets.index('Venus') # find index
Loop through list:
for planet in planets: print(planet) for i, planet in enumerate(planets, 1): print(i, planet)
โœ‚๏ธ List Slicing
nums = [10,20,30,40,50,60,70]
SliceResult
nums[2:5][30, 40, 50]
nums[:3][10, 20, 30]
nums[4:][50, 60, 70]
nums[::2][10, 30, 50, 70]
nums[::-1][70, 60, 50, 40, 30, 20, 10]
nums[1:6:2][20, 40, 60]
๐Ÿ’ก list[start:stop:step] โ€” stop is NOT included!
Useful functions:
sum(nums) # total of all items max(nums) # largest item min(nums) # smallest item sorted(nums) # returns sorted copy
๐Ÿ”ฒ Nested Loops
Basic nested loop:
for row in range(3): for col in range(3): print(row, col) # 9 total iterations (3ร—3)
Star grid:
for row in range(5): for col in range(5): print("*", end=" ") print() # new line
Multiplication table:
for i in range(1, 6): for j in range(1, 6): print(i*j, end="\t") print()
๐Ÿ’ก Total iterations = outer count ร— inner count
๐Ÿ”— Loops + Lists Together
Build list with loop:
squares = [] for i in range(1, 6): squares.append(i * i) # [1, 4, 9, 16, 25]
Filter list:
scores = [85, 45, 92, 67, 38] high = [] for s in scores: if s >= 70: high.append(s) # [85, 92, 67]
List comprehension (advanced):
squares = [i*i for i in range(1,6)] high = [s for s in scores if s>=70]
๐Ÿ’ก Always initialise list before loop: mylist = []
๐Ÿ† XP Ranks & Worlds
RankXPIcon
Cadet0๐ŸŒฑ
Explorer500๐Ÿ”ญ
Navigator1,500๐Ÿงญ
Captain3,000โšก
Admiral6,000๐Ÿ‘‘
WorldYearTheme
๐ŸŒ Syntax World3Sequences
๐Ÿ”ฎ Algorithm Alley4Logic
๐ŸŒ€ Syntax Station5Loops
๐Ÿœ๏ธ Debugging Dunes6Debugging
๐Ÿ”’ Canon Authority: Katarzyna Kalina vel Kalinowska