set in python

Veröffentlicht am: 09 April 2020
auf dem Kanal: Tarun Sir
116
8

In this video you will learn about sets in python.
A set is also a primitive data structure which allows us to store the values. The set is also a part of python core collection. A set does not stores the elements in any specific order, so a set is unordered and unindexed. A set can-not store duplicate data. A set is mutable, it means we can add or remove elements from a set. A set internally uses a hash table. To create a set we can use following :
• Creating set using curly brackets: We can create a set using the curly brackets.
Example:
fruits={"apple","banana","grapes","strawberry","blueberry","mango"}
print(fruits)#Sets are unordered so we cannot be sure in which order elements will be printed.

ages={34,56,67,89}
print(ages)
• Creating set using set() constructor: We can also create a set using set() constructor.
Example:
fruits=set(("apple","banan","grapes","strawberry","blueberry","mango"))
print(fruits)
Note: We have to use double (()) while creating set using the set() constructor.

Access the set elements
As we know that sets does not have any index or key, so we can only use a set using the for loop only.
Example:
fruits=set(("apple","banan","grapes","strawberry","blueberry","mango"))
print(fruits)
for fruit in fruits:
print(fruit)
Adding elements to set
• add() : The add function takes an argument and add it to the set.
Example:
numbers={10,20}
print(numbers)
numbers.add(30)
print(numbers)
• update() : The update function can take a list of arguments and add all the elements of list to the set.
numbers={10,20}
print(numbers)
numbers.update ([30,40,50])
print(numbers)

Deleting element from the set
• remove(): The remove() method takes an element as argument and remove it from set if it is in the set. If the element is not in the set then remove generates key error.
Example
numbers={10,20}
print(numbers)
numbers.remove(10)
print(numbers)

• discard() : The discard() method removes the element from the set. It does not generates error if the element is not in the set.
Example
numbers={10,20}
print(numbers)
numbers.discard (10)
print(numbers)

• pop() : The pop() method deletes the last element from the set. As the order is not defined in the set, so we can not predict which item will be popped.
Example
numbers={10,20}
print(numbers)
numbers.pop()
print(numbers)

• clear() : The clear method deleted all the elements of the set.
Example
numbers={10,20}
print(numbers)
numbers.clear()
print(numbers)

• del : We can delete the complete set using del keyword.
Example
numbers={10,20}
print(numbers)
del numbers
print(numbers)#Error, numbers does not exist

Searching a value in set
We can search value in set() with the help of in operator.
Example
numbers={10,20}
print(numbers)
if 10 in numbers:
print(“The value is found in the set”)

#python_by_tarun_sir #tarun_sir #python #tarun_sir_python #python_video #set_in_python #find_sum #python_tutorial


Auf dieser Seite können Sie das Online-Video set in python mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Tarun Sir 09 April 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 116 Mal angesehen und es wurde von 8 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!