Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: Python loops

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


Talk Python's Mastodon Michael Kennedy's Mastodon