https://bit.ly/2G41HLK
Welcome to 60 seconds to code where I show you how to code in 60 seconds or less.
Today you will learn about lambda functions. Lambda functions are anonymous function that you can optionally name. They can be used to write quick functions that require a simple return value. One common use of Lambda functions is for sorting and filtering lists.
Step 1- Create a file called lambda.py and add a list of founding fathers in the file.
Step 2 – In line 2 type get_last_name = lambda name : name.split()[-1]. This lambda function will split the given input string based on spaces and then use a negative index to return the last item in the list.
Step 3 – In line 3 type print(get_last_name('John Adams')).
Step 4 – in line 4 type founding_fathers.sort(key=get_last_name). The key parameter in the sort function accepts a function that returns a value to be sorted on. In this case, for each item in the list, the get last name lambda function will return the last name of the founding father and the sort function will sort the list based on that last name.
Step 5 – Type print(founding_fathers) on line 5 and run the file in the python interpreter.
The code is available in a link in the description.
Congratulations, you have learned about lambda functions. For your homework, make a modification to the lambda function such that it does not run in to problems if someone forgets to capitalize the last name of a founding father.
Learn #lambda functions in #python #100DaysOfCode #301DaysofCode #60secondstocode
https://bit.ly/2G41HLK
On this page of the site you can watch the video online Lambda Functions in Python - 60 Seconds to Code with a duration of hours minute second in good quality, which was uploaded by the user Faisal Akhtar 01 January 1970, share the link with friends and acquaintances, this video has already been watched 31 times on youtube and it was liked by 0 viewers. Enjoy your viewing!