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



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

How to Modify Values
Besides adding and creating an empty dictionary list, you can also modify a
value in your dictionary. To do this, you have to specify the dictionary name
before adding the new value. Let us change the color from red to blue.
team1 ={‘color’: ‘red’}
print(f “The color of the team is {team1[‘color’]}.”)
team1 = [‘color’] = ‘blue’
print(f “The team’s new color is {team1[‘color’]}.”)
What we did is to first define the team1 dictionary, which contains only the
team’s  color.  We  then  print  the  team’s  color  before  changing  the  value  of
the  corresponding  key  from  red  to  blue.  The  output  shows  that  we  have
changed the team color from red to blue.
The color of the team is red.
The team’s new color is blue.
To make the program more interesting, I will track the position of the team
based  on  their  speed  in  the  opponent’s  direction.  With  the  ball  speed,  we
can determine if the team is on the left or right.
team1 = {‘home_team’ : ‘left’, ‘away_team’: ‘right’, ‘ball’: ‘ neutral’
print(f “Original direction: {team1(‘home_team’)}”)
#The team is on the left position
#Current speed of the team
if team1[‘ball’] == ‘slow’:
home_incre = 1
elif team1[‘ball’] == ‘medium’:
home_incre = 2
else:
#in opposition half
home_incre = 3
team1[‘home_team’] = team[‘home_team’] + home_incre
print(f “Position of team: {team1[‘home_team’]}”)
If you observe, I intentionally avoided the color and its point value in order
to make the program simple. We began by defining a team with the left and


right position, a ball speed set medium.
In the line “if team1[‘ball’] == ‘home’:” we use an if-elif-else conditional
statement to test the position of the ball. If the ball is slow, then it is on the
home  team,  if  it  is  medium,  then  it  is  positioned  in  the  middle.  Run  the
program and notice what the output will be. Towards the end of this chapter,
I will unveil the outcome of this program.

Download 2,02 Mb.

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