Lambda Functions in Python - 60 Seconds to Code

Publié le: 01 janvier 1970
sur la chaîne: Faisal Akhtar
31
0

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


Sur cette page du site, vous pouvez voir la vidéo en ligne Lambda Functions in Python - 60 Seconds to Code durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Faisal Akhtar 01 janvier 1970, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 31 fois et il a aimé 0 téléspectateurs. Bon visionnage!