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



Download 2,02 Mb.
Pdf ko'rish
bet59/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   55   56   57   58   59   60   61   62   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Slicing a List in Python
If  you  want  to  perform  a  slice  in  Python,  you  have  to  stipulate  the  first  and  last  index  of  the
elements  in  the  list.  Similar  to  the  range()  function  we  use  previously,  Python  halts  at  one  item
before  the  second  index  you  stipulate.  To  display  the  first  five  elements,  you  have  to  request
indices from 0 to 5. In this situation, this would return the following elements 0, 1, 2, 3, 4, and 5.
The following are team members in Manchester United Football club
united_players =[ “Rashford,” “Young,” “Pogba,” “James,” “De Gea”]
print(united_players [1:4]
The second line prints a slice of the element in the list, which comprises of the following players.
“Young,” “Pogba,” “James,” “De Gea”
If you mistakenly forget the first index when slicing a list, Python spontaneously begins your slice
at the start of your list. For instance,
united_players =[ “Rashford,” “Young,” “Pogba,” “James,” “De Gea”]
print(united_players [:4]
Since there is no starting index, the output will be:
“Young,” “Pogba,” “James,” “De Gea”
The same thing applies if you omit the last index of the list. For instance, you want to start your
index at 2 while omitting the second index.
united_players =[ “Rashford,” “Young,” “Pogba,” “James,” “De Gea”]
print(united_players [2:]
Python will return the following value:
“Pogba,” “James,” “De Gea”
With this, you can print out all elements in a list irrespective of the length and its position. You can
use a negative index, but it recalls a number from list end. For instance, our goal to display the last
four United players in our list
united_players =[ “Rashford,” “Young,” “Pogba,” “James,” “De Gea”]
print(united_players [-4:]
This will print the last four players
“Young,” “Pogba,” “James,” “De Gea”



Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   55   56   57   58   59   60   61   62   ...   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