Python #4: For Loops, Range, and Iteration

Python programming for loops range and iteration, realistic comic-book style technology illustration of a programmer at workstation with code editor and repeating data-flow loops across screens, grounded professional environment, detailed realistic proportions, no text, landscape news cover composition, polished realistic editorial quality

Python #4 introduces for loops, range(), and iteration. Loops let a program repeat an operation across a collection of data instead of writing the same instruction again and again. That pattern appears everywhere from Bitcoin mining telemetry to games, sports statistics, and entertainment applications.

Loop Through Bitcoin Miners

miners = ["S21", "S21 Pro", "S19 XP"]

for miner in miners:
    print(miner)

On each pass through the loop, miner refers to the next item in the list. A monitoring script could use the same pattern to process a fleet of ASIC miners.

Use range()

for rack_number in range(1, 6):
    print("Checking rack", rack_number)

range(1, 6) produces the integers 1 through 5. The ending value is not included.

Gaming Example: Inventory

inventory = ["pickaxe", "battery", "cooling kit", "ASIC miner"]

for item in inventory:
    print("Inventory item:", item)

A game can iterate through inventory, characters, buildings, map tiles, missions, or other collections using the same basic loop.

Sports Example: Total a Score

quarter_scores = [7, 10, 3, 14]
total = 0

for points in quarter_scores:
    total += points

print("Final score:", total)

This loop accumulates points from four quarters. The same technique can total statistics, mining rewards, game resources, or other numeric data.

Loop Through the Dictionary from Python #3

miner = {
    "model": "S21",
    "hashrate_th": 200,
    "status": "online"
}

for key, value in miner.items():
    print(key, value)

Python #3 introduced dictionaries. The items() method lets a loop receive each key and its associated value. This connects dictionaries directly to iteration.

Nested Loops: Mining Racks

for rack in range(1, 3):
    for miner_slot in range(1, 4):
        print("Rack", rack, "Miner", miner_slot)

A nested loop runs one loop inside another. This simplified example visits three miner positions on each of two racks. Similar patterns can traverse game grids, tournament data, seating layouts, or equipment groups.

break and continue

temperatures = [62, 65, 71, 96, 68]

for temperature in temperatures:
    if temperature >= 90:
        print("High-temperature alert:", temperature)
        break

break exits the loop immediately. continue skips the remainder of the current iteration and proceeds to the next one. Both are useful, but straightforward loops are usually easier to read when special control flow is unnecessary.

Video Lesson

Programming with Mosh’s Python Full Course for Beginners includes dedicated chapters on for loops, for-else, nested loops, iterables, and while loops. The for-loop section begins around 1:24:18.

Practice

  1. Create a list containing four ASIC miner models and print each one with a for loop.
  2. Use range() to print rack numbers 1 through 10.
  3. Create a game inventory and loop through every item.
  4. Total four sports scoring periods using a loop.
  5. Create a miner dictionary and print every key-value pair with items().

Key Takeaway

A for loop processes items from an iterable one at a time. range() supplies integer sequences, nested loops handle multidimensional repetition, and dictionary iteration connects directly to Python #3.

Leave a comment