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



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

Lists with Dictionaries


The combination of lists within dictionaries and vice-versa can become confusing when trying to figure out how to access information. Always remember lists are indexed, and dictionaries use keys. Depending on the order of the data stored, you’ll need to do one or the other first. When a list is storing a dictionary, you need to access that dictionary by the index first. After that you have access to the key-value pairs within the dictionary. Let’s see an example:

# storing a dictionary within a list and accessing it data = [ "John", "Dennis", { "name": "Kirsten" } ] print( data[2] ) # the dictionary is in index 2 print( data[2]["name"] ) # first access the index, then access the key

Go ahead and run the cell. First, we access the item in the second index, which is our dictionary. Then we access the value stored at the “name” key, which is the output of “Kirsten”.
Note Be very careful when using numbers for keys.

Dictionaries with Dictionaries


Dictionaries are very powerful and efficient due to how they’re stored in memory. Often, you’ll want to use dictionaries as the value for your key-value pairs. Let’s see an example:

# storing a dictionary within a dictionary and accessing it data = {
"team": "Boston Red Sox",
"wins": { "2018": 108, "2017": 93 }
}
print( data["wins"] ) # will output the dictionary within the wins key print( data["wins"]["2018"] ) # first access the wins key, then the next key

Go ahead and run the cell. This will output “108” in the second statement. We’re able to access this information by accessing the first key of “wins” followed by the second key of “2018”.

MONDAY EXERCISES


  1. User Input: ask the user for their name and age, and then create a dictionary with those key-value pairs. output the dictionary once created.


  2. Download 2,61 Mb.

    Do'stlaringiz bilan baham:
1   ...   90   91   92   93   94   95   96   97   ...   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