A loop defines the observations
range(start, stop, step) includes start and excludes stop. range(0, 500, 100) visits 0, 100, 200, 300 and 400. A missing endpoint can change both the sample count and the interpretation of a test.
Week 04 / Describe and decide
Choose when to look, then explain what you actually observed.
Last week we made a decision from one set of inputs. A changing machine gives us a new problem: which moments will our program inspect?
A simulated signal is high from 200 ms up to, but not including, 300 ms. We will inspect it between 0 and 500 ms.
Start with the problem
Draw the high interval on a timeline. Mark samples at 0, 250 and 500 ms, then at 0 and 300 ms. Which schedule notices the event?
A for loop repeats the same observation at selected times. range defines the start, spacing and stopping point, so it also determines what can be missed.
By the end: List the times your program inspected and explain why an empty alarm log does not establish that nothing happened between samples.
range(start, stop, step) includes start and excludes stop. range(0, 500, 100) visits 0, 100, 200, 300 and 400. A missing endpoint can change both the sample count and the interpretation of a test.
Sampling at 0, 250 and 500 ms detects this pulse. Sampling at 0, 300 and 600 ms misses it. Those observations describe two schedules, not two different physical events.
Incrementing t_ms in a notebook does not schedule a real sensor at that interval. Here, a loop explores a mathematical timeline. Real acquisition would also involve timing accuracy, latency and sensor behaviour.
Read the example alongside the explanation. Run it in a new notebook cell and change one input to see how it behaves.
hits = 0
for t_ms in range(0, 501, 100):
high = 200 <= t_ms < 300
print(t_ms, high)
if high:
hits = hits + 1
print('Detected samples:', hits)0 False 100 False 200 True 300 False 400 False 500 False Detected samples: 1
A loop performs repeated updates. The accumulator has a meaning after every iteration: total is the sum of the values processed so far. That meaning explains both its initial value and where its update belongs.
Trace the three samples 4, 7 and 2. Draw one row per iteration with columns current sample, previous total and new total.
Predict before running. What does the program remember if total = 0 is moved inside the loop?
The intended answer is 13. A repeated instruction is useful only when its state update preserves the intended meaning.
Change one thing. Replace addition with count = count + 1. Explain why the result becomes 3 rather than 13. For a nested loop, trace one complete inner loop before the outer loop advances.
Türkçe: Toplam değişkeni işlenen kısmın toplamını tutar. Her turda sıfırlamak önceki veriyi siler; saymak ile toplamak farklıdır.
Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.
t = 0, 100, 200, 300, 400, 500 ms
Question: How many high samples? Is 300 ms inside the pulse?
The pulse excludes 300 ms. Six observations do not reveal the entire continuous signal.
t = 0, 300 ms within the 0–500 ms observation window
Question: Will this schedule detect the pulse?
No detection is not proof of no event. The pulse existed between observations.
t = 50, 250, 450 ms
Question: Can fewer samples still detect this particular pulse?
Phase matters as well as interval. This example does not establish a universal sampling guarantee.
Interactive walkthroughs of for Loops & range -- Repetition. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.
Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.
Something to take away: A time-axis sketch, two loop traces and a carefully limited conclusion about event detection.
Read the notebook's teaching cells before these exercises.
Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.
Notes stay in this browser. Download a copy to keep them.
Ask AI whether zero high samples proves the signal stayed low. Supply the pulse and 300 ms schedule as a counterexample. Check its proposed loop endpoints and its distinction between simulated and real time.
You can also review the supplied example with a partner. Use the same inputs to compare the reasoning. Follow the syllabus rules for assessed work.