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



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

Lambda Function Syntax


The syntax for lambda functions will generally remain the same, unlike list
comprehensions when you begin to add the conditional statements. To start, let’s look at the basic structure:
>>> lambda arguments : expression
Lambdas will always begin with the keyword lambda. Following that you’ll find any arguments that are being passed in. On the right side of the colon, we’ll see the expression to be performed and returned. Lambdas return the expression by default, so we don’t need to use the keyword:
>>> lambda arguments : value_to_return if condition else value_to_return
Like list comprehension, the conditional statement goes at the end. This is as complex as lambda functions get. Anything more than this would require writing the function out completely.
Note Lambdas basically use ternary operators on the right side of the colon.

Using a Lambda


When using lambdas without storing them into a variable, you need to wrap parenthesis around the function, as well as any arguments being passed in. Let’s start small by writing a lambda function that will return the result of the argument squared:

# using a lambda to square a number
( lambda x : x**2 )( 4 ) # takes in 4 and returns the number squared

Go ahead and run the cell. We’ll get an output of 16. The first set of parenthesis holds the lambda function. The second set holds the argument being passed in. In this case, the integer 4 is passed into x, and the expression x∗∗2 is performed and the result returned. They are known as anonymous functions because they don’t have a name. In the following, you’ll find the code written for a normal function that would perform the same execution:
>>> def square(x):
>>> return x**2
>>> square(4)
We’ve taken three lines and turned them into one. Once you get used to reading lambda syntax, programs become easier to read and write with these functions.

Download 2,61 Mb.

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