0% found this document useful (0 votes)
5 views1 page

Python Set Functions

The document provides a comprehensive overview of Python's built-in set functions, detailing their descriptions, syntax, and examples. Key functions include add(), update(), remove(), and various operations for set manipulation such as intersection, union, and difference. Each function is illustrated with a practical example to demonstrate its usage.

Uploaded by

venkatasai012345
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)
5 views1 page

Python Set Functions

The document provides a comprehensive overview of Python's built-in set functions, detailing their descriptions, syntax, and examples. Key functions include add(), update(), remove(), and various operations for set manipulation such as intersection, union, and difference. Each function is illustrated with a practical example to demonstrate its usage.

Uploaded by

venkatasai012345
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/ 1

Python Built-in Set Functions

Function Description Syntax Example

add() Adds an element to the set set.add(item) {1, 2}.add(3) -> {1, 2, 3}

update() Adds multiple elements to the set set.update(iterable) {1, 2}.update([3, 4]) -> {1, 2, 3, 4}

copy() Returns a shallow copy of the set set.copy() s = {1, 2}; s.copy() -> {1, 2}

remove() Removes an element (error if not found) set.remove(item) {1, 2, 3}.remove(2) -> {1, 3}

discard() Removes an element (no error if missing) set.discard(item) {1, 2, 3}.discard(2) -> {1, 3}

pop() Removes and returns an arbitrary element set.pop() {1, 2, 3}.pop() -> 1, {2, 3}

clear() Removes all elements from the set set.clear() {1, 2, 3}.clear() -> {}

difference() Returns difference of sets set.difference(set2) {1, 2, 3}.difference({2}) -> {1, 3}

difference_update() Updates set with difference set.difference_update(set2) {1, 2, 3}.difference_update({2}) -> {1, 3

symmetric_difference() Returns elements in either set but not bothset.symmetric_difference(set2) {1, 2, 3}.symmetric_difference({2, 4}) ->

symmetric_difference_update()
Updates set with symmetric difference set.symmetric_difference_update(set2)
{1, 2, 3}.symmetric_difference_update(

intersection() Returns intersection of sets set.intersection(set2) {1, 2, 3}.intersection({2, 3}) -> {2, 3}

intersection_update() Updates set with intersection set.intersection_update(set2) {1, 2, 3}.intersection_update({2, 3}) -> {

union() Returns union of sets set.union(set2) {1, 2}.union({2, 3}) -> {1, 2, 3}

isdisjoint() Checks if sets have no elements in common


set.isdisjoint(set2) {1, 2}.isdisjoint({3, 4}) -> True

issubset() Checks if set is a subset set.issubset(set2) {1, 2}.issubset({1, 2, 3}) -> True

issuperset() Checks if set is a superset set.issuperset(set2) {1, 2, 3}.issuperset({1, 2}) -> True

You might also like