WEBVTT

0
00:00.290 --> 00:08.450
Now, up to this point, we've only been using for loops in association with lists, so we've been looping

1
00:08.450 --> 00:13.790
through the list and getting hold of each of the items in the list and then doing something with it,

2
00:13.790 --> 00:14.390
right?

3
00:14.390 --> 00:19.040
But we're not always going to be working with lists and using for loops.

4
00:19.220 --> 00:24.230
Sometimes we might want to use a loop completely independent of a list.

5
00:24.230 --> 00:31.260
And a good example of this is when Carl Gauss, the German mathematician, was just a child.

6
00:31.260 --> 00:36.510
When he was ten years old, his math teacher gave him an exercise that she probably thought would tie

7
00:36.510 --> 00:43.950
him up for a little while, and the idea was to get him to add all of the numbers from 1 to 100.

8
00:43.950 --> 00:49.920
So 1 + 2 +3 + 4 + 5 et cetera, all the way until 100.

9
00:50.040 --> 00:54.810
And even though she thought this would keep him busy for an hour or so, getting this young child to

10
00:54.810 --> 00:59.180
add up these numbers and I can have some peace and quiet scrolling on Facebook,

11
00:59.180 --> 01:03.800
but unfortunately, he came back to her within two minutes and gave her the answer.

12
01:04.370 --> 01:06.230
So how did he work it out?

13
01:06.260 --> 01:11.930
Well, he is actually a really smart kid, and he figured out that if you flip the numbers around.

14
01:11.930 --> 01:20.360
So 100 plus 99 plus 98, and you look at both of these two lines, you can see that 1 + 100 = 101

15
01:20.360 --> 01:21.250
,

16
01:21.250 --> 01:23.380
2 + 99 = 101,

17
01:23.380 --> 01:25.570
3 + 98 = 101.

18
01:25.570 --> 01:31.720
Basically, if you tie all of these numbers together, there's actually 50 pairs of 101.

19
01:31.720 --> 01:37.750
So he could simply just do 50 multiplied by 101 which is 5050.

20
01:37.810 --> 01:40.030
And that's how he figured out the answer.

21
01:40.150 --> 01:47.040
But we can actually outshine Gauss because we can do this calculation in less than a minute just by

22
01:47.040 --> 01:48.510
writing a few lines of code.

23
01:48.870 --> 01:56.070
But in order to do this, we first have to learn about using for loops with the range function.

24
01:56.580 --> 02:02.820
Now the range function is something that is really, really helpful if you want to generate a range

25
02:02.820 --> 02:04.800
of numbers to loop through.

26
02:04.920 --> 02:08.220
And the syntax looks something like this.

27
02:08.220 --> 02:15.630
So we've still got our for and our in keywords highlighted in blue, but instead of looping through

28
02:15.630 --> 02:21.990
a list, we define how our loop is going to work by creating a range.

29
02:21.990 --> 02:26.700
So in this case I'm creating a range between a and b,

30
02:26.700 --> 02:32.550
and then I'm going to get hold of each number in that range and do something with that number.

31
02:33.330 --> 02:39.080
Now it's important to remember that the range() function doesn't actually do anything by itself.

32
02:39.080 --> 02:46.520
So if we create a range between 1 and 10 and we say, just try to print this and we run, you can see

33
02:46.520 --> 02:47.900
it doesn't actually do anything.

34
02:47.900 --> 02:51.170
It doesn't print out the numbers 1 through to 10 at all.

35
02:51.170 --> 02:55.880
This function has to be used in conjunction with another function,

36
02:55.880 --> 02:59.120
and in our case it's going to be with the for loop.

37
02:59.420 --> 03:08.270
So for example if I wrote, "for number in range..." and then my range is going to be (1, 10).

38
03:08.270 --> 03:12.620
So this is going to be the range that I'm going to create between 1 and 10,

39
03:12.620 --> 03:16.640
and then I want to get hold of each of the numbers inside that range.

40
03:16.640 --> 03:21.950
And this is going to be between 1 and 10 and not including ten.

41
03:21.950 --> 03:28.250
So if I go ahead and print out the number that I create from my for loop, then you'll see that what

42
03:28.250 --> 03:36.410
happens is it prints 123456789, but not the last digit, which I've included here.

43
03:36.410 --> 03:44.210
So if I wanted all of the numbers from 1 to 10, I actually have to set a range between 1 and 11.

44
03:44.210 --> 03:49.310
And now when you run the code, you can see that it goes from one all the way up to ten.

45
03:50.330 --> 03:59.020
Now by default the range() function will step through all the numbers from the start to the end, and

46
03:59.020 --> 04:00.640
it will increase by one.

47
04:00.670 --> 04:06.250
Now, if you wanted to increase by any other number, then you have to add another comma to the end

48
04:06.250 --> 04:11.860
of it and specify how large you want the step to be.

49
04:11.980 --> 04:15.070
So let's say we change the step size to 3.

50
04:15.100 --> 04:19.420
Now if I rerun the same code, you'll see that it goes from 1,

51
04:19.420 --> 04:25.420
and then it steps by 3 to 4, and then it steps by 3, and then it steps by 3, finally to 10.

52
04:26.770 --> 04:30.940
Now let's come back to the problem that I mentioned at the beginning of this lesson,

53
04:30.940 --> 04:36.430
How can we add up all of the numbers from 1 to 100 by using code?

54
04:36.670 --> 04:42.550
So I want you to pause the video and see if you can solve the Gauss challenge.

55
04:42.550 --> 04:43.600
Have a go now.

56
04:46.980 --> 04:47.520
All right.

57
04:47.520 --> 04:49.380
So how do we solve this?

58
04:49.410 --> 04:51.570
Well, it's going to involve the for loop,

59
04:51.570 --> 04:56.910
and it's going to involve the range() function because we're going to get hold of every number in the

60
04:56.910 --> 05:00.450
range between 1 and 100.

61
05:00.450 --> 05:02.340
So we need to write 101.

62
05:02.610 --> 05:08.790
Once we've got hold of all of these numbers it's as simple as using an accumulator.

63
05:08.790 --> 05:11.970
So let's say a total = 0.

64
05:11.970 --> 05:13.740
And then inside the for loop.

65
05:13.740 --> 05:15.150
So that means indented,

66
05:15.150 --> 05:21.210
we're going to say total += number.

67
05:21.810 --> 05:28.530
So now it's going to add every number in this range to the total starting from zero.

68
05:28.530 --> 05:33.870
And this is basically going to give us the sum of every number from 1 to 100.

69
05:33.870 --> 05:38.960
So let's go ahead and print it out and see if it matches Gauss's value.

70
05:40.340 --> 05:43.820
And there you have it, 5050.

71
05:44.180 --> 05:49.370
So now that we've beaten Gauss at his own game of math using programming, let's head over to the next

72
05:49.370 --> 05:55.100
lesson and try to complete a challenge using the range() function to see if you managed to internalize

73
05:55.100 --> 05:57.920
and understand everything we talked about in this lesson.

74
05:57.920 --> 06:00.740
So for all of that and more, I'll see you there.