Inspecting Other Uses
00:00 In this lesson, you’ll learn about additional uses of the underscore in Python. You’ll be looking at the placeholder variable in REPL sessions. It can be used as a throwaway variable in loops and other constructs, and it can be used as a wildcard in structural pattern matching, right.
00:22
So if you wouldn’t mind moving to your REPL to start with or to your terminal, if you haven’t opened your REPL yet, type python
or python3
.
00:31
And let’s have a simple example. Let’s say 12 + 30
. I think we know that’s 42. Now the underscore is being used here if I enter the underscore, it gives me 42.
00:43 So in the context of a REPL session, the underscore character has an implicit role as a special variable containing the result of the last evaluated expression.
00:55
We can have another example, pow(4,2)
01:00 So yeah, that gives us 16. That’s correct. The underscore now stores 16, so it no longer stores 42 because it will contain the result of the last evaluated expression.
01:13 Another example, where we have a list of numbers.
01:20 The length of these numbers, how many are there in there? That should be four. Okay. Now my underscore therefore holds four. Now I can use that in another calculation.
01:32
So for example, if I sum()
my numbers and I want to divide that by the count, then I can reuse four here, well, it would’ve worked if I would’ve called my list what it actually is.
01:46
It’s not called sumbers
, it’s called numbers
. Okay? numbers
. And I divide that by the underscore, and that gives me two and a half.
01:56 So that is the sum of these. So one and two is three, plus three is six, plus four is 10. 10 divided by four. That’s your two and a half there. That’s the first example, let’s move on to the second.
02:09
In this second example, you’ll be looking at throwaway variables in loops and in other constructs. So you’ll often use a throwaway variable in a for
loop or in list comprehensions where you don’t actually need the used value of the loop variable in any of the computations.
02:27
So you need a loop, but you don’t need the value of the loop variable. So let’s clear the screen, Ctrl + L
, and let’s create a matrix, which is a double list.
02:39
So it’s a list comprehension. So we start with open brackets, and then within there I’m going to have a second list. This is going to be number
02:57
Okay, so that is my first list. So a number for the number in range(5)
. And then I want to do that for each value in range(5)
. And then don’t forget to close the list comprehension.
03:12
So what does that give me? Let’s have a look. So matrix
, so this gives me a matrix of five rows. So you’ve got the first row with 0, 1, 2, 3, 4, and we have that five times or 1, 2, 3, 4, 5.
03:29
So I have a number for a number in range(5)
. So 0, 1, 2, 3, and 4. For a throwaway variable in range(5)
. So I just want to do this five times, but you can see that the loop variable here, we haven’t used in any computation.
03:44 We just wanted to create this matrix, right? Let’s move on to the third example. So the third example talks about wildcards in structural pattern matching.
03:56
Now, structural pattern matching was introduced in Python in version 3.10, and it uses a match
case construct to compare an object to several different cases.
04:07 Python treats a single underscore as a wildcard in a case statement, so it will match anything. So you often use that to alert the user that an object doesn’t have that expected structure.
04:21 As an example, though, you will be building a function, and this function will be a recursive function, and it uses pattern matching to sum a list of numbers.
04:31
So let’s start with that. Clear the screen, Ctrl + L
, and then your function is called sum_list()
, and it takes numbers
as an input.
04:45
Now I need four spaces: 1, 2, 3, 4, match
of the match case, statement or structure. So match numbers
. Now I need eight. 1, 2, 3, 4, 1, 2, 3, 4.
04:59
First case, it’s an empty list. Now I need 12. 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, return 0
. Okay, so if the list is empty, return zero, that makes sense.
05:14
I’m lining up here with case
. So the second case
statement looks at a list and if the first element is an integer or a float, and then we unpack the rest of that list.
05:30
If that is the case, now I need 12, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4. I return first +
05:42
sum_list(rest)
. So this is a recursive function. You can see the recursion here. My third case here uses the underscore, so 1, 2, 3, 4, case _
05:56 colon, and then 2, 3, 4.
06:00
Now I want to raise an error and I actually want to raise a ValueError
with a little note that says, “can only sum numbers.”
06:14
Okay, hit Enter and hit Enter again, and let’s play around with it. So sum_list()
, and let’s give it a list of integers. 1, 2, 3. That’s my list.
06:27 And that adds up to six because one plus two is three, and three plus three is six. So that works. What if I try a list of floats?
06:38 So I’ve got my list, and let’s do just the same numbers, but as floats. So 2.0, 3.0 and the list, close the brackets, and that is indeed six, the float 6.0 in this case.
06:55
And then finally, let’s check what happens if I try to sum_list()
on a list of characters. So I’ll use ‘x’ and let’s say ‘y’. What happens if we try to add those together?
07:11
We’ll get our error message here as expected. There’s a ValueError
because we can only sum numbers, which is what we asked it to return in the all other case if you like.
07:25 There you have it. That’s the end of this lesson. See you in the next one.
Become a Member to join the conversation.