Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,360 --> 00:00:02,970
Let's talk about loops.
2
00:00:02,970 --> 00:00:11,220
We've talked about conditional operators logical operators but loops gives us a whole new power when
3
00:00:11,220 --> 00:00:19,360
it comes to our machines the concept of loops or looping in programming is really really powerful.
4
00:00:19,360 --> 00:00:26,050
We saw that with logical operators and conditional logic we're able to skip lines in our program so
5
00:00:26,050 --> 00:00:33,370
that we don't always go one two three four five six so on and so forth but loops do a really interesting
6
00:00:33,370 --> 00:00:33,960
thing.
7
00:00:34,090 --> 00:00:42,650
It allows us to run lines of code over and over and over and that's really powerful because that means
8
00:00:42,740 --> 00:00:46,500
we can run things thousands of times millions of times.
9
00:00:46,730 --> 00:00:55,520
And this is where machines Excel machines excel at doing small tasks over and over really really fast
10
00:00:55,850 --> 00:00:57,400
way better than humans.
11
00:00:57,410 --> 00:01:02,140
So loops are one of the most powerful features of programming languages.
12
00:01:02,180 --> 00:01:11,410
So how do we create what Well it's as simple as using what we call for or in python we call them for
13
00:01:11,560 --> 00:01:24,810
loops with this keyword for we're able to say for let's say item in 0 2 mastery do something.
14
00:01:24,850 --> 00:01:29,820
So they're kind of like conditional operators instead of if we have four.
15
00:01:29,950 --> 00:01:37,580
But then we have this thing grade where we have an item which what is that we don't really know.
16
00:01:37,630 --> 00:01:42,070
Well this is a variable that we create we can name it whatever we want.
17
00:01:42,100 --> 00:01:47,680
We can say I we can say teddy bears whatever you want.
18
00:01:47,680 --> 00:01:56,550
This is a variable and a variable is created here for each item after the n.
19
00:01:56,620 --> 00:02:08,080
So think hey for every item in 0 2 mastery do something and we've seen this in keyword before and we'll
20
00:02:08,080 --> 00:02:15,280
dig deep into this later but we call this an intolerable and irritable is something that is able to
21
00:02:15,280 --> 00:02:16,640
get looped over.
22
00:02:16,810 --> 00:02:25,930
So an irritable allows us to use this notation of A for something in an interval to iterate over each
23
00:02:26,080 --> 00:02:37,540
item in our case if I print item here and I click Run look at that it prints each item in the interval
24
00:02:37,900 --> 00:02:39,850
which is the strings 0 2 mastery.
25
00:02:39,850 --> 00:02:48,310
So every letter it goes into every bookshelf in our machine's memory and prints each item one at a time.
26
00:02:48,310 --> 00:02:56,770
Now this works with strings but we can also use lists like One two three four five that we've seen before.
27
00:02:56,770 --> 00:03:04,900
If I click Run Again this is an integral we're able to iterate over it and it grabs each item in the
28
00:03:04,900 --> 00:03:05,320
list.
29
00:03:05,860 --> 00:03:08,130
Let's do a set.
30
00:03:08,130 --> 00:03:14,390
Can we do this with a set well if I click Run that works as well.
31
00:03:14,410 --> 00:03:21,750
What about a temple let's do with the top one fact like run that works as well.
32
00:03:21,750 --> 00:03:23,200
That's amazing.
33
00:03:23,220 --> 00:03:25,620
What about a dictionary.
34
00:03:25,620 --> 00:03:34,000
Well we'll get to dictionaries in a bit so for loops allow us to iterate over anything that has a collection
35
00:03:34,000 --> 00:03:40,510
of items so that in this case we're looping one two three four five times and you can see over here
36
00:03:40,510 --> 00:03:49,030
that we have the call in and then the indentation to tell Python hey whatever comes here I can print
37
00:03:49,210 --> 00:03:56,260
item again I can print item again I can do it as many times as possible as long as I have indentation
38
00:03:56,590 --> 00:04:07,710
it's going to keep printing our numbers but as soon as I open up here I am print something else.
39
00:04:11,220 --> 00:04:14,450
I only get that once because it's not in the loop.
40
00:04:14,670 --> 00:04:22,320
So our program first runs this for one so it's going to print 1 3 times then two then 3 and 4 then 5
41
00:04:22,590 --> 00:04:23,760
and then only finally.
42
00:04:23,760 --> 00:04:25,660
Then it goes to print.
43
00:04:25,860 --> 00:04:30,570
You see how we're looping over and over and over.
44
00:04:30,660 --> 00:04:33,650
Now what happens if I try and print item here.
45
00:04:35,690 --> 00:04:38,830
And I click Run.
46
00:04:38,970 --> 00:04:45,980
Did you see that item gets printed at the very end here I don't know if you can notice it but you see
47
00:04:45,980 --> 00:04:47,280
that there's four fives.
48
00:04:47,330 --> 00:04:55,160
We've printed one two three for each three times but five gets printed four times because this last
49
00:04:55,280 --> 00:05:05,510
print at the very end is well five because by the time the loop ends item the value of item is actually
50
00:05:05,520 --> 00:05:06,940
five.
51
00:05:06,950 --> 00:05:16,130
Let me ask you this can I do something like for let's say x in another list that contains strings a
52
00:05:16,340 --> 00:05:22,280
b and c could I do something like this.
53
00:05:22,290 --> 00:05:30,660
Absolutely I can mess things in Python and as a matter of fact when we do conditionals in Python.
54
00:05:30,900 --> 00:05:32,940
Like if statements.
55
00:05:32,940 --> 00:05:35,340
We can always ness those as well.
56
00:05:35,340 --> 00:05:38,180
Because in Python it's always the indentation.
57
00:05:38,200 --> 00:05:39,010
Right.
58
00:05:39,060 --> 00:05:40,700
So I can say print here.
59
00:05:40,710 --> 00:05:44,470
Item and then also print.
60
00:05:44,520 --> 00:05:50,220
And you know let's print them one next to the other one as a print item and print x.
61
00:05:50,520 --> 00:05:51,330
If I click Run
62
00:05:54,460 --> 00:06:03,030
you see that we're printing 1 and day 1 and b 1 and c because item we run line 1.
63
00:06:03,130 --> 00:06:08,690
So item is currently 1 so we're gonna go to the next line.
64
00:06:08,720 --> 00:06:09,500
Line 2.
65
00:06:09,590 --> 00:06:15,690
And we're gonna say we're gonna loop over this interval and we're gonna say this is x.
66
00:06:15,800 --> 00:06:17,680
So this is going to be 1.
67
00:06:17,690 --> 00:06:19,440
This is going to be a.
68
00:06:19,850 --> 00:06:26,030
And then Python comes back to line 2 because we're still in this loop and we're going to say hey what's
69
00:06:26,030 --> 00:06:26,620
X now.
70
00:06:26,630 --> 00:06:27,650
Well we're done with.
71
00:06:27,680 --> 00:06:28,560
So let's go to be.
72
00:06:28,790 --> 00:06:38,120
So it's going to run B and then C and only once this is done does it go back out and now starts with
73
00:06:38,120 --> 00:06:47,080
2 which is right here to a to b to c so we can have nested loops over and over and over as well.
74
00:06:47,090 --> 00:06:47,630
All right.
75
00:06:47,700 --> 00:06:55,320
So these are loops and right now maybe it's not completely obvious why these may be useful.
76
00:06:55,500 --> 00:07:00,630
But before we get into that I want to talk about this idea of ignorable in the next video.
7707
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.