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



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

.remove( )


The remove method allows us to remove items from a list based on their given value:

# using the remove method with a try and except sports = [ "baseball", "soccer", "football", "hockey" ] try:
sports.remove("soccer") except:
print("That item does not exist in the list") print(sports)

Go ahead and run that cell. Here we’ll see that the output is our sports list without soccer because we were able to remove it correctly. Now the reason why we use a try and except with the removal is because if “soccer” didn’t exist in the list, then the program would crash.

Working with Numerical List Data


Python provides a few functions for us to use on lists of numerical data, such as min, max, and sum. There are several more that we can use, though these are used most frequently:

# using min, max, and sum nums = [5, 3, 9] print( min(nums) ) # will find the lowest number in the list print( max(nums) ) # will find the highest number in the list print( sum(nums) ) # will add all numbers in the list and return the sum

Go ahead and run that cell. The output will result in 3, 9, and 17. As their names state, they’ll find the minimum and maximum number. The sum function will simply add all the numbers up.

Sorting a List


Often, you’ll need to work with a sorted list. There are a couple methods for doing so, but they are very different. One will change the original list, while the other returns a copy. sorted( )
The sorted function will work on either numerical or alphabetical lists, but not one that is mixed. Sorted also returns a copy of the list, so it doesn’t alter the original. Usually if you need to keep the original intact, be sure to use this function:

# using sorted on lists for numerical and alphabetical data nums = [5, 8, 0, 2] sorted_nums = sorted(nums) # save to a new variable to use later print(nums, sorted_nums) # the original list is in tact

Go ahead and run that cell. You’ll notice the output of our nums list is still in the original order when we declared it. To use the new sorted list, we simply save it to a new variable.

Download 2,61 Mb.

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