Тёмный

Python LAMBDA FUNCTION?! #python #programming #coding 

Подписаться
Просмотров 1,6 млн
% 90 947

This video shows a quick illustration of what lambda functions are in Python. These are also referred to as anonymous functions.
Background Music:
Rain, Book And Cup Of Tea by | e s c p | escp-music.bandcamp.com
Music promoted by www.free-stock-music.com
Attribution 4.0 International (CC BY 4.0)
creativecommons.org/licenses/by/4.0/

Опубликовано:

 

15 янв 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 672   
@b001
@b001 7 месяцев назад
Yes, this video is slightly sarcastic. I would never use it to calculate 1+2. I used an extremely trival problem so that I could better demonstrate the syntax of lambda functions.
@MrWayneDX
@MrWayneDX 7 месяцев назад
That made me understand this less. Why would you do that when you can just print(X+Y) or print(1+2)?
@xinaesthetic
@xinaesthetic 7 месяцев назад
​@@MrWayneDXI think the video would've been better if it showed how it can be used when mapping over an array or something.
@muhammadqaisarali
@muhammadqaisarali 7 месяцев назад
Damage is already done. U can recover you honour now. 😊
@bruhstandler
@bruhstandler 6 месяцев назад
@@MrWayneDXYou wouldn't but there are a lot of other instances where it would be easier to define a function and use it all in one line instead of wasting 3 lines of code on it.
@OneWeirdDude
@OneWeirdDude 6 месяцев назад
Ah, okay. How about a serious one, then?
@heddevh
@heddevh Год назад
Can you make a tutorial for 1+3 next?
@Jefferson31
@Jefferson31 Год назад
​@Darth Vader and 3+1?
@andreab5185
@andreab5185 Год назад
​​@Darth Vader No need. Already written my code and It says: 31
@youber3200
@youber3200 Год назад
this is how i calculate 1+3 def add(x, y): sum=0 for i in range (x): sum+=1 for i in range (y): sum+=1 return sum print(add(1,3))
@heddevh
@heddevh Год назад
@@youber3200 that's so cool it worked! There might be a small efficiency problem though, so do you think it could manage 1+4?
@andreab5185
@andreab5185 Год назад
@@youber3200 There are too few operations and lines of code, this can't work.
@aceplayer555
@aceplayer555 Год назад
Lambda functions are just like regular functions, except they make your coworkers hate you.
@taiwo_the_illusive
@taiwo_the_illusive Год назад
Haha why so? I’m new to programming
@aceplayer555
@aceplayer555 Год назад
@@taiwo_the_illusive They're just significantly more difficult to read and understand if you're not used to them.
@hyde4004
@hyde4004 Год назад
They're not hard to understand if they're used for what they're supposed to be used for, simple and short expressions that can be easily expressed in line.
@icakinser
@icakinser 11 месяцев назад
I had to follow-up a guy that LOVED them. I swear every other line was a LAM
@samcolton943
@samcolton943 11 месяцев назад
so do you really only use these in specific use cases, or just try and avoid them? Or is it just personal preference? Just out of curiosity of course, I'm in the networking field but I'm teaching myself to code.
@moustachemewe
@moustachemewe Месяц назад
I swear someday someone is going to figure out a Python one-liner that attains world peace but no one is going to use it because its "not easy to read"
@timilehinorawusi
@timilehinorawusi Год назад
Note : lambda has many use cases , but key places to use lambda are in amp and filter function please learn about map and filter and how to use lambda in them . Very useful !! 😊
@ChannelCheesecake
@ChannelCheesecake 7 месяцев назад
It’s just easy bro
@givrally7634
@givrally7634 5 месяцев назад
Max, min, and sorted as well.
@user-wr2cd1wy3b
@user-wr2cd1wy3b 3 месяца назад
Can you 'splain main
@givrally7634
@givrally7634 3 месяца назад
@@user-wr2cd1wy3b Both of the above functions, as well as min/max/sorted, take in an iterable (list, tuple...) and a function, and apply that function to each element of the iterable. map returns the result, while filter returns an iterable with the unchanged elements, but only those where the function returned True. As for min, max, and sorted, their optional "key" parameter applies the function to each element and then works with the resulting values instead of the original values. If you don't use lambda, you have to create a new function every time you want to use them. For example, if you want to sort a list of 2-tuples based on the 2nd element, you have to create a function def f(x): return x[1]. That wastes 2 lines, 4 if you comply with PEP8, just so you can write "key=f". If you use lambda, you can create that function in an "anonymous" way, meaning you don't give it a name or anything, it just does something simple. In this case, you could write "key=lambda x: x[1]" and you'd be done. Assigning a name to it is possible, you could write "f = lambda x: x[1]", but it's not recommended in most cases. This makes things so much more readable in functions, and makes them so much more powerful.
@pabs4516
@pabs4516 2 месяца назад
I agree my friend.
@elgatorado4907
@elgatorado4907 Год назад
My professors teaching me Lambda as if I'm not going to just use regular functions anyway
@philippebaillargeon5204
@philippebaillargeon5204 Год назад
Your teacher is teaching you that because you will eventually need it. Lambda are essential to keep a clean code and improve readability. Any good programmer knows how and when to use lambdas.
@elgatorado4907
@elgatorado4907 Год назад
@@philippebaillargeon5204 I know, I understand that now. I was just joking about my attitude back then. I understood how to write functions and recursive functions and even did it well but I was so hung up on thinking that Lambda was a waste of time and an insult to all the effort I put into everything that did the same thing. I'm a little more experienced and greatly more appreciative now
@64imma
@64imma 8 месяцев назад
I'm mainly learning game development, so for me, lambda functions have been useful mostly for me in being able to pass a function that requires parameters in places where it's normally not allowed.
@gottem_
@gottem_ Год назад
Damn, I thought it would write the whole Half-Life code for me.
@so_ma_music
@so_ma_music Год назад
WOAHHH thank you!! Just started learning Python recently and lambda definitely was a weird one to learn for me personally, but this all makes sense now 🙏
@vorpal22
@vorpal22 Год назад
Lambdas in Python aren't great... you can actually assign them to variables, though: my_lambda = lambda x, y: x + y Then you can call it like any other function: my_lambda(1, 2) Most programming languages support multi-line lambdas and they're essential in functional programming. They're just anonymous functions that you don't need to declare and keep around, and if you have something like Kotlin, they are extremely powerful.
@originaljokester7679
@originaljokester7679 Год назад
Hey I just started learning python too, do you have any advice about indentation? I get the basics of it but sometimes it gets confusing. Is there a rule or pattern?
@vorpal22
@vorpal22 Год назад
@@originaljokester7679 The indentation in Python is absolutely required to delineate code blocks because there are no braces like other languages. Foe xample: if cash > 0: pirin('You do have cash!" else: print('Poor as dirt...." If you don't insert the indentitation (usually four, sometimes two if you want), then Python doesn't know how to process a block.
@coldenate
@coldenate Год назад
Dude, this is the best use case of short form content…. learning!? Brilliant 👏👏
@hhill5489
@hhill5489 Год назад
Using lambdas along with map to perform fast operations on lists is what got me into them
@Kadori328
@Kadori328 Год назад
If I see a junior developer do this just know I'm breaking your keyboard
@dangdrjay3011
@dangdrjay3011 Год назад
Why
@jacoL8
@jacoL8 8 месяцев назад
@@dangdrjay3011because they don’t understand what the value of anonymous functions is and they have anger issues…
@synsez
@synsez Год назад
Thanks!
@isoceptic
@isoceptic 7 месяцев назад
'lambda' half life 2 flashbacks
@reeti5958
@reeti5958 5 месяцев назад
Word of advice, if you are making a video about lambda functions, actually explain its use case instead of just writing 1+2 to show that you can do it like that. Lambda functions are really important and useful functions which allows us to pass values to other functions by implementing it in a single line. You won't understand its usecase in web programming but it totally make sense in application programming. I always end up using them in my kivy click event code because kivy doesn't let me pass the values directly or passes a reference that I don't need in my listeners. Thus wrapping the listener call with lambda functions helps to keep code compact and understandable.
@Zxv975
@Zxv975 Год назад
The core use of a lambda is explained in the video, but I'll just explain a bit further for anyone confused. Functions are good programming practice because they group chunks of similar code (closure) and they allow for reuse by referring to the function's name later in the code. But what if you want the benefits of closure but you don't want to reuse a piece of code? You would then use an anonymous function which has no name and therefore can't be referred to elsewhere in the code. Lambdas are pythons version of anonymous functions.
@Phasma6969
@Phasma6969 Год назад
wow it makes sense ayo
@AJ.911
@AJ.911 11 месяцев назад
So if I want to pass new inputs I have to define a new lambda line?
@Zxv975
@Zxv975 11 месяцев назад
@@AJ.911 if you're going to be reusing it, then you probably want a named function and not an anonymous function.
@kentagent6343
@kentagent6343 7 месяцев назад
Lambda is what they're called in Haskell and Java too.
@BearfootBrad
@BearfootBrad 7 месяцев назад
More python shorts please! This is so helpful
@alkebabish
@alkebabish 11 месяцев назад
Annonymous functions are popular in JavaScript, where they enjoy being wrapped around eachother in an endless web of confusion.
@timber2lease
@timber2lease 8 месяцев назад
in js, anonymous functions is something different
@hueman4927
@hueman4927 8 месяцев назад
@@timber2leaseI’m pretty sure that Anonymous Function = Arrow Function (js) = Lambda (python) But there are probably some scoping differences here and there, idk
@matiasalexannder
@matiasalexannder Год назад
There is no way that you make me understand a concept that took me like 2 hours to parcially understand in just 30 seconds... thanks dude
@nopenevermind8559
@nopenevermind8559 Год назад
I take the first one, the easiest. 🤣
@AndrewMycol
@AndrewMycol Год назад
I myself never really understood lambda functions, until I watched this video and it clearly explained it in a concise way with a rather simple example. Very good job!
@andreysankov2948
@andreysankov2948 Год назад
try to code lamda funcrion in other languages and you will understand why its so cool and easy to use. Unfortunatly python does not represent lambda fucntion like in other languges. In python its just boring and useless shit.
@TheReesesGuy
@TheReesesGuy Год назад
This was so helpful. I have never understood Lambda
@TheBigQQ69420
@TheBigQQ69420 Год назад
you still dont if you think this is how to use these
@tantalus_complex
@tantalus_complex Год назад
Just think of them as the simplest possible kind of function. Assign one to a variable (like any other value) and you have a small reusable function. Why do they exist? In Python, mostly just as "syntax sugar" - a concise way to write tiny functions. They aren't necessary. You could always define a normal function instead. But for tiny functions, it's nicer to have a tiny syntax to go along with them.
@tobennaokoli4450
@tobennaokoli4450 15 часов назад
I wasn’t used to lambda functions until I learn JavaScript. And honestly… they make everything so much easier and quicker.
@apalsnerg
@apalsnerg 8 месяцев назад
This was a much more succinct explanation than I've heard elsewhere. Thank you.
@thepenguin9
@thepenguin9 6 месяцев назад
Instructions unclear, caused a Recursion Cascade and now I have to save the facility
@TheAleBecker
@TheAleBecker Год назад
wow! one of the few programming shorts that is actually useful and very well explained. Good job!
@beneji2404
@beneji2404 7 месяцев назад
You my friend just summed up the whole python philosophy!
@FappimusPrime
@FappimusPrime 2 месяца назад
Solid prank to pull on my professor before my next homework is due 😂🤦🏼‍♂️😭
@citrusgamingYT
@citrusgamingYT Год назад
Thanks for posting. I know many others who start out with Python and struggle with understanding lambda functions.
@utkarshpandey2375
@utkarshpandey2375 Год назад
Please make shorts on other python topics..Loved this one.
@rameshnaveen6964
@rameshnaveen6964 11 месяцев назад
The best and easiest way i ever saw someone explain lambda. ❤. Thanks mate
@fdsdark
@fdsdark Год назад
actually really helpful, been confused on stuff like lambda and using classes for a while, was able to easily understand. :D
@vinnieg6161
@vinnieg6161 Год назад
as someone quite new to programming it just looks like you made a simple 1+2 into an overly complicated quantum physics problem
@kipchickensout
@kipchickensout 7 месяцев назад
as someone not new to programming, yes he did
@epsi
@epsi 6 месяцев назад
names = ['George', 'David', 'Sam', 'Rob'] names.sort(key=str.upper) Sorts by name in a case-insensitive manner. If you only want a partial sort based on the first letter of a given name, instead of the entire name, you can use a lambda that returns the first letter, optionally applying str.upper() to that letter: names.sort(key=lambda s: s[0].upper()) Lambda in Python is intended to be used for that kind of thing - a short, uncomplicated function you only need for one particular use case. You might use it to sort even numbers before odd numbers for whatever reason: nums = list(range(10)) nums.sort(key=lambda n: n%2) The demo was a bad example, intended to illustrate that it's the same as a function, forgetting to illustrate why you might prefer it over a named function in the first place.
@Hloredea
@Hloredea 11 месяцев назад
bro really using lambda to slove 1+2 💀
@Boomk27
@Boomk27 Год назад
Can’t wait for the useful case video, thanks mate !
@JordanMetroidManiac
@JordanMetroidManiac Год назад
I use lambda frequently when providing the “key” keyword argument of sorted() or list.sort(). Sure, you could define a function specifically for a special sort, but if you know you will only ever use it on one line of code, you might as well keep that sorting logic all on one line of code.
@GuizinPE81
@GuizinPE81 Год назад
he is showing things in useless cases to make it easier to explain, you when you are writing code you will need to know how to use the things you learn in a useful way
@Boomk27
@Boomk27 Год назад
@@GuizinPE81 yeah mate, reason why I wrote that comment 💪🏽💪🏽💪🏽
@Boomk27
@Boomk27 Год назад
@@JordanMetroidManiac in a dict for example ? Can you be more specific ? (I’m a noob over here hahaha)
@tylerdurden4285
@tylerdurden4285 Год назад
Keep making these videos. This was really helpful and simple to understand.
@abrotheryourbrother3429
@abrotheryourbrother3429 7 месяцев назад
Whenever I think of lambda function in Python, I think of anonymous function. So lovely to use, yet takes a good amount of practice to master.
@Laittth
@Laittth Год назад
it would've been good to show a real use case here
@prub4146
@prub4146 3 месяца назад
I love this content. It is pleasant and simple and helps remember useful stuff. Please keep making more. Lots of programmers ego can be seen in the comment section, which should be ignored.
@AUGamerTV
@AUGamerTV Год назад
Liked and subbed. Cool little explanation!
@Tenkite001
@Tenkite001 5 месяцев назад
Him : it's not funny with one line Also him : IT TAKES 3 LINES OF CODE
@Dividendology
@Dividendology Год назад
Great explanation. Thanks!!
@voxj.
@voxj. 2 месяца назад
Incredible. I'm all ears for 1-2!
@TimeManInJail
@TimeManInJail Год назад
Cool, something new I learned today. I heavily rely on this channel
@naniv
@naniv 6 месяцев назад
Love your videos bro
@engine_man
@engine_man 11 месяцев назад
Python discovers anonymous self invoking functions
@shakthiviviyn3528
@shakthiviviyn3528 Месяц назад
Amazing video ❤
@MinuteRecapped
@MinuteRecapped Год назад
Great explanation... But whats the point?
@dham3007
@dham3007 Год назад
great explanation
@swizice
@swizice Год назад
I cannot begin to say how well done and concise this explanation is…you, sir have won the month of January! 🎉
@sshady675
@sshady675 2 месяца назад
An idiot admiries complexity, a genius admires simplicity. ~ Terry Davis
@intrepidzephyr
@intrepidzephyr 7 месяцев назад
This music bops
@WhyOhWhy123
@WhyOhWhy123 7 месяцев назад
Great. Nice music too
@TurdFerguson43
@TurdFerguson43 8 месяцев назад
Wow so this is good for when I need to waste time! Excellent! Thank you!
2 месяца назад
nice, simple, beautiful
@rusirumunasingha2234
@rusirumunasingha2234 Месяц назад
Brilliant!
@Hillo372
@Hillo372 7 месяцев назад
That's soo exhilarating
@mohamedcoufi9873
@mohamedcoufi9873 16 дней назад
That s what I needed to know, thx.
@Apr0x1m0
@Apr0x1m0 Год назад
Finally, thanks. Now I know what lambda is and understand how to use it.
@fezkhanna6900
@fezkhanna6900 Год назад
Awesome! Very clear
@flowrling
@flowrling 6 месяцев назад
i dont code in python much (or at all) but i do make spreadsheets and ive seen the =LAMBDA function but never understood it so this was actually useful
@returnnull3476
@returnnull3476 8 месяцев назад
Python triggers my fight instinct
@adalbertpayan811
@adalbertpayan811 Год назад
Thank you for your videos 😊
@PS_Tube
@PS_Tube 6 месяцев назад
The first time I found a lambda function in Python, I was amazed. 🐍 is ❤️
@dipeshsamrawat7957
@dipeshsamrawat7957 Год назад
Excellent ❤
@0verflowSec
@0verflowSec Год назад
Thank you for your video
@quelloliciccionedidoraemon4790
Yeah but everytime I have to re-use that function I have to type lambda x,y: x + y instead of just typing sum(1,2)
@Meaty33
@Meaty33 Год назад
or you could type print(1+2)
@explosingpigman2251
@explosingpigman2251 8 месяцев назад
but what's the fun in that?
@seanmcghee2373
@seanmcghee2373 7 месяцев назад
I am fairly new to lambda functions after having coded for around 47 years. They're...cute.
@araptrap
@araptrap 11 месяцев назад
Made it look so easy
@AminalCreacher
@AminalCreacher Год назад
ooh reminds me of arrow functions in javascript
@islamalmsarrhad2152
@islamalmsarrhad2152 9 месяцев назад
You're my hero 😮.. Finally I got it 🎉🎉🎉
@matrikomatriko
@matrikomatriko Год назад
Is it really good though to explain how lambdas work by showing their worst use case? They generally should be used when calling higher order functions. I understand it might be confusing for beginners, but if they don’t know about higher order functions, then they don’t have to know about lambdas. Here’s hoping we don’t get lots of beginners just throwing in lambdas wherever they can
@davidgrown4067
@davidgrown4067 Год назад
Very happy to know that
@aythanraherisoanjato3056
@aythanraherisoanjato3056 Год назад
If you want a named function without writing "return" for a reason, use this syntax: functionName = lambda params: return With functionName being the name of the function, params the parameters (a, b=0, *c, **d) and return is what is returned by the function For example with the function "add" taking 2 numbers as arguments and returning their sum: add = lambda x, y: x+y
@muhammadkamil3558
@muhammadkamil3558 2 месяца назад
Lambda helps a lot when working with group by operations in dplyr.
@Leeon_King
@Leeon_King Год назад
Thank you for this! Currently learning python.
@arisbaur
@arisbaur 4 месяца назад
The only ever usecase for those i saw in the map-reduce algorithm
@oschwald9784
@oschwald9784 Год назад
wow now I understand how useful it is...
@caspermoon6052
@caspermoon6052 Год назад
Thank you!
@hidude1354
@hidude1354 7 месяцев назад
major use case of lambdas: assume you have multiple lines of codes which take similar inputs and applies a function to them in an abstract manner. you can now create a function which takes a lambda as an input and simplify your code. i.e. to build a simple calculator: def main(): x = float(input("first number: ")) y = float(input("second number: ")) operation_type = input("choose one from: add, sub") add_lambda = lambda x,y: x + y sub_lambda = lambda x,y: x - y if operation_type == "add": chosen_operation = add_lambda elif operation_type == "sub": chosen_operation = sub_lambda print(calc(chosen_operation, x, y)) def calc(operation, x, y): return operation(x,y) of course this is a low-level example, but it shows the possibility to remove code redundancy in more complex programs
@pikachuofficial69421
@pikachuofficial69421 24 дня назад
They are like arrow functions in js, mostly passed as callbacks
@ryankhart
@ryankhart Год назад
You just demystified a concept that I keep having to look up every time I encounter a lambda in someone else's code in a way that the not even GeeksForGeeks could do for me.
@vorpal22
@vorpal22 Год назад
Just think of it as an anonymous, unnamed one line function.
@ryankhart
@ryankhart Год назад
@@vorpal22 Well, I don't tend to use those in my code yet, so that explanation still wouldn't have helped me in the past.
@WilburJaywright
@WilburJaywright Год назад
I can imagine this would be useful if I’m running a large operation on values that are in a data table or something and I don’t want to have to keep looking them up or assigning them to new variables just for the one operation.
@amiganer681130
@amiganer681130 Месяц назад
Even a old C64 had that called "Def". You showed a unnamed lambda, so it can not be reused. You can say "A=lambda..." the A is more like a function. Sometimes you need to use function, so you can put it in a lambda.
@spearmintlatios9047
@spearmintlatios9047 8 месяцев назад
This does seem a little extra but it’s just the most basic of use cases for these. There are actually many uses to lambda functions, for example sorting by a certain attribute in a class with the .sort() function, or passing a function as an argument into another function. For example, if you had a list of 100 names and you needed to change them in several different ways, you can make a function: def permute(list_names, func): for i in range(length(list_names): func(list_names[i]) Then you could pass a lambda function as an argument into the above function. This use case is also a little silly in terms of effectiveness but it is more like where you’ll actually see them
@mrjebum123
@mrjebum123 Год назад
In python I find I use lambda the most when I need to pass parameters to button commands in things like tkinter userforms.
@beepbeepgamer1305
@beepbeepgamer1305 Год назад
This is interesting, I'll try to figure it out on where it can be used.
@waffle8364
@waffle8364 8 месяцев назад
for their practical use, they are good for callbacks.
@mohammadalmoqdad7377
@mohammadalmoqdad7377 Год назад
amazing!
@chiebidoluchinaemerem5860
@chiebidoluchinaemerem5860 Год назад
To me lamda is like arrow functions in javascript
@bhavishshetty5332
@bhavishshetty5332 Месяц назад
If u every feel useless then thing again there is lambda in python 😂
@studyaccount4251
@studyaccount4251 Месяц назад
it’s very useful actually
@RIP123
@RIP123 Год назад
What is this font? Looks gorgeous
@morsuk
@morsuk 6 месяцев назад
It's very often used in tkinter to pass arguments on command functions
@maighe_tv2848
@maighe_tv2848 6 месяцев назад
I love python because you just write lambda instead of having some weird specific notation for it
@alexlynpi
@alexlynpi Год назад
This escalated quickly 🤣🤣🤣
@Desh_O_Bangla
@Desh_O_Bangla Месяц назад
I am not coding person , I learnt C , c+ when I WAS in polytechnic college, it was just 6 month of course besides my main course, But now I am in love with this python.
@EgeQaqd
@EgeQaqd 7 месяцев назад
When I made buttons with Tkinter I found out that I need lambda to use a command with parameters
@AdityaKumar-dv9cp
@AdityaKumar-dv9cp 11 месяцев назад
Its been months and i have never understood LAMBDA.. But this short vid.. Made it. Possible
@thibw638
@thibw638 Год назад
Amazing
@noname67250
@noname67250 7 месяцев назад
I used lambda very much when using TK library.
@Myra.1
@Myra.1 Год назад
bro thanks before this i didnt understand how lambda functions work