Sets in Python
SETS
The sets are nothing but the collections of well defined objects, In set of the python it defined by the "set" function into the variable.
s = set()
print(type(s))
<class 'set'>
Note: In python to make a list is always used by "[....]" square brackets
a = [1, 2, 3, 4]
This_is_a_set = set(a)
print(This_is_a_set)
{1, 2, 3, 4}
There is a function ".add" which adds the elements into the sets during programing.
code👇
s1 = set()
s1.add(23)
s1.add(23)
print(s1)
{23}
".add" function can add the elements but it never acept the same number/ string element.
s1 = set()
s1.add(23)
s1.add(20)
s2 = s1.union({1, 2, 3})
print(s1, s2)
{1, 2} {1, 2}
".union" function makes the new set
".intersection" function intersects the two variables
For more sets functions click here👉https://www.w3schools.com/python/python_sets.asp
Comments
Post a Comment