Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Now up to this point,
2
we've only been using for loops in association with lists.
3
So we've been looping through the list and getting hold of each of the items in
4
the list and then doing something with it, right?
5
But we're not always going to be working with lists and using for loops.
6
Sometimes we might want to use a loop completely independent of a list.
7
And a good example of this is when Carl Gauss the German
8
mathematician was just a child when he was 10 years old,
9
his math teacher gave him an exercise that she probably thought would tie him up
10
for it a little while.
11
And the idea was to get him to add all of the numbers from 1 to 100.
12
So 1 + 2 + 3 + 4 + 5, et cetera,
13
all the way until a hundred.
14
And even though she thought this would keep him busy for an hour or so getting
15
this young child to add up these numbers and I can have some peace and quiet
16
scrolling on Facebook,
17
but unfortunately he came back to her within two minutes and gave her the
18
answer. So how did he work it out? Well,
19
here's actually a really smart kid and he figured out that if you flip the
20
numbers around, so 100 + 99 + 98,
21
and you'll look at both of these two lines,
22
you can see that 1 + 100 = 101, 2
23
+ 99 = 101, 3 + 98 = 101, basically,
24
if you tie all of these numbers together,
25
there's actually 50 pairs of 101.
26
So he could simply just do 50 multiplied by 101 which is 5,050.
27
And that's how he figured out the answer.
28
But we can actually outshine Gaus because we can do this calculation in less
29
than a minute, just by writing a few lines of code. But in order to do this,
30
we first have to learn about using for loops with the range function.
31
Now the range function is something that is really, really helpful
32
if you want to generate a range of numbers to loop through.
33
And the syntax looks something like this.
34
So we've still got our for and our in keywords highlighted in blue,
35
but instead of looping through a list,
36
we define how our loop is going to work by creating a range.
37
So in this case,
38
I'm creating a range between a and b and then I'm going to get hold of each
39
number in that range and do something with that number.
40
So for example, if I wrote for number in range,
41
and then my range is going to be 1, 10.
42
So this is going to be the range that I'm going to create between 1 and 10
43
and then I want to get hold of each of the numbers inside that range.
44
And this is going to be between 1 and 10 and not including 10.
45
So if I go ahead and print out the number that I create from my for loop,
46
then you'll see that what happens is it prints 1 2 3 4 5
47
6 7 8 9, but not the last digit, which I've included here.
48
So if I wanted all of the numbers from 1 to 10,
49
I actually have to set a range between 1 and 11.
50
And now when you run the code,
51
you can see that it goes from 1 all the way up to 10.
52
Now by default,
53
the range function will step through all the numbers from the start to the end,
54
and it will increase by 1. Now,
55
if you want it to increase by any other number,
56
then you have to add another comma to the end of it and specify how large you
57
want the step to be. So let's say we changed the step size to three.
58
Now, if I rerun the same code,
59
you'll see that it goes from one and then it steps by three to four and then it
60
steps by three. And then it steps by three, finally to 10.
61
Now let's come back to the problem that I mentioned at the beginning of this
62
lesson.
63
How can we add up all of the numbers from 1 to 100 by using code?
64
Well,
65
it's going to involve a for loop and it's going to involve the range function
66
because we're going to get hold of every number in the range between 1 and
67
100,
68
so we need to write 101. Once we've got hold of all of these numbers,
69
it's as simple as using a accumulator,
70
so let's say a total = 0. And then inside the for loop,
71
so that means indented,
72
we're going to say total += number.
73
So now it's going to add every number in this range to the total starting from
74
zero.
75
And this is basically going to give us the sum of every number from 1 to 100.
76
So let's go ahead and print it out and see if it matches Gauss's value.
77
And there you have it 5,050.
78
So now that we've beaten Gauss at his own game of math using programming,
79
let's head over to the next lesson and try to complete a challenge using the
80
range function to see if you managed to internalize and understand everything we
81
talked about in this lesson. So for all of that and more, I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.