Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Multiple Expressions in lambda

00:00 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,

00:37 I can say “I’m going to create that in a lambda.”

00:41 I want to take x and I want to add 1 to it. Now, writing a function to add 1 to a number isn’t terribly complicated, just trying to give you an example of how a lambda is written.

00:54 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:18 Now I can actually give this a name—

01:22 I’m going to call it incr (increment). It takes x and adds 1 to it,

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.

01:47 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.

02:06 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 hello and 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 "Hello" and "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.

02:46 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 hellolet 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 w, "World".

03:23 And so if I call lambda_func(), Python knows that it’s expecting two parameters. The first one I’ll say "Hello", and the second one I’ll say "World".

03:40 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 False value.

04:02 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 true_func() and false_func() functions to see short-circuit evaluation, it actually has to evaluate this.

04:22 It has to perform all of its tasks before it can determine whether it’s True or False. So the first thing it has to do is it has to print 'Hello'.

04:32 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.

04:49 And so we evaluate the right-hand operand. To do that, it has to carry out the function, so it prints 'World' and then it returns that, but there’s nothing to do with that return value.

05:04 In fact, if I tell it to print the result of lambda_func() with "Hello" and "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 False, print() evaluates to False and it just gives us that evaluation of None.

05:36 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.

05:54 In the next lesson, we’ll wrap up everything we’ve done in this course.

Become a Member to join the conversation.