Lambda Function in Python

preview_player
Показать описание

^Downloadable code & more! Lambda functions can be used with arrays, if statements and much more. This is a quick tutorial on some of the things you can do with one line lambda functions in Python

This is a Python anaconda tutorial for help with coding, programming, or computer science. These are short python videos dedicated to troubleshooting python problems and learning Python syntax. For more videos see Python Help playlist by Rylan Fowers.





LAMBDA FUNCTIONS

I’m going to show you a quick easy way to create one line functions in python

These are particularly helpful for computational functions

to create a lambda function type the name of a variable, (we will do f) equals, lambda, a name of another variable (we will do x) colon, then use the second variable to create a function. In this first example we will do x squared

Now to call the function we write the name of it with a value in parenthesis to plug in as x

So 5 squared is 25 and 3.22 squared is 10.368…

In this next example we create a lambda function with an if statement

So our function will be 1 or -1 depending on the value we give it. This has endless applications when coding.

For our last example we will import numpy

This is a three dimensional example. We define the function the same way, only this time when we write the actual function part we treat x as if it is a vector with three values. x[0] is the first entry of the vector

This function will sum the values of our vector, and require that we put a vector of size 3 in

Lambda functions can also be plotted with marplot lib, check the description for a video on how to plot lambda functions

So there you have it, that is how you use lambda functions in python

#PythonMarathon #LearnPython #PythonTutorial
Рекомендации по теме