Search

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 (,).

A 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:

Make a swapping program in Python:

# traditional method

a = 10

b = 18

temp = a

a = b

b = temp

print(a,b)

 

In the next blog, we cover if and else conditions.

Table of Contents

Social Media
Facebook
Twitter
WhatsApp
LinkedIn