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



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

Conditional Statements


Once you begin adding conditional statements into a lambda function, they act the same way that ternary operators do. The only difference is that you must provide both the if and else statements. You can’t use just an if statement; it will render a syntax error, as it always needs an expression to return . Let’s create a lambda that will return the greater number between two arguments passed in:

# using if/else statements within a lambda to return the greater number greater = lambda x, y : x if x > y else y result = greater(5, 10) print(result)

Go ahead and run the cell. We’ll get an output of 10 as it is the higher value. Lambdas are extremely useful when you need a function that can perform a simple conditional such as this. The same code written as a normal function can be seen in the following:
>>> def greater(x, y):
>>> if x > y:
>>> return x >>> else:
>>> return y
>>> result = greater(5, 10)
When conditional statements are used, it’s easy to see the power of lambda functions. In this case we were able to turn five lines of code into one.

Returning a Lambda


Where lambda functions shine is in their ability to make other functions more modular. Let’s say we have a function that takes in an argument and we want that argument to be multiplied with an unknown number later in the program. We can simply create a variable that stores a returned lambda function while passing an argument. Let’s try a couple examples:

# returning a lambda function from another function def my_func(n):
return lambda x : x * n doubler = my_func(2) # returns equivalent of lambda x : x * 2 print( doubler(5) ) # will output 10 tripler = my_func(3) # returns equivalent of lambda x : x * 3 print( tripler(5) ) # will output 15

Go ahead and run the cell. We’ll get an output of 10 and 15. What occurs when we define our doubler variable is that we call my_func while passing in the integer value 2. That value is used within the lambda function, and the lambda is then returned. However, the lambda isn’t returned as “lambda x : x n”; it is now returned with the integer 2 in place of n. Whenever doubler is called, it’s really the lambda function being called. Which is why we get an output of 10 when we pass the value 5 into doubler. The same applies to our variable tripler. We’re able to modify the result of my_func because of the returned lambda function.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   130   131   132   133   134   135   136   137   ...   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