Learning Python: The Ultimate Guide for Beginners to Coding with Python with Useful Tools



Download 2,02 Mb.
Pdf ko'rish
bet89/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   85   86   87   88   89   90   91   92   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Performing Multiple Conditions
Using  the  if-elif-else  statement  comes  handy  when  especially  when  you
want  to  pass  only  one  test.  Once  the  interpreter  discovers  that  this  test  is
passed, it skips other tests and halts the program. With this feature, you test
a specific condition in a line of code.
Nevertheless,  there  are  situations  that  may  warrant  you  to  check  all  the
conditions available. In such a scenario, you can use multiple if statements
without adding the elif or else blocks. This method becomes relevant when
more than one of the condition returns true. For instance, let us consider the
previous example of players in Manchester United to illustrate this. In this,
we  want  to  include  the  players  in  an  upcoming  match  against  their  rivals
Manchester City.
united_players = [“Rashford,” “Young,” “Pogba,” “Mata,” “De Gea”]
if “Young” in united_players:
print(“Adding Young to the team list.”)


if “De Gea” in united_players:
print(“Adding Dea Gea to the team list.”)
if “Messi” in united_players:
print(“Adding Messi to the team list.”)
print( “\ Team list completed for the match against Manchester City!”)
In  the  first  line,  we  defined  united_players  as  a  variable  with  values
Rashford,  Young,  Pogba,  Mata,  and  De  Gea.  The  second  line  uses  the  “if
statement” to check if the person requested for Young. The same applies to
the lines with the “if statement” and the condition is run regardless of the
outcome of the previous tests. For this program above, the output will be:
Adding Young to the team list.
Adding Dea Gea to the team list.
Team list completed for the match against Manchester City!
If we decide to use the if-elif-else block, the code won’t function properly
because once a particular test returns true, the program will stop. Let us try
it and see.
united_players = [“Rashford,” “Young,” “Pogba,” “Mata,” “De Gea”]
if “Young” in united_players:
print(“Adding Young to the team list.”)
elif  “De Gea” in united_players:
print(“Adding Dea Gea to the team list.”)
elif  “Messi” in united_players:
print(“Adding Messi to the team list.”)


print( “\ Team list completed for the match against Manchester City!”)
In this code, Python will evaluate the first condition, and once it is true, the
program stops. The output for this program will be:
Adding Young to the team list.
Team list completed for the match against Manchester City!
Exercise to Try
Consider  the  list  of  colors  we  have  in  the  world.  Create  a
variable name color and assign the following colors to it – blue,
red, black, orange, white, yellow, indigo, green.
Use an “if statement” to check if the color is blue. If
the  color  is  blue,  then  print  a  message  indicating  a
score of 5 points.
Write  a  problem  using  the  if-else  chain  to  print  if  a
Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   85   86   87   88   89   90   91   92   ...   126




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