0% found this document useful (0 votes)
9 views26 pages

1.2 - Python Sets

Uploaded by

harsheenkaur1811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views26 pages

1.2 - Python Sets

Uploaded by

harsheenkaur1811
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Sets

1
Error/
Information Flashback Class Exercise
Warning

2
Python Sets
Creation, Functions, Methods

3
Set Creation

4
Set

● Sets are very similar to list & tuple data structures


● Used to store multiple values in a single variable
● Sets are unordered collections of homogeneous or heterogeneous elements
● Sets do not allow duplicates
● Set items cannot be changed. You can add new items or remove existing items

5
Set
Set supports operations like union, intersection, difference & symmetric difference

6
Set

Create a set using { }.

7
Set

Note how sets ignore duplicate values.

8
TRY IT

Consider the list: [12, 45, 65, 65, 87, 98, 65]

Convert the above list to a set type object.

9
TRY IT

Create a blank set. Check the type of the object using the type() function.

10
Built-in Functions

11
len()

Counting the number of elements in a set

Notice the number of elements printed for


a set created with duplicate items

12
max()

Get the maximum value from a set

Numeric
values

Character
values

13
min()

Get the minimum value from a list

Numeric
values

Character
values

14
Set Methods

15
Add elements to a set

16
Delete elements from a set

17
Difference between remove() and discard() method

Note: The discard() method does not


throw an error if the element does not
exist in the set object
18
Delete arbitrary element from a set

Note: The pop() method does not take any


input parameters. Delete an element
arbitrarily.

19
Delete a set

20
Set operations : union

21
Set operations : intersection

22
Set operations : difference

23
Set operations : symmetric difference

24
We learnt the collection objects...

List Tuple Dictionary Set

Create using [] () {} {}

Index using [] [] [key]

Editable

25
Thank You

You might also like