Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet95/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   91   92   93   94   95   96   97   98   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Accessing Ingredients: output all the ingredients from the following list within the “ingredients” key using a for loop:

>>> pizza = {
>>> 'ingredients': ['cheese', 'sausage', 'peppers']
>>> }

Data collections allow us to work with large data as they are stored in key-value pairs. remember that data is accessed through keys.
Tuesday: Working with Dictionaries
Today’s lesson will cover how to add data, manipulating data, removing key-value pairs, and iterating through dictionaries.
To follow along with this lesson, let’s continue from our previous notebook file
Week_06” and simply add a markdown cell at the bottom that says, “Working with Dictionaries.

Adding New Information


You’ll often need to add new key-value pairs after declaring a dictionary. Let’s see how:

# adding new key/value pairs to a dictionary car = { "year": 2018 } car["color"] = "Blue" print( "Year: { } \t Color: { }".format( car["year"], car["color"] ) )

Go ahead and run the cell. To add new pairs, on the left side of the equals operator, you provide the dictionary name, followed by the new key within brackets. On the right side is whatever you want the value to be. This will output a nicely formatted string with our car information.
Note as of python, 3.7 dictionaries are ordered by default. in older versions of python, key-value pairs didn’t always keep their order. You would have needed to use an OrderedDict( ).

Changing Information


Altering key-value pairs is exactly like adding a new pair. If the key exists, it simply overwrites the previous value; however, if it doesn’t exist, it will create a new key-value pair for you:

# updating a value for a key/value pair that already exists car = { "year": 2018, "color": "Blue" } car["color"] = "Red" print( "Year: { } \t Color: { }".format( car["year"], car["color"] ) )

Go ahead and run the cell. Like how we declared a new key-value pair earlier, since the key “color” already exists in the dictionary, it simply overwrites the previous value.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   91   92   93   94   95   96   97   98   ...   200




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