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


If Statements vs. Elif Statements



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

If Statements vs. Elif Statements


A major difference that you’ll need to understand going forward is the use for elif statements against using multiple if statements. All elif statements are connected to one original if statement, so that once a single conditional is True, the rest do not run. Let’s see an example:

# testing output of two if statements in a row that are both true x, y, z = 5, 10, 5 if x < y:
print("x is less") if x == z: print("x is equal")

Go ahead and run that cell. Notice that the resulting output is both print statements here. This is due in part to having two if statements. These if statements are not related to each other; they are separate conditional statements, whereas an elif is always connected to an if.

# testing output of an if and elif statement that are both true x, y, z = 5, 10, 5 if x < y:
print("x is less") elif x == z: print("x is equal to z")


Go ahead and run that cell. Notice that the output here is only “x is less” and doesn’t include the second print statement. That’s because an elif is attached to an if statement, and once one of the conditionals returns True, all others will not be checked even if they are True themselves.

WEDNESDAY EXERCISES


1. Higher/Lower: Ask the user to input a number. Type convert that number, and use an if/elif statement to print whether it’s higher or lower than 100.
2 . Find the Solution: Given the following code, fix any/all errors in order to make it output “lower”:

x, y = 5, 10 if x > y:
print("greater") try x < y:
print("lower")

Today was the next step into creating a program that will make decisions for us, not just one decision, but multiple.
Thursday: Else Statements
The third and final part of any good decision is what to do by default. In Python, we know them to be else statements. Today’s lesson will be quite short, but necessary in understanding conditional statements further.
To follow along with this lesson, let’s continue from our previous notebook file
Week_03” and add a markdown cell at the bottom that says, “Else Statements.

Download 2,61 Mb.

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