Access Items from a Dictionary in Python (Get) - Python Tutorial for Beginners

Publié le: 22 mars 2021
sur la chaîne: Digital Academy
401
13

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Access Items from a Dictionary in Python (Get)

Dictionary elements must be accessible, somehow! If you do not get them by index, then how would you get them?

While indexing is used with other container types to access values, dictionary use keys. Therefore, values can be retrieved from a dictionary by specifying its corresponding key, in square brackets [].

my_dict = {
'age': 27,
'job': 'Youtuber',
'name': 'Digital Academy'
}

print(my_dict['name'])
OUTPUT: Digital Academy


○ Get Item from Dictionary in Python and avoid Exception

Nonetheless, If you refer to a key that is not in the dictionary, you will get an exception. To avoid such exception, you can use the special dictionary get() method. The difference while using get() is that it returns None, instead of KeyError, if the key is not found - and returns the value associated to its key, if key is in the dictionary.

Example: Try to print the value of the key ‘salary’

print(my_dict['salary'])
OUTPUT: KeyError: 'salary'

Example: Print values of the keys ‘name’ and ‘salary’

Key present
print(my_dict.get('name')) # OUTPUT: Digital Academy

Key absent
print(my_dict.get('salary')) # OUTPUT: None


Let's play this video, stick around and watch until the end of this video! 👍🏻

Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python -    • DATA TYPES in Python (Numbers, String...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assi...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF,...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Co...  

📖 Blog: http://digital.academy.free.fr/blog

📖 [FULL Course] HOW TO Learn Python? Python Tutorial for Beginners:    • 🐍 Python 101: Learn Python Basics for...  

📖 [PLAYLIST] Complete Python Development Course for Beginners: http://digital.academy.free.fr/playli...

🧑‍🎓 [COURSE] http://digital.academy.free.fr/courses

📘 [BOOK] Python for Absolute Beginners: https://amzn.to/3NvyOWV

🛒 Shopping and Discounts: http://digital.academy.free.fr/store

💌 Weekly Newsletter for Developers: https://www.getrevue.co/profile/digit...

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
✧ http://digital.academy.free.fr/
✧   / digitalacademyy  
✧   / digitalacademyfr  
✧   / digital_academy_fr  
✧    / digitalacademyonline  

♡ SUPPORT US ♡
✧ http://digital.academy.free.fr/join
✧ http://digital.academy.free.fr/store
✧ http://digital.academy.free.fr/donate
✧ http://digital.academy.free.fr/subscribe
✧   / digital_academy  
✧ https://www.buymeacoffee.com/digital_...

***


Sur cette page du site, vous pouvez voir la vidéo en ligne Access Items from a Dictionary in Python (Get) - Python Tutorial for Beginners durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Digital Academy 22 mars 2021, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 401 fois et il a aimé 13 téléspectateurs. Bon visionnage!