Intermediate Python Tutorial #5 - Lambda Functions
Intermediate Python Tutorial #5 - Lambda Functions
Today's Topic: lambda functions, these are known as anonymous functions as you do not have to define them at the beginning of your program. They operate the same as regular functions but only occupy one line and contain a single expression.
In this set of videos I will be explaining more advanced programming concepts and showing you intermediate-advanced tools that you can use in python. A lot of these tools will help you to solve problems/code more efficiently and will save you a ton of time!
Text-Based Tutorial: https://techwithtim.net/tutorials/python-programming/intermediate-python-tutorials/lambda-functions/
Twitter: https://twitter.com/TechWithTimm
Future topics:
6. Intro to Collections
7. Collections: named tuple
8. Collections: deque
9. Collections: orderedDict
10. Collections: defualtDict
11. docstrings
Want To Support This Channel?
Bitcoin: 1PbkAYLFaJBgjbKn2ptGyBz65xWN8hJgBU
Ethereum: 0xdd42dbbdba60f7163fc7a840e189474b6e8bfcad
Ripple: rD4arM9CVjQWqi8f1kxdpCgkCgEkqBgtud
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Python Tutorials
- Intermediate Python Tutorials
- Lambda
- lambda in python
- anonymous functions
What You'll Learn
This video tutorial covers lambda functions in Python, also known as anonymous functions, and demonstrates their usage with the map and filter functions.
Full Transcript
hey guys and welcome back to the fifth video in my advanced Python programming tutorials in today's video I'm gonna be going over lambda I think that's the way you say it at least excuse me if I'm gonna say around in this whole video which pretty much stands for an anonymous function now these are extremely useful and they don't take up very much space in your program which is really good and I'm going to show you exactly how they work and what they do so I first wanted to start off by defining a regular function again this is one example I've been using a lot I'm just gonna say defined func say like return X plus 5 that's what our function is gonna do it's simply gonna add 5 to the number whatever we give it's alright if I print something like funk of - we should get 7 there we go and we get 7 to the screen now for an extremely basic function like this on oftentimes you don't want to have to write a function definition and take up like two lines of code like this it's easier to write it in a different way and it looks like more smooth maybe more slick in your program and I'm going to show you how to do that so that's one way that we can do it by making a function like that another way and this is using lambda is to do something like this so the name of our function I say func I'll say func - is equal to and then we're going to type our keyword lambda like this you can see it highlights in orange just like our definition key here for our function we're gonna give it a parameter or multiple parameters I think you can use multiple parameters at least and then we're simply gonna state what it returns so the way this works is you type lambda the parameter or multiple parameters which we're gonna try in a second a colon and then whatever value you're gonna return so the thing with lambda is it's used when you have one returned X or one expression in your function so something like this so again I can do return x plus 5 over 4 like I could do a whole expression as long as that expression fits on one line then that's perfectly fine to return and I'll show you how this works so if I say print func - and I just give a number like 9 there we go we get 14 and seven so it works just like a regular function and it kind of just looks like a variable so we're saying function two is equal to an anonymous function that's what lambda stands for an anonymous function with the parameter access simply gonna return X plus five now these are really useful for using with the map function and the filter function which I'm gonna show you later so you don't have to constantly create a new function up at the top of your program and you can also use them inside of other functions so let's let's start by doing that okay I guess a func to simply just gonna copy it and paste it into my other function and then I'm a return func two of x plus 85 let's try that and now if I call my function we can see that we get 92 like so now this again is really useful because you want to oftentimes create another function or use something multiple times within a function but you might not want to like write a new one up here like funk too you just want it to be only used within that function and in that case you would use lambda as it keeps it all contained and have to be top your function you can write a bunch of other many functions that you can then use multiple times within your program it's hard to see the use case for it in small examples like this but they are extremely useful especially in like more in-depth code longer code so now I'm going to show you with multiple parameters so I believe and I'm not sure but we'll see if we can do this so we're gonna try I'm going to make func three is equal to lambda X Y and then somebody to get returning X plus y so let's just see if we call fun and let's give it value like five and five and we'll print that to the screen as well see there we go so that does work as well so you can give this infinitely num an infinite amount of parameters just like you'd be able to do something like this in our function and you can also I believe do optional parameter so you do something like y equals four and then if I put five we should be getting a value of nine here and there we are so you can use optional parameters you can do everything that you'd be able to do with a regular function except you can just return one expression like that okay so let's now use this with our map function and our filter function so in earlier videos I used the map function and to recall what that does is going to create a list quickly say a equals one two three four five six seven eight nine oops nine and ten like that and I'm now just gonna say let's say new this equals list map and then we're gonna have a function in this case I'll just write func for now and then we give it a list right like okay so what I want to do now is I actually want to just put a function in here without having to create a new one up here like I don't want to define func so this is a perfect example where we can use the lambda we don't even have to make a variable like equal to the function anymore we can just type lambda right in here so lambda and there was so X and so X plus five so now we again should simply just be adding five to each of the elements in a and then practice over spread new listed screen now and see if this is indeed working and there you go we can see we start at one so six all the way up to 15 like so again that saves us now having to make a function at the top of our program that we're only gonna use for one specific case which is this map function I again we also don't have to create like func equals lambda up here what you can if you want if you're gonna use it in a different case you can just type it right in the same line as your map function or as your filled with your filter function so this works the same with the filter function of your filter like so it's gonna be our function I guess yeah I believe this works with the fields for me oh yes it does okay so what this is gonna do now is we're just going to return X modulus two equals equals zero so again now given X we're gonna see if it's divisible by two if it is we return true we add that element to the list otherwise we will not be check now we get two four six eight ten and there we go so you can see why lambda is extremely useful it's really cool especially if you want to create a lot of mini fun you want to create functions within functions and say keep everything nice and organized and it's a nice trick to be able to use in Python so with that being said that's been the end of the video today if you guys get in journey to learn something please make sure you leave a like and subscribe to the channel and I will see you again in the next tutorial [Music]
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
Related AI Lessons
⚡
⚡
⚡
⚡
Connecting Frontend to Backend: A Backend Engineer’s Reality Check
Medium · Programming
Build Secure Authentication System Using Access and Refresh Tokens
Medium · Python
5 PHP Features You're Probably Not Using (But Should)
Dev.to · Mahdyar
How to Use the any Type and When to Avoid It
Dev.to · Krunal Kanojiya
🎓
Tutor Explanation
DeepCamp AI