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 10 years old,
his math teacher gave him an exercise that she probably thought would tie him up
for it 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 a hundred.
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,
here's actually a really smart kid and he figured out that if you flip the
numbers around, so 100 + 99 + 98,
and you'll 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 5,050.
And that's how he figured out the answer.
But we can actually outshine Gaus 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.
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 10.
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 1 2 3 4 5
6 7 8 9, 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 1 all the way up to 10.
Now by default,
the range function will step through all the numbers from the start to the end,
and it will increase by 1. Now,
if you want it 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 changed the step size to three.
Now, if I rerun the same code,
you'll see that it goes from one and then it steps by three to four and then it
steps by three. And then it steps by three, 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?
Well,
it's going to involve a 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 a 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 5,050.
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.