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



Download 2,02 Mb.
Pdf ko'rish
bet45/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   41   42   43   44   45   46   47   48   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Using the sort() Method
An easier means of organizing a list in Python is by using the sort method. Consider our kitchen
items in previous examples, and you want to sort them alphabetically. To make it easier, we assume
all the items are in lowercase.
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,” “knife,” “bowls”]
kitchen_item.sort
print(kitchen_item)
The output will be as follows when the sort method operates on the list items. Remember, it sorts
the item in alphabetical order.
[“bowls,” “dishwasher,” “fridge,” “kettle,” “knife,” “microwave,” “pot”]
Look at the code below:


kitchen_item = [“pot”, “kettle”, “fridge”, “dishwasher”]
kitchen_item.sort(reverse=true)
print(kitchen_item)
What  do  you  think  will  happen  to  our  list?  Well,  the  kitchen  items  will  be  reversed,  and  your
output will be:
[ “fridge,” “dishwasher,” “pot,” “kettle”]
List Length
Another important thing you can do with a list is to determine the length. To do this, you have to
use the len() function. Using our kitchen item list, we can demonstrate this.
kitchen_item = [“pot,” “kettle,” “fridge,” “dishwasher,” “microwave,” “knife,” “bowls”]
len(kitchen_item)
print(kitchen_item)
The output will be
7
This  function  comes  handy,  especially  when  you  want  to  determine  the  number  of  birds  that
remains to be shot.
Summary
I  am  convinced  that  you  know  what  lists  are,  how  to  work  with  them,  including  adding  and
removing elements in a list. Furthermore, you know how to determine the length of a list; however,
I want to go a step higher by introducing how you can work with items in a given list effectively.
We will loop through these items with a few line codes. With looping, you can work with any list,
irrespective of how many items contained in it.

Download 2,02 Mb.

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