๐Ÿ” Coding ยท Ages 9 to 15 ยท 1:06

Python loops: what a for loop does

Why write the same line five times when a loop can repeat it? See for i in range(5) run step by step, watch i count 0 to 4, then print the square numbers and change 5 to 100.

โ–ถ Video ยท 1:06 Python loops: what a for loop does

Straight after watching

Now try it yourself

In this video

  1. 0:00 Say hello five times
  2. 0:13 A for loop
  3. 0:41 Use the counter

Transcript

0:01 Imagine saying hello to five friends. You could write the same line five times.

0:07 Or you could ask the computer to repeat it for you. That's a loop.

0:13 Here it is. For i in range of five, print hello.

0:21 Range of five counts zero, one, two, three, four. Five numbers.

0:28 Each time round, i takes the next number, and the indented line runs once.

0:35 Round and round it goes. Five hellos, from just two lines.

0:41 Now let's use i itself. Print i times i.

0:46 Zero, one, four, nine, sixteen. The square numbers, made by the loop.

0:55 Change five to a hundred, and you get a hundred square numbers, just like that.

1:01 A loop repeats your code, so you don't have to.

More videos

Watch next

All videos โ†’
Chat on WhatsApp
Esc

Try