Skip to content
Tactiverge

Learning Rooms/△ Level 2 · Builder/Computing and algorithms

Python Tutorial for Beginners 7: Loops and Iterations - For/While Loops

Corey Schafer · Length: see YouTube · captions on YouTube · nothing loads until you press play

By the end you can…

  • Store readings in a list and loop over them with for.
  • Use a running total and a count inside a loop to compute a mean.
  • Guard against an empty list before dividing.
?Predict▶Watch✓Practise⚙Build

Watch it all: no segment has been checked by hand for this video yet.

1.Predict before you press play

total = 0.0, then for r in [2.0, 4.0, 6.0]: total = total + r. What is total after the loop?

Pick one before you press play. A wrong guess is fine: it is what makes the video stick.

2.See the idea first

Play with this diagram for a minute. It gives you something to hang the video on.

Loading the interactive…

Can't use the diagram? Read a description instead

The loop visualiser shows the readings 41.2, 42.0, 43.5, 47.9, 52.3, 49.0, 44.1 °C as bars with a 50 °C limit line. Each step runs one loop pass and updates count, total, hottest, coldest and over. After 7 passes it prints mean 45.714285714285715, max 52.3, min 41.2, over 1. For the empty log the loop never runs and the program prints 'no readings'.

3.Watch, actively

Shows for loops over lists, while loops, break and continue: the tools for working through a log of readings.

Length: see YouTube · captions on YouTube · source

Nothing loads from YouTube until you press play.

Watch for…

  • → Watch how a for loop takes each item of a list in turn, and when Corey reaches for while instead.

Pause whenever you like. Rewatching a hard minute is a good habit, not a failure.

Can't stream it? Read the idea instead

A for loop runs the same steps once for each item in a list, such as every reading in a sensor log. A while loop repeats until a condition becomes false. Break leaves a loop early and continue skips to the next item; a while loop whose condition never changes runs for ever.

What the video covers (our own note, written when we verified it): Shows for loops over lists, while loops, break and continue: the tools for working through a log of readings.

Written by Tactiverge in our own words, not a transcript. The full lesson below explains the idea without the video.

Read it instead

Jot down one thing that surprised you and one question you still have.

Private: saved only on this device, never sent to Tactiverge.

4.Practise what you just saw

These come from the lesson that use this video and are checked on the server. Wrong answers get a hint and another try.

Practice 1

What happens with total / len(readings) when readings is []?

Practice 2

In a loop that totals readings, where should print(total) go to print the total once?

Practice 3

Readings 20, 22, 24 and 26 °C. What is the mean, in °C?

°C

Practice 4 · a fresh version every time

A loop totals and counts these temperature readings (°C): [18.0, 25.4, 31.8, 31.6, 21.9, 26.6]. What is total / count, in °C?

°C

5.Try it yourself

No simulation models this idea yet. The lesson below has an interactive diagram instead.

6.Where engineers use it

Condition-monitoring systems compute exactly these summaries over each shift's data to decide whether a motor or bearing is running hotter than usual and needs inspection.

From the applied view in the lesson for loops: summarise a sensor log. Checked by automated tests only.

7.Build something with it

Follow-up project

Write a program for the 'Motor temperature log' that prints the mean, the count and how many readings are above 50 °C. Test it on an empty list and on a list of one reading, and make sure neither crashes.

Where this video lives

In the course catalogue: CSE-210 Python for engineers

The video is by Corey Schafer, embedded from YouTube with privacy-enhanced mode and never autoplayed. The room around it (prompts, notes, practice, projects) is Tactiverge's, written with AI assistance and checked by automated tests only.