Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet133/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   129   130   131   132   133   134   135   136   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Passing Multiple Arguments


Lambdas can take in any number of arguments, like functions. Let’s try passing in two arguments this time and multiplying them by each other:

# passing multiple arguments into a lambda
( lambda x, y : x * y )( 10, 5 ) # x = 10, y = 5 and returns the result of 5 * 10

Go ahead and run the cell. We’ll get an output of 50. This time the lambda function accepted two arguments of x and y on the left side of the colon. On the right side of the colon, it was able to perform the expression of multiplying those two arguments together and returning the result. In the following, you’ll find the same code, as if we wrote a normal function:
>>> def multiply(x, y):
>>> return x * y
>>> multiply(10, 5)
Same as before, we were able to save a couple lines of code to get the same result.

Saving Lambda Functions


Lambdas get there name anonymous function because they don’t have a name to reference or call upon. Once a lambda function is used, it can’t be used again unless it is saved into a variable. Let’s use the same lambda function as before, except this time save it into a variable called “square” that can be referenced even after the lambda function is read:

# saving a lambda function into a variable square = lambda x, y : x * y print(square) result = square(10, 5) # calls the lambda function stored in the square variable and returns 5 * 10 print(result)

Go ahead and run the cell. We’ll get the same output as before, except this time we got it by calling square as a function. When functions are stored inside of variables, the variable name acts as the function call. When we stored a lambda inside of the square variable, we were able to call the lambda function by calling square and passing in the arguments.
Note even functions that are defined normally can be saved into variables and referenced by the variable name.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   129   130   131   132   133   134   135   136   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish