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



Download 2,02 Mb.
Pdf ko'rish
bet82/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   78   79   80   81   82   83   84   85   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Using “OR”
The “OR” keyword allows you to check multiple conditions as the “AND”
keyword.  However,  the  difference  here  is  that  the  “OR”  keyword  is  used
when  you  want  to  ascertain  that  one  expression  is  true  for  multiple
conditions. In this situation, if one of the expression is false, the condition
returns true. It returns false when both conditions are false.
Let  us  consider  our  previous  example  using  the  “OR”  keyword.  For
instance, you want to ascertain if two students in a class have over 45 score
mark.
>>> score_1 = 46
>>> score_2 = 30
>>> score_1 >=45 or score_2 >= 45
True
>>> score_1 = 30
>>> score_1 >= 45 or score_2 >= 45
False
We began by declaring two variables score_1 and score_2 and assign values
to them. In the third line, we test the OR condition using the two variables.
The test in that line satisfies the condition because one of the expressions is
true. Then, it changed the value of the variable score to 30; however, it fails
both conditions and therefore evaluates false.
Besides using the “And” and “OR” conditional statements to check multiple
conditions,  we  can  also  test  the  availability  of  a  value  in  a  particular  list.
For  instance,  you  want  to  verify  if  a  username  requested  is  already  in


existence  from  a  list  of  usernames  before  the  completion  of  online
registration on a website.
To do this, we can use the “in” keyword in such a situation. For instance, let
us use a list of animals in the zoo and check if it already on the list.
>>>animals = [“zebra”, “lion”, “crocodile”, “monkey”]
>>> “monkey” in animals
True
>>> “rat” in animals
False
In the second and fourth lines, we use the “in” keyword to test if the request
word in a double quote exists in our list of animals. The first test ascertains
that  “monkey”  exists  in  our  list,  whereas  the  second  test  returns  false
because the rat is not in the animal's list. This method is significant because
we  can  generate  lists  of  important  values  and  check  the  existence  of  the
values in the list.
There  are  situations  where  you  want  to  check  if  a  value  isn’t  in  a  list.  In
such a case, instead of using the “in” keyword to return false, we can use
the “not” keyword. For instance, let us consider a list of Manchester United
players before allowing them to be part of their next match. In order words,
we want to scan the real players and ensure that the club does not field an
illegible player.
united_player = [“Rashford,” “Young,” “Pogba,” “Mata,” “De Gea”]
player = “Messi”
if player not in united_player:
print(f  “{player.title()},  you  are  not  qualified  to  play  for  Manchester
United.”)
The line “if player, not in united_player:” reads quite clearly. Peradventure,
the  value  of  the  player  isn’t  in  the  list  united_player,  Python  returns  the


expression  to  be  True  and  then  executed  the  line  indented  under  it.  The
player “Messi” isn’t part of the list united_player; therefore, he will receive
a message about his qualification status. The output will be as follow:
Messi, you are not qualified to play for Manchester United.

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   78   79   80   81   82   83   84   85   ...   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