E ExamMaster

Engineering Python · Data & Control

Loops: for and while

In Engineering Python because labs repeat over rows and counters — for, while and range are how you accumulate results.

`for` walks an iterable; `while` repeats while a condition stays True; `range` builds integer sequences for counted loops.

  • Engineering Python
  • Easy level
  • 4 concepts

1for and range

`for i in range(n):` runs the body with i = 0 .. n-1. `for x in sequence:` binds x to each element.

Figure. for i in range(n) runs the body with i = 0 through n-1. range(4) yields 0, 1, 2, 3.

Accumulate

total = 0
for i in range(4):
    total += i
print(total)
How many times does `for i in range(3):` run its body?
  1. 3
  2. 2
  3. 4

range(3) yields 0, 1, 2.

2while loops

`while condition:` repeats until the condition is False. Ensure the condition eventually fails or you loop forever.

Figure. while condition repeats until the condition is False. n starts at 3 and decreases by 1 each time.

Takeaway

  1. Idea`while condition:` repeats until the condition is False. Ensure the condition eventually fails or you loop forever.

Countdown

n = 3
while n > 0:
    print(n)
    n -= 1
What risk arises if the condition of a while loop never evaluates to False inside its body?
  1. A syntax error thrown before execution begins
  2. An infinite loop that runs indefinitely until interrupted or process memory runs out
  3. The loop terminates immediately on the first iteration
  4. The loop converts itself automatically into a for loop

A while loop continues as long as its condition remains True. If the state never changes, it becomes an infinite loop.

3Accumulation patterns

Common pattern: start an accumulator, update it each iteration, print once after the loop.

Figure. Common pattern: start an accumulator, update it each iteration, print once after the loop.

Takeaway

  1. IdeaCommon pattern: start an accumulator, update it each iteration, print once after the loop.

4Lab: accumulate in a loop

Sum a small range and print the total.

Figure. Sum i for i in range(5) and print the total. 0+1+2+3+4 = 10.

Takeaway

  1. IdeaSum a small range and print the total.

Coding lab. Sum with a for-loop runs in the app, with checks on your output.

Notes

  • In Engineering Python because labs repeat over rows and counters — for, while and range are how you accumulate results.
  • `for i in range(n):` runs the body with i = 0 .. n-1. `for x in sequence:` binds x to each element.
  • `while condition:` repeats until the condition is False. Ensure the condition eventually fails or you loop forever.

Exam traps & shortcuts

  • Run one cell at a time and read stdout before changing more lines.
  • Names are labels for values; rebinding a name does not rewrite old prints.

Recap

for walks iterables; while repeats on a condition; accumulate then print.

for and range
`for i in range(n):` runs the body with i = 0 .. n-1. `for x in sequence:` binds x to each element.
while loops
`while condition:` repeats until the condition is False. Ensure the condition eventually fails or you loop forever.
Accumulation patterns
Common pattern: start an accumulator, update it each iteration, print once after the loop.
Lab: accumulate in a loop
Sum a small range and print the total.

Practise Loops: for and while

Reading is free and needs no account. Practice, mocks and progress live in the app.

  • A 2-question practice set that ends the chapter
  • 2 quick checks with worked explanations
  • Timed mocks scored with the real marking scheme
  • Readiness tracked per topic, kept on your device
Continue with Google — freeNo card, no trial. Works offline once installed.