Dictionary in Python

 Dictionary

  1. It is a mutable data-type (you can change the values in-place, don't worry we will discuss further about mutability).
  2. Contains key-value pair.
  3. Values are accessed using keys, not index unlike in list and tuples.
  4. It is an ordered data-type.
  5. Dictionary doesn't allow duplicate keys.
Now let's start practical session where we will learn how to create an empty dictionary, how to access values from a dictionary using keys, functions of dictionary:

Create an empty dictionary:

    There are different ways to create an empty dictionary in python:

    1. Using Curly Braces:
student = {}
print(student) #output: {}
print(type(student))   #output: <class 'dict'>
    
    2. Using a built-in function dict():
student = dict()
print(student) #output: {}
You saw that we were able to create an empty dictionary using both methods.
Both set and dictionary are denoted by {}.
In first block, I have used a type() function (returns data-type of object) to show you that a dictionary gets always created on using curly braces method and not set.

Create a dictionary with key-value pairs:

    Let's create a dictionary to store a student's details:
student = {"name": "sam", "age": 26, "roll_no": 10, "marks_list": [58, 70, 80]}
print(student)   #output: {"name": "Sam", "age": 26, "roll_no": 10, "marks_list": [58, 70, 80]}
This time we have created dictionary 'student' with the key-value pairs inside it.

Create an empty dictionary and add key-value pairs:

    Now, we will create an empty dictionary first and then we will insert key-value pairs inside it.

student = {}
print(student) #output: {}

#let's add key-value pairs in student dictionary which we just created.

student["name"] = "Sam"
student["age"] = 26
student["roll_no"] = 10
student["marks_list"] = [58, 70, 80]
student["course"] = {"frontend": ["HTML", "CSS", "JS"], "backend": "python"}
print(student)    #output: {"name": "Sam", "age": 26, "roll_no": 10, "marks_list": [58, 70, 80], "course": {"frontend": ["HTML", "CSS", "JS"], "backend": "python"}}
A dictionary can store multiple values in multiple data-types like int, float, string, tuple, list and even another dictionary itself and in above example I have added values of multiple data-types including list (stored on "marks_list" key) and a dictionary (stored on "course" key)  itself.

Accessing values from dictionary using keys:

    As I stated above, values from a dictionary can only be accessed using keys and not using index unlike in list and tuples.
So now we have already learned how to create dictionary using different methods, it's time to access values from the dictionary we created just a while ago(or any other dictionary you might have created during learning).

#accessing values from 'student' dictionary using keys.

print(student["name"])
print(student["age"])
print(student["roll_no"])
print(student["marks_list"])

#output:
Sam
26
10
[58, 70, 80]
Accessing values from the list stored as "marks_list" in dictionary:

Comments

Popular posts from this blog

Bihar BCECEB Recruitment 2024 | BCECEB Senior Resident / Tutor Online Form 2024

UPSSSC Junior Analyst Food Online Form 2024