Python Dictionaries

Python dictionary is nothing but a key value pair here key denotes the word and value denote the meaning . Key value is provided in the dictionary to make it more optimized . Each key value pair is separated by colon (:) and each key is separated by comma (,).

Dictionary in python is as similar to dictionary in real world . Here key must be unique and immutable (can not change) .

#creating a Dictionary

dict = {}

print(type(dict)) # dict is dictionary or not or type of dict

#creating a small dictionary

dict1 = {“Utkarsh”:”shukla”,”red”:”securium”,”rohan”:”eat burger”,”Dinesh”:”want money”}

print(dict1)

dict1[“red”] #access a particular key

#we can make dictionary in another dictionary .

dict2 = {“utkarsh”:”shukla”,”shivam”:”milk”,”Neha”:{“senior”:”ma’am”,”hobby”:”programming”}}

print(dict2) # print the hole dict2 key value

print(dict2[“Neha”]) #print Neha dictionary

See the program and output :

#create empty dictionary

dict = {}

print(dict)

print(dict)

#adding element in empty dictionary

dict[0] = ‘hello’

dict[1] = ‘rokey’

dict[2] = 2

print(“dictionary after adding this element “)

print(dict)

See the program and output :


why python is a simple and very effective language ? let’s take a example.

Make a swapping program in python :

# traditional method

a = 10

b = 18

temp = a

a = b

b = temp

print(a,b)

#python way to create swapping program

a = 2

b = 45

a,b =b,a

print(a,b)

Here we see that what is dictionary and how is work and some functions in dictionary with live examples .

In next blog we cover if and else condition .

Leave a Comment

Your email address will not be published. Required fields are marked *