0% found this document useful (0 votes)
2 views6 pages

Experiment - 7

The document outlines Experiment No. 7, which focuses on understanding Sets and Dictionaries in Python through various programming tasks. It includes objectives, prerequisites, hardware and software requirements, and theoretical explanations of dictionaries. Additionally, it provides sample code, output expectations, and common questions related to dictionary operations.

Uploaded by

sujeetsoni018780
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)
2 views6 pages

Experiment - 7

The document outlines Experiment No. 7, which focuses on understanding Sets and Dictionaries in Python through various programming tasks. It includes objectives, prerequisites, hardware and software requirements, and theoretical explanations of dictionaries. Additionally, it provides sample code, output expectations, and common questions related to dictionary operations.

Uploaded by

sujeetsoni018780
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/ 6

EXPERIMENT NO.

Aim / Title: To understand the basic concept of Sets and Dictionaries in python

Problem Statement:

Sets and Dictionaries


Write program
1) To remove an item from a set if it is present in the set
2) To create an intersection of sets.
3) To Add a key to a dictionary
4) To check whether a given key already exists in a dictionary.
5) To merge two Python dictionaries.
Objectives: To perform different operations on Sets and Dictionary in python

Outcomes: Students will be able to learn to Sets and Dictionary implementation

Prerequisite: Basic knowledge of Python

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.

Creating the dictionary

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:

1. Dict = {"Name": "Tom", "Age": 22}

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'}

Program: //Paste Code and Output here : -

1) To remove an item from a set if it is present in the set

Output :

2) To create an intersection of sets.


Output :

3) To Add a key to a dictionary

Output :

4) To check whether a given key already exists in a dictionary.


Output :

5) To merge two Python dictionaries.

Output :

Sample Viva Questions and Answers:

1. Create an empty dictionary ?


Ans : In Python to create an empty dictionary, we can assign no elements in curly brackets {}. We can also create
an empty dictionary by using the dict() method which is a built-in function in Python and takes no arguments.

my_new_dict = {}

print("Created empty dictionary:",my_new_dict)

print(type(my_new_dict))

2. How do you access dictionary values ?

Ans : We can access the items of a dictionary by referring to its key name, inside
square brackets:

Get the value of the "model" key:

thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]

3. How do you add values in a dictionary ?

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)

4. How to Delete elements using del keyword ?

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)

5. Write down 5 Built-in Dictionary methods.

Ans : clear() Removes all the elements from the dictionary


copy() Returns a copy of the dictionary
get() Returns the value of the specified key
keys() Returns a list containing the dictionary's keys
pop() Removes the element with the specified key
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary

Roll Name of Date of Date of Grade Sign of Sign of


No. Student Performance Evaluation Student Faculty

0818CS Sujeet Kumar 11/10/2022 17/10/2022


201175 Soni

You might also like