Multiple Expressions in lambda
In this lesson, we’re going to take a look at one last non-Boolean context for using Python’s
or, and it’s going to allow us to perform a
lambda operation on multiple expressions. Now, if you’re not familiar with lambdas, let me give you a brief overview.
00:18 Basically, it allows you to define a function—sometimes something we only need to do once—without using the typical notation and to have to give it a name. So, for example, if I want to create a function that simply increments the value of a number,
So, I write my function that I just want to apply once, and then I provide the number that I want to apply it to. And so this is going to take
3, put it in place of
x on the right-hand side of the colon (
:), and it’s going to give me the value of
4. A one-time, quick and dirty operation.
01:32 and then I can actually call that like a typical function. So, it’s a different way to define a function, and it’s one aspect of an area of programming called functional programming which has been implemented in Python.
But here is the cool thing that you can do. You can write a
lambda which operates on more than one variable. Typically, we just have it operating on
x, but here is an interesting way to write a
lambda to perform two actions.
I have this in the slides about what’s happening, but I think it’s more fun to actually see this get called. So my operation is,
lambda is going to take
world, it’s going to print
hello, ending with a space and not a newline character, or it’s going to print
world, and then we’re going to apply this to the phrase
"World" and get the very first program a lot of people write when they are learning programming for the first time or for a new programming language. It’s one of the first programs that they write, but typically not like this.
So, I want to give this a name so I don’t have to type it over and over again, so we’re going to call this
lambda_func = lambda, which is going to take
hello—let me just call it
h for the first parameter and
w for the second parameter because I don’t want to do all of that typing. It’s going to print whatever
h is, which will be
"Hello"—and we’ll want it to end with a space (
' ') because we want the next word to appear on the same line—or we’re going to print
And it worked! What happened here? Well, it takes advantage of the fact that the print statement returns
None when it needs to have its value determined and that
None is interpreted as a
So, we have this
or statement, which needs to be evaluated. Python evaluates the left expression first. It’s a
print() function call, so it actually has to carry out the
print() function, it has to do that function. Just like we saw the
false_func() functions to see short-circuit evaluation, it actually has to evaluate this.
When it finishes that, it then evaluates. The
print() function returns
None, which evaluates as
False, which means the
or operation now needs to simply do the second part and return the second part.
In fact, if I tell it to print the result of
"World", it prints
Hello World, which is what the print statements were doing. But since the
or operation returns the value of the second operand when the first operand is evaluated to be
print() evaluates to
False and it just gives us that evaluation of
So, there is a way to leverage the fact that the first expression is always evaluated, no matter what it takes to evaluate it. And if it’s
False, the second expression is always evaluated and returned no matter what it takes to do that evaluation.
Become a Member to join the conversation.