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



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

Logical Operator “not”


In certain instances, you’ll want to check for the opposite of a value. The “not” operator is used for just that. It essentially returns the opposite of whatever the current value is. Let’s try it out:


# using the keyword 'not' within an 'if statement' flag = False if not flag: # same as saying if not true print("Flag is False")

Go ahead and run that cell. You’ll notice that the resulting output is “Flag is False”. This is due to the “not” operator, which took the opposite value of False and made the condition return True.
Note We get the same result if we write “if flag == False:”.

Membership Operators


Membership operators are used to test if a sequence appears in an object. There are two keywords that we can use to check if a value exists in an object or not. Let’s check them out.

Membership Operator “in”


When you want to check if a given object has a value appear in it, you use the “in” operator. The best use case is checking for a certain value within strings. Let’s check out an example:

# using the keyword 'in' within an 'if statement' word = "Baseball" if "b" in word: print( "{ } contains the character b".format(word) )

Go ahead and run that cell. The resulted output is “Baseball contains the character b”. This is case sensitive, but lucky for us the word Baseball has one lowercase and one uppercase b.

Membership Operator “not in”


Likewise, if you want to check to see if an object doesn’t include a specific value, you would use the “not in” operator. This is essentially just checking the opposite of the “in” operator. Let’s see:

# using the keyword 'not in' within an 'if statement' word = "Baseball" if "x" not in word: print( "{ } does not contain the character x".format(word) )

Go ahead and run that cell. The resulting output is “Baseball does not contain the character x”. It simply checks to see if the character x is not included in the string value of our word variable.

Download 2,61 Mb.

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