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

Expt 3

python experiment documents

Uploaded by

Asnan Mullani
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)
24 views6 pages

Expt 3

python experiment documents

Uploaded by

Asnan Mullani
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

Name : Asnan Mullani

Roll no: 55
Subject: Software Proficiency Program -I
Div: B
Year- TY CSE

Experiment no 3
Title : To implement Sets and dictionaries in python

Sets in Python
A Python set is the collection of the unordered items. Each element in the set
must be unique, immutable, and the sets remove the duplicate elements. Sets
are mutable which means we can modify it after its creation.
Unlike other collections in Python, there is no index attached to the elements of
the set, i.e., we cannot directly access any element of the set by the index.
However, we can print them all together, or we can get the list of elements by
looping through the set.

Creating a set
The set can be created by enclosing the comma-separated immutable
items with the curly braces {}. Python also provides the set() method,
which can be used to create the set by the passed sequence.

Example 1: Using curly braces

1. Days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday

", "Sunday"}

2. print(Days)

3. print(type(Days))

4. print("looping through the set elements ... ")

5. for i in Days:

6. print(i)

Example 2: Using set() method

1. Days = set(["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Satur

day", "Sunday"])

2. print(Days)
3. print(type(Days))

4. print("looping through the set elements ... ")

5. for i in Days:

6. print(i)

It can contain any type of element such as integer, float, tuple etc. But mutable
elements (list, dictionary, set) can't be a member of set.

Adding items to the set


Python provides the add() method and update() method which can be used to
add some particular item to the set. The add() method is used to add a single
element whereas the update() method is used to add multiple elements to the
set. Consider the following example.

Example: 1 - Using add() method

1. Months = set(["January","February", "March", "April", "May", "June"])

2. print("\nprinting the original set ... ")

3. print(months)

4. print("\nAdding other months to the set...");

5. Months.add("July");

6. Months.add ("August");

7. print("\nPrinting the modified set...");

8. print(Months)

9. print("\nlooping through the set elements ... ")

10. for i in Months:

11. print(i)

To add more than one item in the set, Python provides


the update() method. It accepts iterable as an argument.

Consider the following example.

Example - 2 Using update() function

1. Months = set(["January","February", "March", "April", "May", "June"])

2. print("\nprinting the original set ... ")


3. print(Months)

4. print("\nupdating the original set ... ")

5. Months.update(["July","August","September","October"]);

6. print("\nprinting the modified set ... ")

7. print(Months);

Removing items from the set


Python provides the discard() method and remove() method which can
be used to remove the items from the set. The difference between these
function, using discard() function if the item does not exist in the set then
the set remain unchanged whereas remove() method will through an
error.

Consider the following example.

Example-1 Using discard() method

1. months = set(["January","February", "March", "April", "May", "June"])

2. print("\nprinting the original set ... ")

3. print(months)

4. print("\nRemoving some months from the set...");

5. months.discard("January");

6. months.discard("May");

7. print("\nPrinting the modified set...");

8. print(months)

9. print("\nlooping through the set elements ... ")

10. for i in months:

11. print(i)

Dictionaries in Python
Dictionaries are a useful data structure for storing data in Python because
they are capable of imitating real-world data arrangements where a
certain value exists for a given key.
The data is stored as key-value pairs using a Python dictionary.

o This data structure is mutable

o The components of dictionary were made using keys and values.

o Keys must only have one component.

o Values can be of any type, including integer, list, and tuple.

A dictionary is, in other words, a group of key-value pairs, where the values can
be any Python object. The keys, in contrast, are immutable Python objects, such
as strings, tuples, or numbers. Dictionary entries are ordered as of Python
version 3.7. In Python 3.6 and before, dictionaries are generally unordered.

Creating the Dictionary


Curly brackets are the simplest way to generate a Python dictionary,
although there are other approaches as well. With many key-value pairs
surrounded in curly brackets and a colon separating each key from its
value, the dictionary can be built. (:). The following provides the syntax for
defining the dictionary.

1. Dict = {"Name": "Gayle", "Age": 25}

In the above dictionary Dict, The keys Name and Age are the strings which
comes under the category of an immutable object.

Accessing the dictionary values


To access data contained in lists and tuples, indexing has been studied.
The keys of the dictionary can be used to obtain the values because they
are unique from one another. The following method can be used to access
dictionary values.

Code

1. Employee = {"Name": "Dev", "Age": 20, "salary":45000,"Company":"WIPRO"}

2. print(type(Employee))

3. print("printing Employee data .... ")

4. print("Name : %s" %Employee["Name"])

5. print("Age : %d" %Employee["Age"])


6. print("Salary : %d" %Employee["salary"])

7. print("Company : %s" %Employee["Company"])

Adding Dictionary Values


The dictionary is a mutable data type, and utilising the right keys allows
you to change its values. Dict[key] = value and the value can both be
modified. An existing value can also be updated using the update()
method.

Example - 1:
Code

1. # Creating an empty Dictionary

2. Dict = {}

3. print("Empty Dictionary: ")

4. print(Dict)

5.

6. # Adding elements to dictionary one at a time

7. Dict[0] = 'Peter'

8. Dict[2] = 'Joseph'

9. Dict[3] = 'Ricky'

10. print("\nDictionary after adding 3 elements: ")

11. print(Dict)

12.

13. # Adding set of values

14. # with a single Key

15. # The Emp_ages doesn't exist to dictionary

16. Dict['Emp_ages'] = 20, 33, 24

17. print("\nDictionary after adding 3 elements: ")

18. print(Dict)

19.

20. # Updating existing Key's Value

21. Dict[3] = 'JavaTpoint'


22. print("\nUpdated key value: ")

23. print(Dict)

Deleting Elements using del Keyword


The items of the dictionary can be deleted by using the del keyword.

You might also like