0% found this document useful (0 votes)
10 views4 pages

Neha Py02 (2-3) - 1

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

Neha Py02 (2-3) - 1

Uploaded by

lsomesh448
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Ankush bhakare

Roll No.: 14
Assignment No.: 02(2.3)
Assignment Title: Develop programs to learn different types of structures (list, dictionary,
tuples) in python
***************************************************************************
Code:
2.3Dictionary:
2.3.1 Create and display Dictionary in python :-
# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)
# Creating a Dictionary
# with dict() method
Student = ({1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'})
print("\nDictionary with the use of dict(): ")
print(Student)
#Creating Dictionary:
Student_List= {1: 'Neha', 2:'Suresh', 3:'Anil', 4:'Ketan', 5:'Pallavi', 6:'Mukesh', 7: 'Bhavesh'}
print(Student_List)

Output:
Empty Dictionary:
{}
Dictionary with the use of dict():
{1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'}
{1: 'Neha', 2: 'Suresh', 3: 'Anil', 4: 'Ketan', 5: 'Pallavi', 6: 'Mukesh', 7: 'Bhavesh'}

2.3.2 Adding dictionary values :-


# Adding new item in Dictionary
Student_List = {1: 'Bhavesh', 2: 'Suresh', 3: 'Tejas', 4: 'Ketan', 5: 'Priyanka', 6: 'Mukesh', 7:
'Dinesh'}
print(Student_List)
Student_List[8] = 'Tejas'
print(Student_List)

Output:
{1: 'Bhavesh', 2: 'Suresh', 3: 'Tejas', 4: 'Ketan', 5: 'Priyanka', 6: 'Mukesh', 7: 'Dinesh'}
{1: 'Bhavesh', 2: 'Suresh', 3: 'Tejas', 4: 'Ketan', 5:
'Priyanka', 6: 'Mukesh', 7: 'Dinesh', 8: 'Tejas'}

2.3.3 Accessing Values in Dictionary :-


#Accessing value in dictionary
Student = {'Name':'Neha','Age':21,'Roll_No':151}
print("Student['Name']:",Student['Name'])
print("Student['Roll_No']:",Student['Roll_No'])
Output:
Student['Name']: Neha Wani
Student['Roll_No']: 151

2.3.4 Print Dictionary using Loop :-


Student_List = {1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'}
print("original dictionary:",Student_List)
for k, v in Student_List.items():
print(k,v)

Output:
original dictionary: {1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'}
1 Bhavesh
2 Mukesh
3 Anil
4 Ketan

2.3.5 Nested Dictionary :-


myfamily = {
"child1" : {
"name" : "bhavesh",
"year" : 2004
},
"child2" : {
"name" : "Mukesh",
"year" : 2007
},
"child3" : {
"name" : "Pallavi",
"year" : 2011
}
}
print(myfamily)

Output:
{'child1': {'name': 'bhavesh', 'year': 2004}, 'child2': {'name': 'Mukesh', 'year': 2007}, 'child3': {'name':
'Pallavi', 'year': 2011}}

2.3.6 Updating Dictionary :-


book={
"Book name": "Authobiography of yogi",
"Author": "Paramhans yogananda ji",
"year": 1964
}
book.update({"Prize": "300"})
print(book)

Output:
{'Book name': 'Authobiography of yogi', 'Author': 'Paramhans yogananda
ji', 'year': 1964, 'Prize': '300'}

2.3.7 Delete Dictionary Elements :-


Student_list = {1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'}
print("original dictionary:",Student_list)
del Student_list[2]
print("the dictionary after removing an element:",Student_list)

Output:
original dictionary: {1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'}
the dictionary after removing an element: {1: 'Bhavesh', 3: 'Anil', 4:
'Ketan'}

2.3.7 Built-in Dictionary methods / functions :-


1 .clear( ) :-

book={
"Book name": "Authobiography of yogi",
"Author": "Paramhans yogananda ji",
"year": 1964
}
book.clear()
print(book)

Output:
{}

2.len() :-
dict = {'Name': 'Bhavesh', 'Age': 21};
print ("Length : %d" % len (dict))

Output:
2

3.pop() :-
book={
"Book name": "Authobiography of yogi",
"Author": "Paramhans yogananda ji",
"year": 1964
}
book.pop("Author")
print(book)
Output:
{'Book name': 'Authobiography of yogi', 'year': 1964}
4. popitem( ) :-
book={
"Book name": "Authobiography of yogi",
"Author": "Paramhans yogananda ji",
"year": 1964
}
book.popitem()
print(book)
Output:
{'Book name': 'Authobiography of yogi', 'Author': 'Paramhans yogananda
ji'}

5.keys():-
book={
"Book name": "Authobiography of yogi",
"Author": "Paramhans yogananda ji",
"year": 1964
}
x = book.keys()
print(x)

Output:
dict_keys(['Book name', 'Author', 'year'])

6. values() :-
book={
"Book name": "Authobiography of yogi",
"Author": "Paramhans yogananda ji",
"year": 1964
}
x = book.values()
print(x)

Output:
dict_values(['Authobiography of yogi', 'Paramhans yogananda ji', 1964])

7. items() :-
book={
"Book name": "Authobiography of yogi",
"Author": "Paramhans yogananda ji",
"year": 1964
}
x = book.items()
print(x)

Output:-
dict_items([('Book name', 'Authobiography of yogi'), ('Author',
'Paramhans yogananda ji'), ('year', 1964)])

You might also like