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



Download 2,02 Mb.
Pdf ko'rish
bet74/126
Sana01.01.2022
Hajmi2,02 Mb.
#285662
1   ...   70   71   72   73   74   75   76   77   ...   126
Bog'liq
Learning Python The Ultimate Guide for Beginners to Coding with

Nesting Dictionary
There are situations that may warrant you to store several dictionaries on a
particular  list.  This  is  referred  to  like  nesting  in  Python  because  you  can
include a dictionary in another dictionary. Nesting is very powerful if you
understand them properly, and we will use an example to explain it better.
The team1 dictionary list comprises of a single team but doesn’t have the
space  to  store  additional  information  concerning  a  second  team.  How  can
you control a group of teams? You can do this by making a list, which has
each team in a dictionary. Consider the program below:
team1 = {‘color’: ‘red’, ‘scores’ : 3}
team2 = {‘color’: ‘black’, ‘scores’ : 5}
team3 = {‘color’: ‘blue’, ‘scores’ : 0}
team4 = {‘color’: ‘brown’, ‘scores’ : 9}
teams = [team1, team2, team3]
for team in teams:
print(team)
First, we create four dictionaries with each representing a different team. In
the fourth line, we stored each of the dictionaries in a particular list called
teams. Then, we loop through our list before printing each team.
{‘color’: ‘red,’ ‘scores’ : 3}
{‘color’: ‘black,’ ‘scores’ : 5}
{‘color’: ‘blue’, ‘scores’ : 0}
{‘color’: ‘brown,’ ‘scores’ : 9}
This  example  looks  simple  and  very  straightforward.  Let  us  look  at  a
realistic  one  that  will  involve  over  four  teams  with  code,  which
automatically  creates  each  team.  In  the  example  below,  we  use  range()  to
generate 40 teams.
#Create an empty list to store the teams.
teams =[]


#create 40 teams.
for team_num in range(40):
new_team  =  {‘color’:  ‘red’,  ‘scores’:  3,  ‘position’:
‘middle’}
teams.append(new_team)
#display the first 6 teams.
for team in teams[:6]:
print[team]
print[“…”]
#display the number of teams we have in our dictionary
print(f “Total teams available in our list: {len(teams)}”)
In this example, we began by creating an empty list comprising of all teams
to be created in our dictionary. We use the range() method to determine the
number of times the loop will repeat. Whenever the loop goes through the
range() method, it creates a new team and appends the new team to our list
“teams.” In the line “for team in teams [:6]:”, we use a slice to print our first
six teams. Finally, the last statement prints the length of the teams, which in
this case is 40 teams.
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘middle’}
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘middle’}
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘middle’}
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘middle’}
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘middle’}
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘middle’}

Total teams available in our list:40
If  you  observe,  the  teams  all  have  the  same  features  –  color,  scores,  and
positions.  However,  it  doesn’t  mean  that  Python  sees  them  as  one  item
rather a separate object. With this, we can modify any team of our choice
without affecting the other.
How  possible  is  it  to  work  with  a  collection  of  teams  like  we  have?
Assuming a particular team decides to change their color and scores, how
can you do this? If you decide to change the color of a team, you can use
the  “for  loop”  conditional  test  with  an  if  statement.  For  instance,  let  us
change  the  first  four  teams'  colors  to  black,  scores  to  5,  and  position  to
center.


#Create an empty list to store the teams.
teams =[]
#create 40 teams.
for team_num in range(40):
new_team  =  {‘color’:  ‘red’,  ‘scores’:  3,  ‘position’:
‘middle’}
teams.append(new_team)
for team in teams[:4]:
if team[‘color’] == ‘red’”
team[‘color’] = ‘black’
team [‘scores’] = 5
team [‘middle] = ‘center’
#display the first 6 teams.
for team in teams[:6]:
print[team]
print[“…”]
#display the number of teams we have in our dictionary
print(f “Total teams available in our list: {len(teams)}”)
When you run the program, your output should be:
{‘color’: ‘black,’ ‘scores’: 5, ‘position’: ‘center}
{‘color’: ‘black,’ ‘scores’: 5, ‘position’: ‘center’}
{‘color’: ‘black,’ ‘scores’: 5, ‘position’: ‘center’}
{‘color’: ‘black,’ ‘scores’: 5, ‘position’: ‘center’}
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘center’}
{‘color’: ‘red,’ ‘scores’: 3, ‘position’: ‘center’}

Total teams available in our list:40

Download 2,02 Mb.

Do'stlaringiz bilan baham:
1   ...   70   71   72   73   74   75   76   77   ...   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