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

Python (Set Codes)

The document explains the characteristics and methods of sets in Python, highlighting that sets are immutable and unordered collections that do not allow duplicate values. It covers various set operations such as adding elements, clearing the set, removing specific elements, popping random elements, and performing union and intersection operations with other sets. Additionally, it notes that while sets are mutable, their elements must be immutable (hashable) types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Python (Set Codes)

The document explains the characteristics and methods of sets in Python, highlighting that sets are immutable and unordered collections that do not allow duplicate values. It covers various set operations such as adding elements, clearing the set, removing specific elements, popping random elements, and performing union and intersection operations with other sets. Additionally, it notes that while sets are mutable, their elements must be immutable (hashable) types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#SET's (These are immutable and unordered) (Duplicate values are not allowed in

Set's)
collection={1, 2, 2, 2, "hello", "world", "world", 4.1}
print(collection) # Here it will ignore Duplicate items
print(type(collection))
print(len(collection)) # Length will also ignore the duplicate items
Sets=set() #This is an empty set
print(type(Sets))
# Note: Sets are mutable but the elements inside it are immutable.
# Immutable value will be provided with some hash value and thus also known as
hashable values whereas, mutable
# mutable values are called as unhashable values.

# SET METHODS
# [Link](el):
set1=set() # created an empty set
[Link](1)
[Link](2)
[Link](2) # Duplicate item will be ignored
[Link]((3, 2, 1)) # We can also add tuple as a set element, because tuple is an
immutable datatype.
[Link]("Hello World")
print(set1)
print(len(set1)) # Here whole Tuple length will be taken as a single element &
hence a single value.
#[Link]([1, 2, 3]) # ERROR: unhashable type 'list'
#print(set1)

# [Link]() --(clear all elements and empties the set)


set2={1, 2, "Samruddhi", (5, 6, 7)}
print(set2)
print(len(set2))
[Link]()
print(set2) # Here it will print / return an empty set
print(len(set2))

# [Link](el) --(removes the particular element)


set3={1, 2, "Hello", (1, 2, 3)}
print(set3)
[Link]((1,2,3))
print(set3)

# [Link]() --(removes the random element)


set4={1, 3, 4.99, "Hello", ('W','O','R','L','D')}
print(set4)
[Link]() # It will randomly remove any element
print(set4)
print([Link]()) # Prints the element which is popped
print(set4)

# [Link](set) --(Combines both set's unique values and return the new set)
setA={1, 2, 3, 4, 4}
setB={4, 5, 3, 6}
print([Link](setB)) # It does not make changes in any of the indivisual set but
only returns new set.
print([Link](setA))
# [Link](set) --(Combines the common values from both sets and returns
them as a new set)
setC={1, 2, 3, 4, 3}
setD={5, 2, 4, 3, 3}
print([Link](setD))
print([Link](setC))

You might also like