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



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

Accessing Dictionary Information


All data stored within a dictionary is accessed via the key associated with the value you’re trying to access. We simply write the name of the dictionary followed by square brackets. Inside of the square brackets is the key. This will retrieve the value stored at that key:

# accessing dictionary information through keys person = { "name": 'John" } print( person[ "name" ] ) # access information through the key

Go ahead and run the cell. This will output “John” since that is what’s stored at the “name” key.

Using the Get Method


Another way of retrieving information is to use the get() method. The major difference between using this method and the previous way of accessing a value is that the get method won’t throw a key error. If the key doesn’t exist, it will simply return “None”. You may also add in a second argument in the call, in order to have the program return a more specific data type. Let’s try:

# using the get method to access dictionary information person = { "name": 'John" } print( person.get("name") ) # retrieves value of name key as before print( person.get("age", "Age is not available.") ) # get is a secure way to retrieve information

Go ahead and run the cell. On the second print statement, we’ll receive the “Age is not available” message because the key “age” does not exist. This gives us a more secure way of retrieving information.

Dictionaries with Lists


Dictionaries become powerful when you start working with data collections as values:

# storing a list within a dictionary and accessing it
data = { "sports": [ "baseball", "football", "hockey", "soccer" ] } print( data["sports"][0] ) # first access the key, then the index

Go ahead and run the cell. In order to access the list, we must first access the “sports” key. After that we can access items like any other list via the index. This will output “baseball”. Keep in mind that we cannot create a dictionary that stores a list without first attaching a key:

# improperly storing a list within a dictionary sports = [ "baseball", "football", "hockey", "soccer" ] sports_dict = dict( sports ) # will produce error, no key

Go ahead and run the cell. This will produce an error because there is no key associated with the sports variable. To properly store this list, you would write the following:
>>> sports_dict = dict( { "sports" : sports } )

Download 2,61 Mb.

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