Check out a free preview of the full Python Fundamentals course

The "Looping Over Lists" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina introduces "for in" syntax as well as how to utilize the range keyword.

Preview
Close

Transcript from the "Looping Over Lists" Lesson

[00:00:00]
>> Nina Zakharenko: For now we're gonna talk about loops and control statements. We gonna talk about for loops in Python and if else and else if. And just kind of as an aside, I'm gonna through the final exercise first. And then I will come back to this section and go over while loops and break and continue.

[00:00:22]
So if you wanna stay afterwards for that, you're welcome to.
>> Nina Zakharenko: But loops and control statements in Python. So if you've done Java or JavaScript or C++, you might have seen some for loops that look like this, right? For and then i is zero, you're defining the condition that we're looping until, which is i is less than five, and then defining the operation you're doing to meet the condition.

[00:00:55]
For loops in Python are a lot nicer than this.
>> Nina Zakharenko: Let's say we had a list of colors. I can just loop over for color in colors and print out the value of the item. That's a for loop in Python.
>> Nina Zakharenko: Let's say my colors were red, green, blue.

[00:01:23]
I can say for color in colors, so that's kind of a temporary, color is a temporary variable that exists in the context of the for loop and each item in that list is gonna get assigned a color in turn.
>> Nina Zakharenko: And then we go through each one and print out red, green, blue.

[00:01:46]
>> Student: So that word color could've been any word, right?
>> Nina Zakharenko: Yes, any variable name that you decide.
>> Student: Cuz you're kind of instantiating it or making it available at that point.
>> Nina Zakharenko: Yeah.
>> Student: Okay.
>> Nina Zakharenko: And then something kind of interesting here is that this isn't a function, there's no scope here, right?

[00:02:10]
And so color will exist in your scope as the last color it was set to. So something to keep an eye on. And just like with functions, it's the indentation that defines what belongs to the for loop.
>> Nina Zakharenko: If we want to go through a sequence of numbers, there's a function in Python called range.

[00:02:39]
And if we call it, we'll see this type. So range implicitly starts with zero and ends at the provided number. And that provided number, it's not inclusive of. So this will give us a range of numbers from 0 to 4. But Python is kind of smart about it, doesn't generate that whole list at once.

[00:03:03]
As we go through the loop, it kind of generates it as we need it. If we want to see what is generated in this list, or in the range function under the hood, we can pass it to list, and that will immediately evaluate all of those. So just like before, I can loop through a number in the range and print out the number.

[00:03:30]
And this will print, 0, 1, 2, 3, 4, not inclusive of 5. For range, I can provide two values. The first is where to start, the second is where to end. The start is inclusive, so in this range 1 is included in the numbers but 5 is not.

[00:03:54]
5 is exclusive. Okay, so we got 1234.
>> Nina Zakharenko: There's one other value that you can pass to range. And that is a step argument, so that's, how many numbers should I skip over by? So let's say I start at 1 and end at 7 and do a step of 2, and you can also kind of explicitly name this optionally.

[00:04:24]
In that way it's a little bit clearer to read. What do you think this will print out?
>> Student: 1, 3, 5.
>> Nina Zakharenko: Oops. So in Python 2, print was not a function, it was a statement. And every once in a while it catches me too. No, I'm sorry, range does not take keyword arguments, it's just an order.

[00:04:51]
So,
>> Nina Zakharenko: My apologies for that. Range takes three arguments, and they're just positional.

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