Skip to content

Programming Constructs

A variable is a named storage location in memory that holds a value which can change during Program execution.

TypeTypical SizeRangePython equivalent
Integer4 bytes[231,2311][-2^{31}, 2^{31}-1]int (unbounded)
Float8 bytesIEEE 754 double precisionfloat
Character1 byteASCII/Unicodestr (length 1)
Boolean1 byteTrue / Falsebool

A constant is a named value that cannot be changed after initialisation.

MAX_SIZE = 100
PI = 3.14159265

Converting a value from one type to another:

x = int(3.7) # x = 3 (truncation)
y = float(5) # y = 5.0
z = str(42) # z = "42"

if condition:
statement_block
elif another_condition:
alternative_block
else:
default_block

Selection statements can be nested, but excessive nesting reduces readability. Use guard clauses or Early returns where possible.

def classify_grade(score):
if score < 0 or score > 100:
return "Invalid"
if score >= 70:
return "A"
if score >= 60:
return "B"
if score >= 50:
return "C"
return "Fail"

Some languages (not Python without match/case in 3.10+) provide switch statements for multi-way Branching.


Executes a fixed number of times.

for i in range(n):
print(i)

Invariant for for i in range(n): At the start of iteration iiThe loop body has executed Exactly ii times.

Executes until a condition becomes false.

while condition:
statement_block

A loop invariant is a property that holds before and after each iteration.

Example: Sum of first nn natural numbers.

def sum_n(n):
total = 0
i = 1
while i <= n:
total += i
i += 1
return total

Invariant: At the start of each iteration, total = 1 + 2 + ... + (i - 1).

Proof:

  • Init: Before the first iteration, i=1i = 1total = 0. Sum of empty set = 0. ✓
  • Maintenance: total increases by iiThen ii increases by 1. After: total = 1 + ... + i and next i"=i+1i" = i + 1So invariant holds.
  • Termination: i=n+1i = n + 1. total = 1 + 2 + ... + n = n(n+1)/2. ✓

Executes the body at least once, then checks the condition. Python does not have a native do-while; Emulate with:

while True:
statement_block
if not condition:
break

A recursive function is one that calls itself. Every recursive function must have:

  1. Base case(s): Direct answer(s) for the simplest input(s)
  2. Recursive case: Reduce the problem and call itself on the smaller input
def factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)

Theorem. factorial(n) returns n!n! for all n0n \geq 0.

Proof. By induction on nn.

Base case. n=0n = 0: returns 1 = 0!0!. ✓

Inductive step. Assume factorial(k) = k! for all knk \leq n. Then:

factorial(n+1)=(n+1)×factorial(n)=(n+1)×n!=(n+1)!\mathrm{factorial}(n+1) = (n+1) \times \mathrm{factorial}(n) = (n+1) \times n! = (n+1)!

\square

Theorem. factorial(n) terminates for all n0n \geq 0.

Proof. Define a variant function V(n)=nV(n) = n. Each recursive call decreases VV by 1: V(n1)=n1<n=V(n)V(n-1) = n - 1 \lt n = V(n). VV is a non-negative integer that strictly decreases. By the Well-ordering principle, VV must eventually reach a base case (V=0V = 0 or V=1V = 1). Therefore, the Recursion terminates. \square

def fib(n):
if n <= 1:
return n
return fib(n - 1) + fib(n - 2)

Complexity: T(n)=T(n1)+T(n2)+O(1)T(n) = T(n-1) + T(n-2) + O(1). This gives T(n)=Θ(ϕn)T(n) = \Theta(\phi^n) where ϕ=1+521.618\phi = \frac{1+\sqrt{5}}{2} \approx 1.618 (the golden ratio).

Proof sketch. The recurrence has characteristic equation r2=r+1r^2 = r + 1Giving roots ϕ\phi and ψ=152\psi = \frac{1-\sqrt{5}}{2}. The solution is T(n)=Aϕn+BψnT(n) = A\phi^n + B\psi^n. Since ψ<1|\psi| \lt 1 T(n)=Θ(ϕn)T(n) = \Theta(\phi^n). \square