Neha Py02 (2-3) - 1
Neha Py02 (2-3) - 1
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'}
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'}
Output:
original dictionary: {1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'}
1 Bhavesh
2 Mukesh
3 Anil
4 Ketan
Output:
{'child1': {'name': 'bhavesh', 'year': 2004}, 'child2': {'name': 'Mukesh', 'year': 2007}, 'child3': {'name':
'Pallavi', 'year': 2011}}
Output:
{'Book name': 'Authobiography of yogi', 'Author': 'Paramhans yogananda
ji', 'year': 1964, 'Prize': '300'}
Output:
original dictionary: {1: 'Bhavesh', 2: 'Mukesh', 3: 'Anil', 4: 'Ketan'}
the dictionary after removing an element: {1: 'Bhavesh', 3: 'Anil', 4:
'Ketan'}
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)])