Now, up to this point, we've only been using for loops in association with lists, so we've been looping
through the list and getting hold of each of the items in the list and then doing something with it,
right?
But we're not always going to be working with lists and using for loops.
Sometimes we might want to use a loop completely independent of a list.
And a good example of this is when Carl Gauss, the German mathematician, was just a child.
When he was ten years old, his math teacher gave him an exercise that she probably thought would tie
him up for a little while, and the idea was to get him to add all of the numbers from 1 to 100.
So 1 + 2 +3 + 4 + 5 et cetera, all the way until 100.
And even though she thought this would keep him busy for an hour or so, getting this young child to
add up these numbers and I can have some peace and quiet scrolling on Facebook,
but unfortunately, he came back to her within two minutes and gave her the answer.
So how did he work it out?
Well, he is actually a really smart kid, and he figured out that if you flip the numbers around.
So 100 plus 99 plus 98, and you look at both of these two lines, you can see that 1 + 100 = 101
,
2 + 99 = 101,
3 + 98 = 101.
Basically, if you tie all of these numbers together, there's actually 50 pairs of 101.
So he could simply just do 50 multiplied by 101 which is 5050.
And that's how he figured out the answer.
But we can actually outshine Gauss because we can do this calculation in less than a minute just by
writing a few lines of code.
But in order to do this, we first have to learn about using for loops with the range function.
Now the range function is something that is really, really helpful if you want to generate a range
of numbers to loop through.
And the syntax looks something like this.
So we've still got our for and our in keywords highlighted in blue, but instead of looping through
a list, we define how our loop is going to work by creating a range.
So in this case I'm creating a range between a and b,
and then I'm going to get hold of each number in that range and do something with that number.
Now it's important to remember that the range() function doesn't actually do anything by itself.
So if we create a range between 1 and 10 and we say, just try to print this and we run, you can see
it doesn't actually do anything.
It doesn't print out the numbers 1 through to 10 at all.
This function has to be used in conjunction with another function,
and in our case it's going to be with the for loop.
So for example if I wrote, "for number in range..." and then my range is going to be (1, 10).
So this is going to be the range that I'm going to create between 1 and 10,
and then I want to get hold of each of the numbers inside that range.
And this is going to be between 1 and 10 and not including ten.
So if I go ahead and print out the number that I create from my for loop, then you'll see that what
happens is it prints 123456789, but not the last digit, which I've included here.
So if I wanted all of the numbers from 1 to 10, I actually have to set a range between 1 and 11.
And now when you run the code, you can see that it goes from one all the way up to ten.
Now by default the range() function will step through all the numbers from the start to the end, and
it will increase by one.
Now, if you wanted to increase by any other number, then you have to add another comma to the end
of it and specify how large you want the step to be.
So let's say we change the step size to 3.
Now if I rerun the same code, you'll see that it goes from 1,
and then it steps by 3 to 4, and then it steps by 3, and then it steps by 3, finally to 10.
Now let's come back to the problem that I mentioned at the beginning of this lesson,
How can we add up all of the numbers from 1 to 100 by using code?
So I want you to pause the video and see if you can solve the Gauss challenge.
Have a go now.
All right.
So how do we solve this?
Well, it's going to involve the for loop,
and it's going to involve the range() function because we're going to get hold of every number in the
range between 1 and 100.
So we need to write 101.
Once we've got hold of all of these numbers it's as simple as using an accumulator.
So let's say a total = 0.
And then inside the for loop.
So that means indented,
we're going to say total += number.
So now it's going to add every number in this range to the total starting from zero.
And this is basically going to give us the sum of every number from 1 to 100.
So let's go ahead and print it out and see if it matches Gauss's value.
And there you have it, 5050.
So now that we've beaten Gauss at his own game of math using programming, let's head over to the next
lesson and try to complete a challenge using the range() function to see if you managed to internalize
and understand everything we talked about in this lesson.
So for all of that and more, I'll see you there.