Python Tutorial: Python Dictionary

Wednesday, 12 August 2020

Python Dictionary

 Dictionary 

Dictionary : A python dictionary is a group of key-value pairs. The elements in a dictionary are indexed by keys. Keys in a dictionary are required to be unique. Dictionaries are also called associative arrays or mapping or hashes.

Creating Dictionary:

To create a dictionary, you need to include the key: value pairs in curly braces as per following syntax :

<dinctinary-name>={ key: value , key: value , key: value ………………}

Example:

 

Teachers={“Sachin”: “Maths”, “Ankita” : “English”, “Gurmeet” : “Accounts”}

 

Kay-Values pair

Key

Values

“Sachin”: “Maths”

“Sachin”

“Maths”,

“Ankita” : “English”

“Ankita”

“English”

“Gurmeet” : “Accounts”

“Gurmeet”

“Accounts”

 

Student ={“Ankita”: 450, “Vanshika”:467, “Jyotsna” : 489}

 

Kay-Values pair

Key

Values

“Ankita”: 450

“Ankita”

450

“Vanshika” : 467

“Vanshika”

467

“Jyotsna” : 489

“Jyotsna”

489

Remember that

Ø  The curly brackets mark the beginning and end of the dictionary

Ø  Each entry (Key : value) consists of a pair separated by colon- the key and corresponding value is given by writing color (:) between them,

Ø  The key-value pairs are separated by commas (,) .

 

How can access data from a dictionary?

While indexing is used with other data types to access values, a dictionary uses . Keys can be used either inside square brackets [] or with the get() method. keys

<dictionary-name>[<kays>]

>>> Student[“Ankita”]

450

>>> Teachers[“Ankita”]

English

>>> print(“Gurmeet Department ->”, Teachers[“Gurmeet”])

Gurmeet Department -> Accounts

Traversing a Dictionary

Traversal of a collection means accessing and processing each element of it. Thus traversing a dictionary also means the same and same is the tool for it, i.e.  the Python loops.

The for loop makes it easy to traverse or loop over the items in a dictionary.

Syntax :

                for <item> in  <Dictionary> :

                                process each item here

Example

Student={'Yash': 45,'Gauri':43,'Abhay':45,'Harsh':44}

print("Name :\tMarks")

for S in Student:  

    print(S,"\t",Student[S])

Output

 Q1. Write a program to create a contact dictionary for all your friends and then print it.

Answer:

ContactDict={“Sachin”:4565764365, “Madhav”: 5687985432, “Ravi” :3254678912, \ “Anita”:3254678490, “Deeksha”:9823739693, “Samay” :9078654323}

for name in ContactDict :

                print(name, “\t: ”, ContactDict[name])

Dictionary using dict() function : The dict() function is used to create a new dictionary with no items. For example :

>>> weekdays=dict()                     #create an empty dictionary

>>> print(weekdays)                     # prints an empty string with no values

{ }

>>> weekdays=dict(Sunday=0,Monday=1,Tuesday=2,Wednesday=3,Thursday=4)

>>>print(weekdays)

{Sunday:0,Monday:1,Tuesday:2,Wednesday:3,Thursday:4}


Input value from keyboard to make dictionary

Source Code

n=int(input("How many student record to be entered :"))

Student={} # empty dictionary

for i in range(n):

    key=input("Name of the Student :")

    value=int(input("Enter the Marks :"))

    Student[key]=value #add dictionary

print("Show Dictionary")

print(Student)

print("Print output in tabular form")

for n in Student:

    print(n,"\t",Student[n])


No comments:

Post a Comment