Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:48Length:7.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

6.6 Lambdas

Lambda functions, also known as anonymous methods, are not new or unique to the Python language, but they sure are useful. In this lesson, I'll show you once again how to make your code more concise by combining this new concept of lambdas with other functions like map or filter.

1.Introduction
2 lessons, 11:32

1.1
Introduction
02:03

1.2
Prerequisites
09:29

2.Python Building Blocks
6 lessons, 1:08:07

2.1
Introduction to the Interpreter
09:51

2.2
Numbers
10:53

2.3
Strings
14:36

2.4
Lists
11:33

2.5
Standard Input and Formatting
12:00

2.6
Building a Tip Calculator
09:14

3.Controlling the Flow
7 lessons, 1:20:10

3.1
Conditional Statements
12:47

3.2
Looping With For
09:36

3.3
The Range Function
10:41

3.4
Looping With While
13:06

3.5
Creating Functions: Part 1
11:55

3.6
Creating Functions: Part 2
08:49

3.7
Building an Average Calculator
13:16

4.Common Data Structures
4 lessons, 46:49

4.1
Lists, Stacks, and Queues, Oh My!
11:45

4.2
Dictionaries
10:10

4.3
Iterating Data Structures
09:41

4.4
Building a Sentence Analyzer
15:13

5.Application Structure
7 lessons, 1:15:12

5.1
Modules
09:08

5.2
Packages
11:24

5.3
Classes
09:53

5.4
Attributes
09:34

5.5
Methods
12:01

5.6
A Special Calculator: Part 1
13:36

5.7
A Special Calculator: Part 2
09:36

6.Collections
7 lessons, 46:55

6.1
What Are Comprehensions?
06:32

6.2
List Comprehensions
06:08

6.3
Dictionary Comprehensions
06:38

6.4
Map
05:45

6.5
Filter
06:31

6.6
Lambdas
05:21

6.7
Generators
10:00

7.File I/O
6 lessons, 48:51

7.1
File Basics
06:50

7.2
Reading Entire Files
07:49

7.3
Navigating a File
08:32

7.4
Writing to Files
07:22

7.5
Reading and Writing to Files
09:15

7.6
Reading and Writing Complex Objects
09:03

8.Networking
5 lessons, 43:48

8.1
Introducing the Socket
04:39

8.2
Getting a Remote IP Address
06:42

8.3
Handling Socket Errors
07:58

8.4
Create a Socket Server
16:04

8.5
Create a Socket Client
08:25

9.Connecting to Network Services
3 lessons, 34:27

9.1
Getting the Current Time With NTP
10:38

9.2
Getting Websites With HTTP
12:57

9.3
Downloading Files With FTP
10:52

10.Conclusion
1 lesson, 02:08

10.1
Goodbye
02:08


6.6 Lambdas

So it's time to be a little honest with ourselves here. And in a few of the last lessons, when we were talking about things like the map function or the filter function, we kinda were dealing with some functions that were maybe a little less than sweet. And one of the examples here that we were using was this sweet function that took in a value of x and it multiplied by two and then it squared that value and then return it back out. Now odds are this is not a very useful function. And if you start to think about the definition of a function, a function is really something or a group of functionality or a set of functionality that is reuseable that you're gonna reuse in different parts of your application that you might find use for in different areas. But, in all honesty, this function isn't very sweet. There's really not a whole lot of reusability here. Just because it's a very contrived example. So what's the point of creating a function and wasting space by defining this function that we can use in other places that odds are we're probably not gonna use anyway. So, what can we do about that? Well, so now is a good time to introduce the concept of a lambda. Now the concept of a lambda is not unique to Python. It's not a new thing, it's been around for a while. You may have heard of it as a lambda expression or maybe an anonymous function or things along those lines where basically the concept is you're able to define the equivalent of a function A without a name. So you don't have to store a name to be able to pass this function around. You're really just going to be able to use it in a single spot, and B, you can define it in line. Which means you don't have to declare it somewhere else like we're doing here. We're defining this function here suite, and it's somewhere else in a file that's living somewhere that you might have to import in your python file or something like. I can just define it in line. So let's revisit our example where we were using our map function, and let's do the same thing we're doing here, but let's get rid of this suite function because in all honesty, it's not very helpful. So how can we get rid of that but still have this line function in the same way or at least in a similar way? So now we're going to use a lambda. So the way that we do that is we specify the keyword lambda. Now once I've done that, that means that tells Python that I'm about to define an anonymous function that has inputs, that has outputs, that does what you would typically figure to be a defined function, but it's going to be done in line. So the way that I do that is I specify my input. So it would be X in this case, since our input here is X. Then we specify a colon. And then after that, we would specify what the body of the function would be. And in this case, with a lambda we're talking about a single line. So we're not talking about being able to take very complex multi-line functions and converting them into lambdas, because in that case it's not really going to work. We have to specify single line or one liners, if you will. So in this case, we want to return, and because it's a single line we don't have to actually use the return keyword. Our body of our lambda or of our function here is gonna be this right here. So let's go ahead and copy that and we'll put it down here. So this is our lambda, this whole thing right here is an anonymous function. It's a lambda that I can then put in line anywhere within my application that references suite and we should have the same exact functionality. So all I'm gonna do here is I'm going to copy this line just so you can see it side by side. And then I will take this lambda here and I will cut this and I will paste it in here where I reference my sweet function, and that's it. So as you can see, any place that takes in a function that's predefined somewhere else can also accept a lambda. So now if I save this, and I'll come back over to my command line, and I'll go ahead and run Python lambda. If I run that, you should see the exact same answer. I see the same values coming back, which means I can replace all of this with this one single line here. So I can delete all of this. Get this down to one individual line, I can save this. And I'll go ahead run it again and I'll get the same answer. So as you can see, using a powerful part of the Python language, the lambda expression or an anonymous function, I can now take what used to be a predefined function that maybe wasn't that reusable that I wasn't using around in a bunch of other places. And instead of wasting space on defining that somewhere when I'm not going to reuse it, I can define it in line using a lambda anywhere in any function that accepts another function. So it's a very cool way to condense your code and to keep things as in line as possible, and not take up a lot of space with things that you're just not gonna reuse.

Back to the top