๐ 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.
Straight after watching
Now try it yourself
In this video
- 0:00 Say hello five times
- 0:13 A for loop
- 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
0:51๐ฌ About LearnFun ยท All agesLearnFun in 50 secondsA calm tour: maths, coding, the Rubik's Cube, games and live classes.
1:31๐ Geometry ยท Ages 9 to 14Why do a triangle's angles add up to 180ยฐ?Tear off the corners and they make a straight line, every time.
1:30๐ช Geometry ยท Ages 12 to 15Pythagoras: why aยฒ + bยฒ = cยฒCount the squares, then prove it by sliding four triangles.