Indentation and blocks
Python uses indentation (spaces at the start of a line) to group statements into a block. A block starts after a line ending with a colon (if, else, for, while) and all its lines must be indented equally (4 spaces is the standard). Wrong indentation gives an IndentationError.
if marks >= 33:
print('Pass') # inside if
print('Done') # outside, always runs
Selection: if, if-else, if-elif-else
if n > 0:
print('positive')
if n % 2 == 0:
print('even')
else:
print('odd')
if m >= 90:
g = 'A'
elif m >= 75:
g = 'B'
elif m >= 50:
g = 'C'
else:
g = 'D'In if-elif-else, only the first true branch runs; the rest are skipped. else is optional and has no condition.
The for loop and range()
for repeats a block once for each item in a sequence.
range(5)→ 0, 1, 2, 3, 4range(2, 7)→ 2, 3, 4, 5, 6range(1, 10, 3)→ 1, 4, 7range(10, 0, -2)→ 10, 8, 6, 4, 2
The stop value is never included. for ch in 'cat': loops over each character.
The while loop
while repeats as long as its condition is true. Something inside must change the condition, or it loops forever (infinite loop).
n = 1
while n <= 5:
print(n)
n += 1Use for when you know how many times to repeat; use while when you repeat until something happens.
break and continue
break ends the loop immediately; the program goes to the first line after the loop.
continue skips the rest of the current turn and goes to the next turn.
for n in range(1, 11):
if n == 8:
break
if n % 3 == 0:
continue
print(n) # 1 2 4 5 7A loop may have an else part that runs only if the loop ended without break.
Nested loops and pattern programs
A loop inside another loop is a nested loop. For each turn of the outer loop, the inner loop runs completely.
for i in range(1, 5):
for j in range(1, i + 1):
print('*', end=' ')
print()Output: * / * * / * * * / * * * * (one row per line). Change what is printed to get number (j) or letter (chr(64 + j)) patterns.
Series programs
A series sum uses an accumulator that starts at 0 (or 1 for products) and adds a term each turn.
# 1 + 1/2 + 1/3 + ... + 1/n
s = 0
for i in range(1, n + 1):
s += 1 / i
# 1 - x + x**2 - x**3 ... (n terms)
s = 0
for i in range(n):
s += (-1) ** i * x ** iOther practical programs: factorial, Fibonacci, prime check, Armstrong, perfect and palindrome numbers, GCD/LCM.
Key formulas and definitions
- range(start, stop, step): stop not included
- if … elif … else: only the first true branch runs
- break: leave loop · continue: next turn
- Accumulator: s = 0; s += term
- Nested loop runs inner loop fully for each outer turn
Worked examples
1. Write a program to print the larger of two numbers.
a = int(input()); b = int(input()) if a > b: print(a) else: print(b)
2. What does for i in range(10, 0, -3): print(i, end=' ') print?
10 7 4 1
3. Find the sum 1 + 2 + … + n using while.
n = int(input()); s = 0; i = 1 while i <= n: s += i i += 1 print(s) # n = 10 gives 55
4. Check whether a number is prime.
n = int(input()) if n < 2: print('Not prime') else: for d in range(2, n): if n % d == 0: print('Not prime'); break else: print('Prime')
5. Print the first 8 Fibonacci numbers.
a, b = 0, 1 for _ in range(8): print(a, end=' ') a, b = b, a + b # 0 1 1 2 3 5 8 13
6. Print this number pattern for 4 rows: 1 / 1 2 / 1 2 3 / 1 2 3 4.
for i in range(1, 5): for j in range(1, i + 1): print(j, end=' ') print()
7. Find the sum of 1 + x + x² + … + xⁿ for x = 2, n = 4.
s = 0 for i in range(0, 5): s += 2 ** i # 1 + 2 + 4 + 8 + 16 = 31
8. Check if 153 is an Armstrong number.
n = 153; s = 0; t = n while t > 0: d = t % 10; s += d ** 3; t //= 10 print(s == n) # 1 + 125 + 27 = 153 → True
Common mistakes
- Forgetting the colon after if/for/while, or mixing tabs and spaces.
- Expecting range(1, 5) to include 5.
- Forgetting to change the variable inside while, causing an infinite loop.
- Using several separate ifs when only one branch should run; use elif.