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



Download 2,61 Mb.
bet69/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   65   66   67   68   69   70   71   72   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

.sort( )


The sort method is used for the same purpose that our previous sorted function is used for; however, it will change the original list directly:

# sorting a list with .sort() in-place nums = [5, 0, 8, 3] nums.sort( ) # alters the original variable directly print(nums)

Go ahead and run that cell. The resulted output will be a properly sorted list. Just remember that the nums variable is now changed, as .sort() changes the value directly.

Conditionals and Lists


When working with lists, often you’ll need to check if values exist. Now we’ll introduce how to run conditional statements on a list. There are many reasons to run a conditional on a list; these are simply a couple examples.

Using “in” and “not in” Keywords


We’ve seen the use of these keywords already, when we covered conditional statements last week. When working with lists, they serve a purpose to find values within the list quickly:

# using conditional statements on a list names = [ "Jack", "Robert", "Mary" ] if "Mary" in names:
print("found") # will run since Mary is in the list if "Jimmy" not in names: print("not found") # will run since Jimmy is not in the list

Go ahead and run that cell. The output results in “found” and “not found”. On the first statement, we were trying to see if “Mary” existed in the list, which it does. The second conditional statement checked to see if “Jimmy” did not exist, which is also true, so it too runs.

Checking an Empty List


There are so many reasons to need to check for an empty list. It’s usually to ensure you don’t cause any errors in your program, so let’s see how we can check:

# using conditionals to see if a list is empty nums = [ ] if not nums: # could also say 'if nums == []' print("empty")

Go ahead and run that cell. This will output “empty”. It’s mentioned in the comment, but we could have also checked to see if it were equal to empty brackets. Here, I wanted to show you how to use the “not” keyword. To check for a list with items, you would write the following:
>>> if nums:

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   65   66   67   68   69   70   71   72   ...   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