What is Set in Python | EP-12 Sets in Python | How to Create Set in Python | Free Python Tutorials
"""Python Set is an unordered collection of data types that is iterable,mutable
(a data type is mutable if its values can be changed, updated, or modified after
the data type has been created), and has no duplicate elements.Sets are written
with curly brackets.There two ways to create sets
1. using {}
2. using set constructor"""
#CreateaSet:
#1.using {}
set1 = {1,1,6.8,"a"}
print(set1)
#2.using set constructor"""
Creating an empty Set
empty_set = set()
print(empty_set)
print(type(empty_set))
Creating a Set with the use of a String
company ="cybrosys"
print(set(company))
Creating a Set with the use of a List
set2 = set([1,2,3,"a"])
print(set2)
length of set
print(len(set2))
Adding Elements to a Set in Python
"""
1. Using add() Method
2. Using update() Method
"""
#1.Using add() Method
"""Elements can be added to the Sets in Python by using the built-in add()
function. Only one element at a time can be added to the set by using add()
method, loops are used to add multiple elements at a time with the use of add()
method.
Note: Lists cannot be added to a set as elements because Lists are not hashable
whereas Tuples can be added because tuples are immutable and hence Hashable.
syntax:object.add()
"""
Creating a Set
#add elements and other data types
set3 = set()
set3.add(1)
print(set3)
set4 = set()
set4.add((1,2,3))
print(set4)
Using update() Method
"""For the addition of two or more elements, Update() method is used.
The update() method accepts lists, strings, tuples as well as other Python hash
set as its arguments. In all of these cases, duplicate elements are avoided.
syntax:object.update()"""
set5 = set()
set5.update([1,2,3])
print(set5)
Accessing a Set in Python
"""Set items cannot be accessed by referring to an index, since sets are
unordered the items has no index. But you can loop through the Python hash set
items using a for loop, or ask if a specified value is present in a set,
by using the in keyword.
"""
Creating a set
set6 = set((1,2,3))
print(set6)
Checking the element
using in keyword
set7 = {"cybrosys", "technologies"}
print("c" in set7)
Removing Elements from the Set in Python
"""
1. Using remove() Method or discard() Method
2. Using pop() Method
3. Using clear() Method"""
1. Using remove() Method or discard() Method
"""
Elements can be removed from the Sets in Python by using the built-in remove()
function but a KeyError arises if the element doesn’t exist in the hashset
Python. To remove elements from a set without KeyError, use discard(),
if the element doesn’t exist in the set, it remains unchanged."""
Removing elements from Set using Remove() method
"""syntax:object.remove()"""
set8 = {"cybrosys", "technologies"}
set8.remove(("cybrosys"))
print(set8)
Removing elements from Set using Discard() method
"""syntax:object.discard()"""
set8.discard("c")
print(set8)
2. Using pop() Method
"""Pop() function can also be used to remove and return an element from the
set, but it removes only the first element of the set.
Note: If the set is unordered then there’s no such way to determine which
element is popped by using the pop() function.
syntax:object.pop()"""
set9 = {1,2,3,6,9}
set9.pop()
print(set9)
3. Using clear() Method
"""
To remove all the elements from the set, clear() function is used.
syntax:object.clear()"""
set9.clear()
print(set9)
#copy() method
set10 = {1,2,3}
set11 = set10.copy()
print(set10, "set10")
print(set11, "set11")
Frozen Sets in Python
"""
Frozenset is similar to set in Python, except that frozensets are immutable,
which implies that once generated, elements from the frozenset cannot be added
or removed.
"""
Fset =frozenset()
Fset.add(1)
print(Fset)
num = 1,2,3
fset = frozenset(num)
print(fset)
#Python #PythonSets #LearnPython #PythonTutorials #Coding #Programming #FreeTutorials #PythonBeginners #DataScience #TechEducation #PythonDevelopment #LearnToCode #SetOperations #DataStructures #PythonForBeginners #PythonBasics #CodingForEveryone #MachineLearning #WebDevelopment
Connect With Us:
—————————————
➡️ Website: https://www.cybrosys.com/
➡️ Email: info@cybrosys.com
➡️ Twitter: / cybrosys
➡️ LinkedIn: / cybrosys
➡️ Facebook: / cybrosystechnologies
➡️ Instagram: / cybrosystech
➡️ Pinterest: / cybrosys