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



Download 2,02 Mb.
Pdf ko'rish
bet36/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   32   33   34   35   36   37   38   39   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Accessing a List Item
Perhaps,  you  don’t  want  the  users  to  see  certain  items  in  a  list;  you  can
decide  to  access  only  the  items  you  want.  To  have  access  to  a  particular
element in a list, you have to write the list name then followed by the index
of the item, which is enclosed in squared brackets. For instance, we can pull
the third item in our list of items in the kitchen.
kitchen_item  =  [“pot,”  “kettle,”  “fridge,”  “dishwasher,”  “microwave,”
“knife,” “bowls”]


print(kitchen_item [3])
The output will be as follow:
dishwasher
Index Positions
The  first  index  position  in  Python  is  not  1  but  0.  If  you  observe  in  the
example  above,  you  will  see  that  when  indexed  the  third  item,  we  started
counting  from  0.  You  can  see  that  pot  (0),  kettle  (1),  fridge  (2),  while
dishwasher (3). Peradventure, you run a Python program and get a different
result; you have to check if you are making a number error.
Always remember that the second element in any list starts with an index of
1. A simple way of knowing what the particular index number will be is to
substrate one from the item you want to index. Does that sound simple?
kitchen_item  =  [“pot,”  “kettle,”  “fridge,”  “dishwasher,”  “microwave,”
“knife,” “bowls”]
print(kitchen_item)
For instance, we want to access only the item “microwave. To do that, you
have to count the number of items to get to “microwave” and subtract one.
from the code, if you count first, you will discover that “microwave” is the
5
th
 element.  However,  to  index  it  to  print  only  “microwave,”  you  have  to
substrate one from the element. Your code will be like this:
kitchen_item  =  [“pot,”  “kettle,”  “fridge,”  “dishwasher,”  “microwave,”
“knife,” “bowls”]
print(kitchen_item [4])
Let us use another example to index the third and sixth items.
kitchen_item  =  [“pot,”  “kettle,”  “fridge,”  “dishwasher,”  “microwave,”
“knife,” “bowls”]


print(kitchen_item [3])
print(kitchen_item [6])
The output will be as follows:
dishwasher
bowls
Assuming  you  only  want  to  access  the  last  element,  Python  has  a  unique
way  of  doing  that.  You  can  do  this  by  using  an  index  of  -1.  With  this,
Python returns the last item.
kitchen_item  =  [“pot,”  “kettle,”  “fridge,”  “dishwasher,”  “microwave,”
“knife,” “bowls”]
print(kitchen_item [-1])
In  this  example,  the  code  will  return  “bowls.”  It  is  easy  to  remember  this
syntax. You do not have to know the particular length of the list. This also
applies to other negative index values. It is like going backward. You can
use  the  negative  index  to  access  the  second  or  third  items  in  a  list.  For
instance,
kitchen_item  =  [“pot,”  “kettle,”  “fridge,”  “dishwasher,”  “microwave,”
“knife,” “bowls”]
print(kitchen_item [-2])
print(kitchen_item [-3])
The output will be as follows
knife
microwave


Exercise to Try
Write a Python program, which contains a list of all your special
friends.  Then  print  their  names  by  accessing  each  element  in
your list.
From  the  list,  you  created  above,  print  a  greeting  message  to
each name you access in the list.
Think  of  any  means  of  transportation  you  can  remember  and
write a list of items used within them. Then make a statement on
some of these elements in the list and print it.

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   32   33   34   35   36   37   38   39   ...   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