0% found this document useful (0 votes)
220 views2 pages

Set PYTHON PDF

Sets in Python are unordered collections of unique elements that can be used to perform mathematical set operations. Sets are written with curly brackets and do not allow indexing or modification of existing elements, but new elements can be added. Built-in methods allow adding, removing, and checking for elements as well as performing set operations like unions, intersections, differences and more. Common functions provide functionality like checking properties of sets, getting the length, maximum, minimum or sum of elements.

Uploaded by

Ketan Bhalerao
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)
220 views2 pages

Set PYTHON PDF

Sets in Python are unordered collections of unique elements that can be used to perform mathematical set operations. Sets are written with curly brackets and do not allow indexing or modification of existing elements, but new elements can be added. Built-in methods allow adding, removing, and checking for elements as well as performing set operations like unions, intersections, differences and more. Common functions provide functionality like checking properties of sets, getting the length, maximum, minimum or sum of elements.

Uploaded by

Ketan Bhalerao
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/ 2

Set data type

A set is a collection which is unordered and unindexed. In Python sets are written
with curly brackets.

You cannot access items in a set by referring to an index, since sets are unordered
the items has no index.

Once a set is created, you cannot change its items, but you can add new items.

Method Description

add() Adds an element to the set

clear() Removes all elements from the set

copy() Returns a copy of the set

difference() Returns the difference of two or more sets as a new set

difference_update() Removes all elements of another set from this set

Removes an element from the set if it is a member. (Do nothing


discard() if the element is not in set)

intersection() Returns the intersection of two sets as a new set

intersection_update() Updates the set with the intersection of itself and another

isdisjoint() Returns True if two sets have a null intersection

issubset() Returns True if another set contains this set

issuperset() Returns True if this set contains another set

Removes and returns an arbitrary set element. Raise Key


pop() Error if the set is empty
Removes an element from the set. If the element is not a
remove() member, raise a KeyError

symmetric_difference() Returns the symmetric difference of two sets as a new set

symmetric_difference_update() Updates a set with the symmetric difference of itself and another

union() Returns the union of sets in a new set

update() Updates the set with the union of itself and others

Built-in Functions with Set

Function Description

all() Return True if all elements of the set are true (or if the set is empty).

any() Return True if any element of the set is true. If the set is empty, return False.

Return an enumerate object. It contains the index and value of all the items
enumerate() of set as a pair.

len() Return the length (the number of items) in the set.

max() Return the largest item in the set.

min() Return the smallest item in the set.

Return a new sorted list from elements in the set (does not sort the set
sorted() itself).

sum() Return the sum of all elements in the set.

You might also like