Sets in Python Tutorial
Sets in Python Tutorial, In this tutorial, we will learn what are python sets different methods for sets in Python to your code. Are you looking for the Python sets tutorial with examples or Are you dreaming to become to certified Pro
Python Developer, then stop just dreaming, get your
Python certification course from India’s Leading
Python training institute.
A Python set is a built-in data type that stores a group of elements without ordering elements. In a given set duplicate elements are not allowed. we need to include all the elements in the set in the form of an iterable object such as a string when we are defining a set. Do you want to know about sets in python with examples, then just follow the below mentioned Python sets tutorial for Beginners from
Prwatech and take advanced
Python training like a Pro from today itself under 10+ years of hands-on experienced Professionals.
What are Python Sets?
A set is a collection of unordered and unindexed elements enclosed with curly brackets.
Ex) x={1,2,4,45}
You cannot access an element in a set by referring to its index since sets are unordered the elements don’t have an index. You can traverse through set using a for a loop.
Ex) x={1,2,3,4,5,6}
for z in x:
print(z)
You can check whether an element is present in a set using keyword “in”
Ex) x={1,2,3,4,5,6}
if(1 in x):
print(z)
Once a set is created, you cannot change its elements, but you can add new items.
Methods for Sets in Python
add(): Used to add a single element to a set.
Ex) x={1,2,3,4,5,6}
x.add(9)
print(x)
update(): Used to add multiple elements to a set.
Ex) x={1,2,3,4,5,6}
x.update(8,11,44,22)
print(x)
len(): Used to determine the total number of elements present in set.
Ex) x={1,2,3,4,5,6}
print(len(x))
remove() or discard(): Used to remove a specific element from a set.
Ex) x={1,2,3,4,5,6}
x.remove(2)
print(x)
pop(): It removes an element from the end of a set.
Ex) x={1,2,3,4,5,6}
print(x.pop())
clear(): It is used to clear all the elements from the entire set.
Ex) x={1,2,3,4,5,6}
x.clear()
del(): It will delete the set completely.
Ex) x={1,2,3,4,5,6}
x.delete()
copy(): It returns a copy of a set.
Ex) x={1,2,3,4,5,6}
z= x.copy()
print(z)
union(): It combines two sets without repeating the common elements
Ex) x={1,2,3,4,5,6}
y={4,5,6,8,9,123}
z= x.union(y)
print(z)
intersection(): It returns only the common elements between the two sets.
Ex) x={1,2,3,4,5,6}
y={4,5,6,8,9,123}
z= x.intersection(y)
print(z)
issubset(): Used to identify whether the given set is part of another set or not.
Ex) x={1,2,3,4,5,6}
y={4,5,6 }
z= x.issubset(y)
print(z)
issuperset(): Used to identify whether the given set is a superset of another set or not.
Ex) x={1,2,3,4,5,6}
y={4,5,6 }
z= y.issubset(x)
print(z)
We hope you understand sets in Python tutorial with examples and methods for sets in the
Python programming concept. Get success in your career as a
Python programmer by being a part of the
Prwatech, India's leading
Python training institute in Bangalore.