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



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

Deleting Information


Sometimes you’ll need to remove a certain pair. To do so, you’ll need to use the del
function:

# deleting a key/value pair from a dictionary car = { "year": 2018 } try:
del car["year"] print(car) except: print("That key does not exist")

Go ahead and run the cell. Be very careful when deleting key-value pairs. If the key you’re trying to remove doesn’t exist, it will crash the program. To avoid that problem, we use a try/except.

Looping a Dictionary


Dictionaries are iterable like lists. However, they have three different methods for doing so. You can iterate over both the keys and values together, only keys, or only values.
Looping Only Keys
To iterate through a dictionary while only accessing the keys, you’ll use the .keys() method:

# looping over a dictionary via the keys person = { "name": "John", "age": 26 } for key in person.keys( ):
print(key) print( person[key] ) # will output the value at the current key

Go ahead and run the cell. As we iterate over person, our temporary variable of key will be equal to each key name. This still gives us the ability to access each value by using our key variable.
Looping Only Values
When you don’t need to access the keys, using the .values( ) method is best:

# looping over a dictionary via the values person = { "name": "John", "age": 26 } for value in person.values( ): print(value)

Go ahead and run the cell. We won’t have access to the key names, but for this method, we’re only trying to get the values anyways. Our temporary variable value will store each value from the key-value pairs as we iterate over person.
Looping Key-Value Pairs
If you need the ability to access both the key and value, then you’ll want to use the .items() method. This approach will assign two temporary variables instead of one:

# looping over a dictionary via the key/value pair person = { "name": "John", "age": 26 } for key, value in person.items( ):
print( "{ }: { }".format(key, value) )

Go ahead and run the cell. As we iterate over person, the key-value pairs are assigned to their respective temporary variables of key and value. We now have access to both easily.

Note

The temporary variable names are usually called “k” and “v.











Download 2,61 Mb.

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