Day #33 - Dictionaries In Python

Day #33 - Dictionaries In Python

ยท

3 min read

Introduction

Welcome to my 33rd blog post. Till now we have covered many datatypes like Lists, Tuples and Sets in python. Today I learned about another similar datatype that helps you take elements in the "key: value" pair form. This data type is none other than DICTIONARIES in python. Let's dive into more details.

So let's get started......

What are Dictionaries?

  • They are ordered collection of data items

  • They store multiple items in a single variable.

  • Dictionary stores items in "key: value" form.

  • The items are separated by commas and enclosed within curly brackets {}.

Example -

dic={
  "Chintan": "Jain",
  "Age": 30,
  "gender": "male"}

print(dic)
{'Chintan': 'Jain', 'Age': 30, 'gender': 'male'}

Example -

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info) 

print(info.get('name2')) #prints None if the key is not present 
print(info['names2']) #similar to above, but this statement raises error
{'name': 'Karan', 'age': 19, 'eligible': True}
None
Traceback (most recent call last):
  File "main.py", line 13, in <module>
    print(info['names2']) #similar to above, but this statement raises error 
KeyError: 'names2'

Dictionary Methods

Accessing single values:

Access dictionary values by mentioning keys either in square brackets or by using the get() method.

Example -

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info['name'])
print(info.get('eligible'))
Karan
True

Accessing multiple values:

Print all the values in the dictionary using the values() method.

Example -

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.values())
dict_values(['Karan', 19, True])

Accessing keys:

Print all the keys in the dictionary using the keys() method.

Example -

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.keys())
dict_keys(['name', 'age', 'eligible'])

Accessing key-value pairs:

Print all the key-value pairs in the dictionary using the items() method.

Example -

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info.items())
dict_items([('name', 'Karan'), ('age', 19), ('eligible', True)])

Using for loops to access values:

We can access key: value pairs using for loops on dictionaries.

Example -

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info) 

for key in info.keys():
  print(info[key])

for key in info.keys():
  print(f"The value corresponding to key {key} is ", info[key])
{'name': 'Karan', 'age': 19, 'eligible': True}
Karan
19
True
The value corresponding to key name is  Karan
The value corresponding to key age is  19
The value corresponding to key eligible is  True

Resources Used

You can watch the video of Day#33 by clicking on the below link ๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡๐Ÿ‘‡

Conclusion

Thanks, guys for going through this blog post. Through this Blog post, we understood how we can create key: value pairs in the dictionary form and store the data. I also learned about how we can access the data from the dictionary using various methods.

Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of python coding. This is it for Day #33

See you in the next one.....


About Me

Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.

I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.

So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.


ย