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

Sets

Sets in Python are unordered collections defined by curly brackets that store multiple items without duplicates. They are unchangeable, meaning items cannot be modified directly, but items can be added or removed. Various methods such as .add(), .remove(), and .union() are available for manipulating sets and checking relationships between them.

Uploaded by

n02426633q
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 views16 pages

Sets

Sets in Python are unordered collections defined by curly brackets that store multiple items without duplicates. They are unchangeable, meaning items cannot be modified directly, but items can be added or removed. Various methods such as .add(), .remove(), and .union() are available for manipulating sets and checking relationships between them.

Uploaded by

n02426633q
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
You are on page 1/ 16

Sets

 Written with curly brackets. {}


 Used to store multiple items in a single
variable.
 A set is a collection which
is unordered, “unchangeable”,
and unindexed.
 Note: Set items are unchangeable, but
you can remove items and add new
items.

 Note: Sets are unordered, so you cannot


be sure in which order the items will
appear.
Unordered
 Items in a set do not have a defined
order.
 Set items can appear in a different order
every time you use them

Unindexed
 Set items cannot be referred to by index
or key.
 E.g. thisset [0]

Access Set Items


 An index or a key does not work in
sets.
 But you can loop through the set items
using a for loop,
 or check if a specified value is present
in a set,
o by using the in keyword.

Checking if item is present in set

 Check if "banana" is present in the set:


Unchangeable
 Once a set is created, you cannot change
its items, e.g. changing values
o but you can remove items
o and add new items.

Duplicates Not Allowed


 Sets cannot have two items with the
same value.
 Duplicate values will be ignored

 True and 1 are considered the same


value in sets.
 Similarly, False and 0 are considered the
same value.

len() function
 Returns no of items a set has
Set () Constructor
 Used to also create a set from a list.

Add Items --. add ()


 To add one item to a set, use
the .add() method.
Add Sets -- set.update(newset)
 To add items from another set into the
current set,
o use the .update () method.
 With .update (), you can add any
iterable, list, etc.
 E.g. Add elements
from tropical into thisset:
Remove Set Items -- .remove()
 To remove an item in a set,
o use the .remove(),
o or the .discard() method.
 E.g. Remove "banana" from thisset
Loop Sets
 E.g. Loop through the set, and print the
values:

Join Two Sets


 There are several ways to join two or
more sets in Python.
i. .union() method
 returns a new set containing all
items from both sets,
ii. .update() method
 inserts all the items from one set
into another:
intersection() method
 returns a new set,
o containing items present in both
sets.
o E.g. keep items in both x & y, store
results in z
intersection_update() method
 returns items present in both sets and
updates in the one of the already
existing sets.
 E.g. Keep items in both x & y, store
results in x
.symmetric_difference() method
 returns a new set, that contains only the
elements that are NOT present in both sets.
 Stores the result in a new set.

 E.g. Return a set that contains all items from


sets x & y, except items that are present in
both, store the result in z.
.symmetric_difference_update() method
 Returns only elements NOT present in both
sets.
 Stores the result in one of the already existing
sets.
 E.g. Keep the items that are not present in both
x & y, store the result in x.

.copy() Method
 Copies a set to another variable
 e.g. Copy the fruits set:

.issubset () Method
 Returns True if all items in set x are a
subset of set y:
.issuperset() Method
 Returns True if x is a superset of y,
 i.e. all items in set y are contained in
set x:

You might also like