How to Create a Dictionary in Python? - Python Tutorial for Beginners

Pubblicato il: 20 marzo 2021
sul canale di: Digital Academy
590
14

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

🖥️ How to Create a Dictionary in Python?

Creating a dictionary in Python is as simple as placing "key: value" items inside curly braces {}, and separated by comma. Each key is separated from its associated value by a colon ":".

Let's have a closer look at the syntax of a Dictionary in Python

Here is an example, in which you want to store an employee record. First, Let's declare an empty dictionary - using the curly braces {}. Eventually, you will then add all of the personal information about the employee, so you can store its information and then access it later.

employee = {
'age': 27,
'city': 'Paris',
'job': 'Youtuber',
'name': 'Digital Academy',
'email': 'digital.academy@free.fr'
}

You can also convert two-value sequences into a dictionary, using the built-in function dict() in Python. The first item inside each sequence is then used as the key, and the second as the value.

Using dict() constructor
my_dict = dict({'name':'apple', 2:'strawberry'})

From sequence, having each item as a pair
my_dict = dict([('name','Digital Academy'), ('age',27)]) # List of Tuples
my_dict = dict((['name','Digital Academy'], ['age',27])) # Tuple of Lists

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

When the keys are simple strings, it is sometimes easier to specify key:value pairs, using keyword arguments.

my_dict = dict(
age = 27,
job = 'Youtuber',
name = 'Digital Academy'
)

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

---

Dictionaries are pretty straightforward, but here are a few points you should be aware of:

○ Keys must be unique

A key can appear in a dictionary only once. Even if you specify a key more than once during the creation of a dictionary, the last value for that key becomes the associated value.

my_dict = {
'age': 27,
'name': 'Jérémy',
'name': 'Digital Academy'
}

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


○ Key must be Immutable type

You can use any object of immutable type as dictionary keys – such as numbers, strings, booleans, tuples. Otherwise an exception is raised, when mutable object is used as a key.

my_dict = {
0: 1, # Number
True: 'a', # Boolean
(2,2): 25, # Tuple
[2,2]: 25, # TypeError: unhashable type: 'list'
'name': 'Digital Academy' # String
}


○ Value can be of any type

There are no restrictions on dictionary values. A dictionary value can be any type of object, and can even appear in a dictionary, multiple times.

Values of different datatypes
my_dict = {
'a': [1,2,3], # List
'b': {1,2,3} # Set
}

Duplicate values
my_dict = {
'a': [1,2], # List a
'b': [1,2], # List b
'c': [1,2] # List c
}


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, Strings, Li...  
○ Operators in Python -    • OPERATORS in Python (Arithmetic, Assignmen...  
○ IF Statements in Python -    • CONDITIONAL Statements in Python (IF, ELIF...  
○ FOR Loops in Python -    • FOR Loop in Python (Syntax, Break, Continu...  

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

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

📖 [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_...

***


In questa pagina del sito puoi guardare il video online How to Create a Dictionary in Python? - Python Tutorial for Beginners della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Digital Academy 20 marzo 2021, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 590 volte e gli è piaciuto 14 spettatori. Buona visione!