Lambda Functions
Lambda Functions
11 pm. I'm on the train home from London, having met up with my little sis.
Should I do some work? No I don't work at this time.
Should I read? I can't at this time.
I know. I'll learn some AI. So currently I am taking two foundational modules that my AI engineer colleagues have recommended. Then I can do the full AI/ML career path.
A wind farm in Cornwall that my wonderful sister sent me. |
Lambda Functions for Pandas
"Learn to define a Python function in one line."...
Sounds like JavaScript. Which I miss. Only because I love it so much - don't remind me. I miss it every day. Anyway I love Python too...
So this is my attempt at writing a lambda function:
age_in_dog_years = lambda age: age * 7
And this is attempt number two:
say_hi = lambda name: print('Hi ' + name)
Okay not so bad for someone who is writing at midnight on the train on the train from London for today having worked all day.
An if statement inside of a lambda function...?
Okay so what if I want to put my if statement inside of a lambda function? then what do we do then? thanks.
OMG I LOVE THIS SYNTAX. THIS IS SOME AMAZING SYNTAX OMG. OMG.
So:
is_it_my_birthday = lambda birthday: 'YES, IT'S MY BIRTHDAY' if birthday else 'Not today, no, sadly...'
Note the use of the keyword lambda.
The syntax is so cool but it is impossible as far as I know without the use of the keyword lambda.
Comments
Post a Comment