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



Download 2,02 Mb.
Pdf ko'rish
bet66/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   62   63   64   65   66   67   68   69   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Adding Key-Value Pairs


Since dictionaries are structurally dynamic, we can add new pairs to them
whenever we want. For instance, we can add additional information to our
original  program  to  include  positions,  which  will  display  the  position  the
score  is  taken.  Let  the  home  team  be  on  the  left  position  while  the  away
team be on the right.
team1 ={‘color’: ‘red’, ‘scores’ : 3}
print(team1)
team1[‘home_team’] = left
team1[‘away_team’] = right
print(team1)
We began by defining a dictionary “team1” and included its key and values,
respectively, before printing the dictionary. In the second phase of the code,
we assign the home team to be on the left position while the away team is
on the right. If we print the program, our output will be:
{‘color’: ‘red,’ ‘scores’ : 3}
{‘color’: ‘red’, ‘scores’ : 3, ‘home_team’: left, ‘away_team’ : right}
In  our  final  program,  we  have  four  key-value  pairs,  where  we  added  two
positions, whereas the original two specifies only color and score.
At  times,  most  programmers  find  it  convenient,  to  begin  with,  empty
dictionary  unlike  what  we  did  by  adding  elements  first.  To  fill  an  empty
dictionary, you have to define the dictionary without adding any key-value
pairs. The dictionary will be empty with a set of braces. For instance, if we
are to define the color and scores dictionary, that will be as follows:
team1 = {}
team1[‘color’] = ‘red’
team1[‘score’] = 3
print(team1)
What  we  did  here  is  to  define  an  empty  team1  dictionary  before  adding
color  and  score  values,  respectively.  We  will  get  the  same  results  as  what
we got in our previous examples.
{‘color’: ‘red,’ ‘scores’ : 3}



Download 2,02 Mb.

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