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



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

How They Work



Else conditional statements are the end all be all of the if statement. Sometimes you’re not able to create a condition for every decision you want to make, so that’s where the else statement is useful. The else statement will cover all other possibilities not covered and will always run the code if the program gets to it. This means that if an elif or if statement were to return True, then it would never run the else; however, if they all return False, then the else clause would run no matter what every time. Again, it’s always easier to see it in code; let’s try!

Writing Your First Else Statement


Like an elif statement, the else clause needs to always be associated with an original if statement. The else clause covers all other possibilities, so you don’t need to write a condition at all; you just need to provide the keyword “else” followed by an ending colon. Remember that an else clause will run the code inside of it if the program reaches the statement. Try the following:

# using an else statement name = "John" if name == "Jacob":
print("Hello Jacob!") else:
print("Hello { }!".format(name) )
Go ahead and run that cell. Notice the output here is “Hello John”. The first if statement returned False, so as soon as it reached the else clause, it ran the print statement inside of it.

Complete Conditional Statement


Now that we’ve covered all three parts of a conditional statement, let’s go ahead and try using all three together in one statement:

# writing a full conditional statement with if, elif, else name = "John" if name[0] == "A":
print("Name starts with an A") elif name[0] == "B":
print("Name starts with a B") elif name[0] == "J":
print("Name starts with a J") else: # covers all other possibilities print( "Name starts with a { }".format( name[0] ) )

Go ahead and run that cell. The resulting output is “Name starts with a J”, which was output by the second elif statement. The first if and elif statements returned False, so their blocks of code didn’t run. Once the second elif statement returned True and ran its own code, the else statement will be skipped over and not run. Remember that indexing starts at 0, so by using the bracket notation after the name variable was accessing the first element within the string.
Note Be sure to go back and check out the section on string indexing if you're having trouble understanding the bracket notation.

Download 2,61 Mb.

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