Python for the .NET developer Transcripts
Chapter: The Python Language
Lecture: Python loops
0:00 It's time to write the Python loop code and
0:02 explore the different types of loops and ways
0:05 we can work with iterable objects in python.
0:08 Now, quick note, I renamed this from program to
0:11 L01 For Language Part 1 Structure
0:16 I'm going to create and partition this a little bit.
0:18 So, I'm going to have L02_iteration
0:21 that's going to be the way we want it to run.
0:23 Remember we always start by having the main
0:25 method and then that if thing that I created
0:28 with if __main__, we going to write it like that
0:30 really quickly.
0:32 And in our main method, we're going
0:34 to do some exploration.
0:35 You could actually just put the code right here
0:36 you don't need a method but I don't know, that's
0:38 just seems wrong to me so I'm not going to do it.
0:41 So, let's do what we had before, we had a while
0:44 loop and in Python, we have while loop as well.
0:48 But we have a for true written like that
0:50 we don't have that in Python.
0:51 We have a capital T, True.
0:53 So True and False, the Booleans are capitalized.
0:56 So, that's fine.
0:57 We wanted to find the block of code here and what
0:59 we did is we had name variable and we ask the
1:02 question What is your name?
1:04 So, What is your name?
1:07 So, we also at the very beginning, we had a
1:10 Python iteration demo.
1:12 Something like that, right?
1:14 Then down here, we had an if statement, we said
1:15 if we had string.IsNullOrWhiteSpace
1:24 We can do something like that, we can just say
1:26 if not name like this, you'd have more sort of
1:32 truthiness in objects in Python than you do in
1:35 C#, everything has to been exactly a Boolean.
1:37 We can just say, is this string False, right
1:39 is it empty or something like that?
1:41 We can break after there.
1:43 Otherwise, we going to do print with a
1:46 formatted string Nice to meet you + name
1:51 something like that.
1:53 All right well, that's pretty straight forward isn't it?
1:55 Lets run it.
1:56 That looks really similar, exchanging the code
1:59 structure a bit as we had before.
2:01 So, down here we can say What is your name
2:03 My name is Michael, Nice to meet you
2:05 What is your name, My name is Zoe.
2:07 If I enter, it breaks out, is done.
2:10 So, that's this part right here.
2:14 I'm going to comment that out for a second
2:15 so we just focus on the next bit.
2:17 We had some numbers over at the C# version
2:20 and I'm just going to grab amount, so we have
2:24 the same thing, so just going to copy those over.
2:27 Now, we are going to define some numbers in Python
2:30 an integer array so we had nums, we don't say
2:33 the type, it's dynamic at least for now unless
2:35 you want to add the types, like I said we'll
2:37 talk about that later.
2:39 And we put brackets, paste.
2:40 So, this is not actually an array, in C#
2:43 we had a static array, here we have a list.
2:46 So, this is like list of int or more like
2:49 list of object equivalent.
2:54 In the C# equivalent would be the list of
2:57 object here, all right, pre-allocated.
3:00 Now, what we want to do is we going to go and
3:02 have a foreach loop type of thing, and I told
3:04 you, it was one of my favorite thing about the
3:07 C# language coming to it from C++ is like
3:09 Whoa! This is so much nicer. Turns out Python
3:13 has exactly the same construct, the rules about
3:16 what go into the loop are the same and has to be
3:18 iterable kind of like it has to be IEnumerable in .NET
3:21 But it's not called foreach, it's called for.
3:24 So, the way it works is that you say
3:25 for like this. So, this is like foreach in C#.
3:36 And then, what do we have, we just print it out
3:39 the next number is n.
3:43 Well, that's pretty straight forward right?
3:44 Really nice that there is something like if
3:46 foreach loop, words are not exactly the same
3:49 but what's really interesting is that there is no
3:51 for that does not exist. There is none of this.
4:03 Alright, so I'll put this down here.
4:06 It turns out that we can simulate this kind
4:08 of loop in Python but there's not an actual
4:12 integer for loop.
4:13 This is the only kind of for loop in Python
4:16 as is for. Alright, well, let's just run it
4:20 and see if it works. Boom!
4:22 The next number is 5, 8, 10 just like we
4:24 had in C#.
4:25 What we did though in C# is that...
4:27 Well, we wanted to say the first number is this
4:29 the second number is that and so on.
4:31 So, we said alright, fine. We'll break down
4:32 using the for loop.
4:34 We're going to do something even better in Python.
4:36 Check this out!
4:39 So, Python has this concept of tuples
4:42 this group, two or more things that is kind of
4:45 like a list, you can iterate it and stuff but
4:48 what's really interesting about it is I can
4:50 have like two variables to find like this.
4:52 Let's say x, y = 1, 2. This would create a tuple
4:57 and assign 1 to x and y would get 2.
5:02 We can use that in this little loop here
5:04 I could say I want index and the number and
5:08 I can not iterate over the collection but I can
5:11 iterate over the enumeration of the collection.
5:14 Enumerate nums and I can even say start = 1
5:20 and then we can come to here the idx
5:24 how we had it, numbers this.
5:26 We don't even have to plus one because it
5:28 automatically goes 1, 2, 3 not zero
5:30 1, 2. Alright, let's run this.
5:33 Let's put it also some space between them.
5:38 How cool is that!
5:40 That is a super slick way to loop over those
5:44 and it's much better than what you have to do
5:46 if you resort to this.
5:47 So, you can see this for in loops are really
5:49 really flexible in Python.
5:52 Final thing is sometimes you want to just
5:54 do a thing 10 times right?
5:56 And this for
6:00 actually it's really useful for I want to do
6:02 limit times.
6:04 We also have that in Python and it leverages
6:06 this idea and natural support for ranges.
6:10 So, I could just say for n in range of
6:14 one to 11, it's a...
6:17 So we go one up to 10 inclusive, not the last one
6:22 and I can say print again or this time or
6:27 I don't know, something like that.
6:28 Let's put a little separator here and
6:31 let's actually make that just run five times.
6:34 So we run this again.
6:36 Then, 1, 2, 3, 4, 5 times
6:38 we said this time.
6:39 All right, so that's really cool.
6:40 This is like your for right?
6:47 But we're not using this n here, there's a
6:48 convention in Python to say there has to be
6:52 a variable define here, you can't just put
6:54 nothing here, that's going to be an error.
6:56 There's a variable that has to go here but I
6:58 specifically am stating I don't want to use
7:01 it, I don't care what it is and that's underscore.
7:05 So, all the Linters and code-checkers and stuff
7:07 when they see that underscore, they will not warn
7:09 you that underscore is not use but they would warn
7:12 you that n wasn't use in that case, potentially
7:15 if it weren't use up here, right?
7:18 So, let's run it one more time. Same thing.
7:20 Alright, so this is loops in Python and
7:23 super good news, your for-each loop, you still
7:26 got it and it's actually I think a little bit
7:28 better than .NET.