For Loop Examples#

Note

Source: Adapted from the C# edition (for/forexamples.rst). C# format strings are replaced by Python f-strings. The power table, ASCII table, modular multiplication table, and reversed-string examples are direct translations. The enumerate() section is Python-specific with no C# equivalent.

Multiples of k#

Suppose we want to print the first n multiples of k — for example the first 5 multiples of 3: 3, 6, 9, 12, 15.

One approach uses an index 1 through n and multiplies:

n, k = 5, 3
for i in range(1, n + 1):
    print(i * k)

Output:

3
6
9
12
15

Another approach steps directly through the multiples using a custom step:

for i in range(k, n * k + 1, k):
    print(i)

Both produce identical output. The second is natural when the step size is the value you care about.

Compound Assignment Operators#

Python supports the same shorthand assignment operators as C#. For any binary operator op, x op= expr means x = x op expr:

  • x += 5 is the same as x = x + 5

  • x -= 3 is the same as x = x - 3

  • x *= 2 is the same as x = x * 2

  • x //= 4 is the same as x = x // 4 (integer division)

  • x %= 7 is the same as x = x % 7

Tables#

Reports often display data in aligned columns. As a first table, print the square, cube, and square root of integers 1 through 10.

A first attempt without any formatting:

import math

for n in range(1, 11):
    print(n, n**2, n**3, math.sqrt(n))

Output:

1 1 1 1.0
2 4 8 1.4142135623730951
3 9 27 1.7320508075688772
...
10 100 1000 3.1622776601683795

The numbers are correct but the columns are ragged. Python f-strings let us specify field widths and precision. The format spec {value:Nd} right-justifies an integer in a field of width N; {value:N.4f} gives a float with 4 decimal places in a field of width N.

import math

print(f"{'n':>4} {'square':>8} {'cube':>8} {'root':>8}")
for n in range(1, 11):
    print(f"{n:4d} {n**2:8d} {n**3:8d} {math.sqrt(n):8.4f}")

Output:

 n   square     cube     root
 1        1        1   1.0000
 2        4        8   1.4142
 3        9       27   1.7321
 4       16       64   2.0000
 5       25      125   2.2361
 6       36      216   2.4495
 7       49      343   2.6458
 8       64      512   2.8284
 9       81      729   3.0000
10      100     1000   3.1623

The heading uses the same field widths as the data rows so that the columns line up.

ASCII and Character Codes#

Every character has a numeric code. Python’s ord(ch) returns the integer code of character ch; chr(i) does the reverse. Codes 32 through 126 correspond to the printable characters on a US keyboard.

A simple listing, one per line:

for i in range(32, 127):
    print(f"{i:3d} {chr(i)}")

To save space, we can print 8 entries per line. We stay on the same line with print(..., end="  ") and advance to the next line after every 8th entry (when i % 8 == 7):

for i in range(32, 127):
    print(f"{i:3d} {chr(i)}", end="  ")
    if i % 8 == 7:
        print()
print()

The final print() ensures the last (partial) line is terminated.

Modular Multiplication Table#

Modular arithmetic takes remainders after multiplying. For example, 3 × 5 mod 7 = 15 % 7 = 1. We will print the full multiplication table mod 7 — a useful structure in cryptography.

Our target output:

* | 0 1 2 3 4 5 6
-----------------
0 | 0 0 0 0 0 0 0
1 | 0 1 2 3 4 5 6
2 | 0 2 4 6 1 3 5
3 | 0 3 6 2 5 1 4
4 | 0 4 1 5 2 6 3
5 | 0 5 3 1 6 4 2
6 | 0 6 5 4 3 2 1

Start with pseudocode — one row per value of r:

for r in range(7):
    print row r

Each row is itself a repetitive pattern — columns 0 through 6 — so we replace “print row r” with an inner loop:

for r in range(7):
    for c in range(7):
        print (r * c) % 7, stay on same line
    advance to next line

Translating to Python:

for r in range(7):
    for c in range(7):
        print((r * c) % 7, end=" ")
    print()

This gets the body right. Now add the border labels. The heading row prints "* |" once, then the column labels 0–6:

print("* |", end=" ")
for i in range(7):
    print(i, end=" ")
print()
print("-" * 17)

for r in range(7):
    print(f"{r} |", end=" ")
    for c in range(7):
        print((r * c) % 7, end=" ")
    print()

To generalise to mod n, replace 7 with n and compute the column width from the number of digits in n:

def mod_mult_table(n):
    width = len(str(n))
    fmt = f"{{:>{width}}}"          # e.g. "{:>1}" for n=7, "{:>2}" for n=11
    header = "* | " + " ".join(fmt.format(i) for i in range(n))
    print(header)
    print("-" * len(header))
    for r in range(n):
        row = fmt.format(r) + " | "
        row += " ".join(fmt.format((r * c) % n) for c in range(n))
        print(row)
mod_mult_table(7)

Output:

* | 0 1 2 3 4 5 6
-----------------
0 | 0 0 0 0 0 0 0
1 | 0 1 2 3 4 5 6
2 | 0 2 4 6 1 3 5
3 | 0 3 6 2 5 1 4
4 | 0 4 1 5 2 6 3
5 | 0 5 3 1 6 4 2
6 | 0 6 5 4 3 2 1

Reversed String#

To reverse a string we iterate through it backwards and accumulate characters. Start rev as the empty string and append each character:

def reversed_string(s):
    rev = ""
    for i in range(len(s) - 1, -1, -1):
        rev += s[i]
    return rev
print(reversed_string("drab"))

Output:

bard

Trace through "drab" to confirm: the loop visits indices 3, 2, 1, 0, producing characters 'b', 'a', 'r', 'd', which accumulate as "b", "ba", "bar", "bard".

Python also offers a concise built-in idiom for the same result:

s[::-1]   # slice with step -1 reverses the string

Understanding the explicit loop version first makes the slice idiom easier to trust.

Using enumerate()#

Sometimes you need both the position and the value while iterating. Python’s enumerate() provides both without manual index tracking:

words = ["apple", "banana", "cherry"]
for i, word in enumerate(words):
    print(f"{i}: {word}")

Output:

0: apple
1: banana
2: cherry

enumerate() returns pairs (index, item); the for loop unpacks each pair into i and word. An optional second argument sets the starting index:

for i, word in enumerate(words, start=1):
    print(f"{i}. {word}")

Output:

1. apple
2. banana
3. cherry

This is cleaner than the while-loop alternative of maintaining a separate counter variable.