It’s Day 11. We’ll see how there’s more to loops that first meets the eye.
Pythonic Looping
00:00
One of the easiest ways of spotting programmers who may be experienced in other programming languages and then who’ve just moved to Python, is in how they write their for
loop.
00:09
Because the for
loop is different in Python to how it is in many other languages. In other programming languages, a common way of looping is to go through a set of numbers, a sequence of numbers.
00:22
For example, you say, let me go through the numbers from zero to 20. These are the indices. And then you use those numbers, those indices within the for
loop.
00:32 For example, if you want to fetch items from a list outside of Python, then you, you would use those numbers. Square bracket is zero to get the first one, and then square bracket is one.
00:41 And since you’re going from number zero to 20, you will go through all of the, in this case, 21 items that you, you have. Now in Python, we do not do that.
00:53 You can, but it’s considered non-Pythonic. Why? And the reason is something you’ve already seen. The Iterator protocol. Python has a protocol that does iteration in a different way.
01:05
We’ve seen how you have your Iterable is what you can use in the for
loop, but then you always can create an iterator, which
01:13
yields one value after another. So the Iterator protocol takes care of going through each item of an Iterable. So this means that the for
loops look, for
loops look a bit different, and looping in general looks different.
01:27 And this is what we generally call Pythonic looping, which can be a bit different to other languages. Now, you don’t have to use Pythonic looping. You can use the loops that are general in other languages, but there are a number of reasons why you might want to prefer Pythonic looping.
01:44 Now, first of all, it’s the standard in Python. So when you look at other people’s code, when you’re working in a team, the standard is to do it the Python way.
01:53 But you want to loop Pythonically, not just so that you can put on the badge. Look, I’m a Python programmer, but since the Iterator protocol is integral to how Python works, looping Pythonically, so looping, using that protocol is the most efficient way of programming.
02:13 So your program will, you are looping in a way Python. Python intends you to, so that is the preferred way. It’ll make your code smoother, easier to read, more efficient in certain situations.
02:28 We’ve already had a glimpse of Pythonic looping. Today we’re going to focus on list comprehensions. These are a tool that are again, central to Python. If you ask many Python programmers, what are your favorite features of Python?
02:44
List comprehension tends to be named very often as one of one of the favorites when you’re one of the top five, if you like. Favorite features. Initially it might seem like it’s just a shortcut for normal for
loops, and in some ways it is.
02:58 But they have a life of their own as well. We’ll have a look at that today.
You must own this product to join the conversation.