ADVANCED PROGRAMMING CONCEPTS - Python
Subject : ADVANCED PROGRAMMING CONCEPTS - Python
To solve assignments Please join Google Classroom.
Invitation of Google Classroom has been sent to your official email id.
Please accept the google classroom invitation through official email id.
Or directly by entering Class code of Google Classroom also you can join Google Class room to solve assignments.
Class code of Google Classroom : 3ojdsjm
With kind regards,
SD Bhosale
- 10 minutes before
To know your attendance : Click here
Recorded Google Meet Session:
High Resolution Video:
Today's Programs:
---------------------------------
# Example
# You can also use the values() method to return
# values of a dictionary:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
for x in thisdict.values():
print(x)
Output:
Maruti
k10
1000
-------------------------
# Example
# Loop through both keys and values,
# by using the items() method:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
# for y,x in thisdict.items():
# print(x,y)
for x,y in thisdict.items():
print(x,":",y)
Output:
brand : Maruti
model : k10
cc : 1000
-----------------------------
# Check if Key Exists
# To determine if a specified key is present in
# a dictionary use the in keyword:
# Example
# Check if "model" is present in the dictionary:
# Finding book in the library book will be the key and cupboard will be value
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
if "brand" in thisdict:
print("Yes, cc is one of the keys in the thisdict dictionary")
print("Yes key is present")
Output:
Yes, cc is one of the keys in the thisdict dictionary
Yes key is present
---------------------------------------
# Dictionary Length
# To determine how many items (key-value pairs) a dictionary has,
# use the len() function.
# Example
# Print the number of items in the dictionary:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000,
"Color":"Red"
}
print(len(thisdict))
Output:
4
-------------------------------------
# Adding Items
# Adding an item to the dictionary is done by
# using a new index key and assigning a value to it:
# Example
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
thisdict["color"] = "Red"
thisdict["price"]=450000
print(thisdict)
Output:
# Adding Items
# Adding an item to the dictionary is done by
# using a new index key and assigning a value to it:
# Example
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
thisdict["color"] = "Red"
thisdict["price"]=450000
print(thisdict)
Output:
{'brand': 'Maruti', 'model': 'k10', 'cc': 1000, 'color': 'Red', 'price': 450000}
----------------------------
# Removing Items
# There are several methods to remove items from a dictionary:
# Example
# The pop() method removes the item with the specified key name:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
thisdict.pop("model")
print(thisdict)
#thisdict.pop() #TypeError: pop expected at least 1 argument, got 0
Output:
{'brand': 'Maruti', 'cc': 1000}
------------------------
# Example
# The popitem() method removes the last inserted item
# (in versions before 3.7, a random item is removed instead):
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
thisdict.popitem()
print(thisdict)
Output:
{'brand': 'Maruti', 'model': 'k10'}
----------------
# Example
# The del keyword removes the item with the specified key name:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
del thisdict["model"]
print(thisdict)
Output:
{'brand': 'Maruti', 'cc': 1000}
-------------------------------
# Example
# The del keyword can also delete the dictionary completely:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
del thisdict
print(thisdict) #this will cause an error because "thisdict" no longer exists.
Output:
Traceback (most recent call last):
File "C:/Users/Hp/PycharmProjects/python_dictionary/15.py", line 10, in <module>
print(thisdict) #this will cause an error because "thisdict" no longer exists.
NameError: name 'thisdict' is not defined
-----------------------------
# Example
# The clear() method empties the dictionary:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
thisdict.clear()
print(thisdict)
Output:
{}
------------------------------
# Copy a Dictionary
# You cannot copy a dictionary simply by typing dict2 = dict1,
# because: dict2 will only be a reference to dict1,
# and changes made in dict1 will automatically also be made in dict2.
# There are ways to make a copy,
# one way is to use the built-in Dictionary method copy().
# Example
# Make a copy of a dictionary with the copy() method:
thisdict = {
"brand": "Maruti",
"model": "k10",
"cc": 1000
}
mydict = thisdict.copy()
print(mydict)
print(thisdict)
Output:
{'brand': 'Maruti', 'model': 'k10', 'cc': 1000}
{'brand': 'Maruti', 'model': 'k10', 'cc': 1000}
----------------------
No comments:
Post a Comment