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



Download 2,02 Mb.
Pdf ko'rish
bet70/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   66   67   68   69   70   71   72   73   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Looping Through Dictionaries
A  dictionary  can  have  a  single  key-value  pair  and  even  up  to  millions  of
pairs  due  to  its  capability  of  accommodating  large  amounts  of  data.  With
this,  you  can  loop  through  a  dictionary.  Since  we  have  different  ways  of
storing  information  when  using  dictionaries,  it  makes  it  possible  to  loop


through  the  dictionary  in  various  ways.  In  this  section,  we  will  explore
ways of looping through dictionaries.
However,  before  we  begin  to  explore  the  various  ways  of  approaching
looping  through  dictionaries,  let  us  look  at  a  dictionary  that  saves  the
information of users visiting a website. The dictionary stores the last name,
first name, and username of the person.
user = {
‘last_name’:
‘Trump,’
‘first_name’:
‘Donald,’
‘username’:
‘dotrum,’
}
If  you  remembered  what  you  have  learned,  you  could  easily  access  every
information in our user dictionary. However, what will you do if you want
to  see  all  the  information  stored  in  our  dictionary?  Well,  to  do  that,  you
have to use a “for loop” to go through the dictionary. Let see how you can
put that into action.
user = {
‘last_name’:
‘Trump,’
‘first_name’:
‘Donald,’
‘username’:
‘dotrum,’
}
for k, v in user.item():
print(f “\nK: {k}”)
print(f “V: {v}”)
As  you  can  see,  for  you  to  loop  through  a  dictionary,  you  have  to  create
names  for  our  two  variables,  which  will  hold  last  and  first  names  in  our
key-value pair. You can use any variable name of your choice. In this case,
we use k and v to represent key and value, respectively. Our output will be:
K: last_name
V: Trump


K: first_name
V: Donald
K: username
V: dotrum
Looping through a dictionary works perfectly for a program like the list of
candidates  in  an  election  we  wrote  previously.  Remember,  it  stores  the
name and party of candidates approved to participate in the election. If we
were to loop through the list, we will get the names of every candidate and
generate  their  respective  parties.  Since  the  keys  represent  the  person’s
name,  whereas  the  value  is  the  party,  we  can  use  variable  names  such  as
name and party in our loop rather than using key and value. This will make
it easier for you to fully understand.
election_list = {
‘John Campbell’ : ‘Freedom Party’,
‘Eric Longman’: ‘Sovereign Nation Party’,
‘Amanda 
Drill’: 
‘ 
Women 
Liberation
Movement’,
‘Condolence Rice’: ‘Democratic Party’,
‘Donald Trump’: ‘Republican Party’,
}
for name, party in election_list.item():
print(f 
“{name.title()}’s 
Download 2,02 Mb.

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