All language subtitles for 051 for loops and the range() function.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,580 --> 00:00:01,960 Now up to this point, 2 00:00:01,990 --> 00:00:06,640 we've only been using for loops in association with lists. 3 00:00:07,030 --> 00:00:11,830 So we've been looping through the list and getting hold of each of the items in 4 00:00:11,830 --> 00:00:14,200 the list and then doing something with it, right? 5 00:00:14,740 --> 00:00:18,940 But we're not always going to be working with lists and using for loops. 6 00:00:19,390 --> 00:00:23,920 Sometimes we might want to use a loop completely independent of a list. 7 00:00:24,640 --> 00:00:29,080 And a good example of this is when Carl Gauss the German 8 00:00:29,080 --> 00:00:32,380 mathematician was just a child when he was 10 years old, 9 00:00:32,710 --> 00:00:36,790 his math teacher gave him an exercise that she probably thought would tie him up 10 00:00:36,790 --> 00:00:37,623 for it a little while. 11 00:00:38,080 --> 00:00:43,080 And the idea was to get him to add all of the numbers from 1 to 100. 12 00:00:43,990 --> 00:00:47,470 So 1 + 2 + 3 + 4 + 5, et cetera, 13 00:00:47,710 --> 00:00:49,660 all the way until a hundred. 14 00:00:50,500 --> 00:00:54,010 And even though she thought this would keep him busy for an hour or so getting 15 00:00:54,010 --> 00:00:58,000 this young child to add up these numbers and I can have some peace and quiet 16 00:00:58,000 --> 00:00:59,020 scrolling on Facebook, 17 00:00:59,440 --> 00:01:03,160 but unfortunately he came back to her within two minutes and gave her the 18 00:01:03,160 --> 00:01:06,880 answer. So how did he work it out? Well, 19 00:01:06,910 --> 00:01:11,140 here's actually a really smart kid and he figured out that if you flip the 20 00:01:11,140 --> 00:01:14,560 numbers around, so 100 + 99 + 98, 21 00:01:14,950 --> 00:01:17,650 and you'll look at both of these two lines, 22 00:01:17,980 --> 00:01:21,700 you can see that 1 + 100 = 101, 2 23 00:01:21,700 --> 00:01:26,410 + 99 = 101, 3 + 98 = 101, basically, 24 00:01:26,410 --> 00:01:28,450 if you tie all of these numbers together, 25 00:01:28,720 --> 00:01:31,690 there's actually 50 pairs of 101. 26 00:01:31,960 --> 00:01:36,960 So he could simply just do 50 multiplied by 101 which is 5,050. 27 00:01:38,080 --> 00:01:39,790 And that's how he figured out the answer. 28 00:01:40,450 --> 00:01:45,450 But we can actually outshine Gaus because we can do this calculation in less 29 00:01:45,670 --> 00:01:50,230 than a minute, just by writing a few lines of code. But in order to do this, 30 00:01:50,380 --> 00:01:55,380 we first have to learn about using for loops with the range function. 31 00:01:56,770 --> 00:02:00,460 Now the range function is something that is really, really helpful 32 00:02:00,760 --> 00:02:04,510 if you want to generate a range of numbers to loop through. 33 00:02:05,290 --> 00:02:08,230 And the syntax looks something like this. 34 00:02:08,410 --> 00:02:12,940 So we've still got our for and our in keywords highlighted in blue, 35 00:02:13,570 --> 00:02:16,540 but instead of looping through a list, 36 00:02:17,050 --> 00:02:21,910 we define how our loop is going to work by creating a range. 37 00:02:22,300 --> 00:02:23,050 So in this case, 38 00:02:23,050 --> 00:02:28,050 I'm creating a range between a and b and then I'm going to get hold of each 39 00:02:29,050 --> 00:02:32,320 number in that range and do something with that number. 40 00:02:33,700 --> 00:02:38,200 So for example, if I wrote for number in range, 41 00:02:38,560 --> 00:02:42,130 and then my range is going to be 1, 10. 42 00:02:42,520 --> 00:02:46,510 So this is going to be the range that I'm going to create between 1 and 10 43 00:02:46,870 --> 00:02:50,470 and then I want to get hold of each of the numbers inside that range. 44 00:02:50,980 --> 00:02:55,810 And this is going to be between 1 and 10 and not including 10. 45 00:02:56,170 --> 00:03:00,340 So if I go ahead and print out the number that I create from my for loop, 46 00:03:00,820 --> 00:03:05,800 then you'll see that what happens is it prints 1 2 3 4 5 47 00:03:05,800 --> 00:03:10,210 6 7 8 9, but not the last digit, which I've included here. 48 00:03:10,660 --> 00:03:14,680 So if I wanted all of the numbers from 1 to 10, 49 00:03:15,010 --> 00:03:17,920 I actually have to set a range between 1 and 11. 50 00:03:18,610 --> 00:03:20,230 And now when you run the code, 51 00:03:20,290 --> 00:03:23,050 you can see that it goes from 1 all the way up to 10. 52 00:03:24,580 --> 00:03:26,320 Now by default, 53 00:03:26,380 --> 00:03:31,380 the range function will step through all the numbers from the start to the end, 54 00:03:32,710 --> 00:03:35,170 and it will increase by 1. Now, 55 00:03:35,170 --> 00:03:37,870 if you want it to increase by any other number, 56 00:03:38,020 --> 00:03:43,020 then you have to add another comma to the end of it and specify how large you 57 00:03:44,140 --> 00:03:48,910 want the step to be. So let's say we changed the step size to three. 58 00:03:49,390 --> 00:03:51,610 Now, if I rerun the same code, 59 00:03:51,850 --> 00:03:55,930 you'll see that it goes from one and then it steps by three to four and then it 60 00:03:55,930 --> 00:03:59,260 steps by three. And then it steps by three, finally to 10. 61 00:04:01,000 --> 00:04:04,180 Now let's come back to the problem that I mentioned at the beginning of this 62 00:04:04,180 --> 00:04:05,013 lesson. 63 00:04:05,170 --> 00:04:10,170 How can we add up all of the numbers from 1 to 100 by using code? 64 00:04:10,720 --> 00:04:11,020 Well, 65 00:04:11,020 --> 00:04:14,590 it's going to involve a for loop and it's going to involve the range function 66 00:04:15,070 --> 00:04:20,070 because we're going to get hold of every number in the range between 1 and 67 00:04:20,770 --> 00:04:21,490 100, 68 00:04:21,490 --> 00:04:26,050 so we need to write 101. Once we've got hold of all of these numbers, 69 00:04:26,110 --> 00:04:29,830 it's as simple as using a accumulator, 70 00:04:29,860 --> 00:04:34,810 so let's say a total = 0. And then inside the for loop, 71 00:04:34,810 --> 00:04:36,100 so that means indented, 72 00:04:36,370 --> 00:04:41,370 we're going to say total += number. 73 00:04:43,090 --> 00:04:48,090 So now it's going to add every number in this range to the total starting from 74 00:04:48,790 --> 00:04:49,623 zero. 75 00:04:49,900 --> 00:04:54,760 And this is basically going to give us the sum of every number from 1 to 100. 76 00:04:55,150 --> 00:04:59,770 So let's go ahead and print it out and see if it matches Gauss's value. 77 00:05:01,690 --> 00:05:04,570 And there you have it 5,050. 78 00:05:05,440 --> 00:05:09,160 So now that we've beaten Gauss at his own game of math using programming, 79 00:05:09,430 --> 00:05:13,240 let's head over to the next lesson and try to complete a challenge using the 80 00:05:13,240 --> 00:05:17,770 range function to see if you managed to internalize and understand everything we 81 00:05:17,770 --> 00:05:21,760 talked about in this lesson. So for all of that and more, I'll see you there. 7780

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.