Experiment - 7
Experiment - 7
Aim / Title: To understand the basic concept of Sets and Dictionaries in python
Problem Statement:
Hardware requirements:
Processors: Intel Atom® processor or Intel® Core™ i3 processor.
Disk space: 1 GB
Software requirements:
Operating systems: Windows* 7 or later, macOS, and Linux.
Python versions: 2.7.X, 3.6.X. , Jupyter Notebook
Theory:
Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python,
which can simulate the real-life data arrangement where some specific value exists for some particular key. It is
a mutable data-structure. The dictionary is defined into element Keys and values.
o Keys must be a single element
o Value can be any type such as list, tuple, integer, etc.
In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any
Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple.
The dictionary can be created by using multiple key-value pairs enclosed with the curly brackets {}, and each key
is separated from its value by the colon (:).The syntax to define the dictionary is given below.
Syntax:
In the above dictionary Dict, The keys Name and Age are the string that is an immutable object. Let's see an
example to create a dictionary and print its content.
1. Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE"}
2. print(type(Employee))
3. print("printing Employee data .... ")
4. print(Employee)
Output
<class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}
Output :
Output :
Output :
my_new_dict = {}
print(type(my_new_dict))
Ans : We can access the items of a dictionary by referring to its key name, inside
square brackets:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
Ans : Adding an item to the dictionary is done by using a new index key and assigning
a value to it:
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict["color"] = "red"
print(thisdict)
Ans : The del keyword can also be used to delete a list, slice a list, delete a dictionaries, remove key-value pairs
from a dictionary, delete variables, etc.
class Sample_class:
some_variable = 20
# method of the class
def my_method(self):
print("GeeksForGeeks")
# check if class exists
print(Sample_class)
# delete the class using del keyword
del Sample_class
# check if class exists
print(Sample_class)