Practical Guide to Python

# Loops Practice

## Nina Zakharenko

Microsoft

### Check out a free preview of the full Practical Guide to Python course

The "Loops Practice" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina walks the students through the Loops practice.

Preview
Close

### Transcript from the "Loops Practice" Lesson

[00:00:00]
>> Let's talk about practice for loops, going down to control statements and looping. We talked about if, else, and else if, so when we have a statement like this, the if statement, we're gonna check and see if the expression afterwards is true. If it is, we'll run this code, if there's an else if present and the if statement is false, we will keep checking the else ifs until we find one that is true.

[00:00:33]
If there are no else ifs with a true statement, then we will jump to the else and go through the code here. So in this case, when we call this on the number 5, we're gonna print out this is a small number, for 99, we're gonna say, this, This is a pretty small number as well.

[00:00:57]
If it's exactly 100, we're gonna say, this number is all right. And then for this number here, we will jump to the else statement, and say this number is huge. We can also have multiple conditions in an if statement. So here we're checking to see if the number that is passed into fizzbuzz is both divisible by 3 and divisible by 5, then we'll print fizzbuzz.

[00:01:21]
So we print nothing for 3, nothing for 5, and then fizzbuzz for the number 15. We also can use if to test for an empty list. So here, my function is expecting a list. Remember that an empty list is falsey and resolves to false, while a list with values in it is truthy and will resolve to true.

[00:01:44]
So when I pass in a list with values, this if statement will be valid and I'll jump down into this block of code. If I pass in an empty list, I'm going to jump straight to the else statement here and print out, got an empty list. We talked about some of those helper functions, like the range and enumerate, we can use those in conjunction with the for loop as well.

[00:02:12]
If I have a list defined here, if I wanted to go through all the values between 0 and 2, I could write them out manually and have a bad time of it. Or instead, I can use a shortcut of the range function, passing a start of 0 and an end of 3, because that ending value is not inclusive and do the same thing.

[00:02:33]
The advantage of using range is that there are some optimizations in play that mean I don't have to define a list with all my values in it. Which is fine for my list of three items, but starts to become more of an issue for large or complex lists.

[00:02:49]
And lastly, if we wanted to loop over and have both an index and the value at that index, we want to use the enumerate function. And the enumerate function is a generator. But if you take a look at what it's actually returning, it's returning a list of tuples.

[00:03:10]
And the first item in that tuple is an index, and the second is the item itself. So we can use tuple unpacking in our for statement here and get back the index and the item from enumerate all in one step. We can also use this to loop over a dictionary.

[00:03:29]
Remember, by default, if you loop over a dictionary, you loop over just the keys. If you want both the keys and the values, you have to make sure that you call the .items function on it. And when you do that, you also get back a list of tuples with the first value being the key and the second value being the value itself.

[00:03:55]
So you can also use tuple unpacking here to loop through both the key and the value at the same time. We can use return statements to break out of functions and break out of loops, which is useful when you are looking for a flag. And I'm not going to go over while loops, but if you want to learn more about while loops or nested loops, I recommend that you read the chapters at practical.learnpython.dev.

[00:04:22]
I also recommend that you check out the optional chapter on break and continue on your own time, because those are two concepts that tend to trip out folks who are beginners or intermediate programmers, and they're important concepts for you to have a solid grasp of.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses
• Industry Leading Experts
• Learning Paths
• Live Interactive Workshops
Get Unlimited Access Now