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


Writing Your First Elif Statement



Download 2,61 Mb.
bet49/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   45   46   47   48   49   50   51   52   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Writing Your First Elif Statement


Creating an elif statement is identical to an if statement, with one difference, you use the elif keyword instead. You’re able to have multiple conditions for each elif as well. Let’s try it:

# using the elif conditional statement x, y = 5, 10 if x > y:
print("x is greater") elif x < y: print("x is less")

Go ahead and run that cell. Notice that the output is “x is less”. It checked the initial if statement, but since that returned False, it moved on to the elif conditional statement. That statement returned True and the block of code within it ran.

Checking Multiple Elif Conditions


Having the ability to write multiple decisions based on a single variable is a necessity, which is why elif statements were built. Take the following code, for instance:

# checking more than one elif conditional statement x, y = 5, 10 if x > y:
print("x is greater") elif (x + 10) < y: # checking if 15 is less than 10 print("x is less") elif (x + 5) == y: # checking if 10 is equal to 10 print("equal")

Go ahead and run that cell. The resulting output is “equal”. The first if and elif statements both returned False, but the second elif statement returned True, which is why that block of code ran. You can have as many elifs as you want, but they must be associated with an if statement.
Note Within the conditional, we perform addition, but we wrap it within parenthesis so that it executes the math operation first.

Conditionals Within Conditionals


We’ve gone over how Python uses indentation to separate blocks of code. So far, we’ve only seen one indentation level, but what if we added an if statement within an if statement?

# writing multiple conditionals within each other - multiple block levels x, y, z = 5, 10, 5 if x > y:
print("greater") elif x <= y: if x == z:
print("x is equal to z") # resulting output elif x != z: print("x is not equal to z") # won't get hit

Go ahead and run that cell. The output results in “x is equal to z”. To break it down, the initial if statement returns False, and the next elif statement returns True, so it runs that block. Now inside of that block is another conditional statement, so it checks the first if statement, which returns True, and runs the block of code inside that.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   45   46   47   48   49   50   51   52   ...   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