- Get link
- Other Apps
- Get link
- Other Apps
Sets
- Sets are the data types.- Sets are used to store multiple data in a single variable.
- Sets are created using curly brackets {}.
- Sets are unchangeable, but we can remove and add new items.
- Sets are NOT allowed duplicate values.
- Sets are unindexed (which means we cannot access the elements of a set by using the index).
- Sets are unordered (which means when we display the elements of a set, it will return in random order).
- Sets are not supported the first value has [0] index, and the second value has [1] index.
- If that specific value does not exist then remove() will raise an error.
- If that specific value does not exist then discard() will NOT raise any error.
Sets with different Data Types Values:
Set with String, Integer, Boolean Values:
Sets are Not Allow Duplicates Values:
Set type():
- The type() method Returns the datatype of cheems_set. (In the below code)
- Syntax: type(variable_name)
Set len():
- len() stands for length.
- The len() method Returns the length of cheems_set. (In the below code)
- The len() method Returns the length of cheems_set. (In the below code)
- Syntax: len(variable_name)
Change string in Set:
- Syntax: set(a)
- a should be another datatype variable to change into a set.
- a is the string to change into a set. (In the below code)
- We can take any datatype to change into a set. Like- list[], tuple(), dict{} etc.
Set add():
- The add() method is used to add one new value in a set.- Syntax: variable_name.add(New Value)
Set update():
- The update() method is used to add data from another set into the current set.Update set to set:
- Syntax: variable_name1.update(variable_name2)
- cheems_set and cheems_set2 are sets. (In the below code)
Update list to set:
- Syntax: variable_name1.update(variable_name2)
- cheems_set is a set and cheems_set2 is a list. (In the below code)
Set remove():
- The remove() method is used to remove the specific value in Set.- If that specific value does not exist then remove() will raise an error.
- Syntax: variable_name.remove(Value in set)
Set discard():
- The discard() method is used to remove the specific value in Set.- If that specific value does not exist then discard() will NOT raise any error.
- Syntax: variable_name.discard(Value in Set)
- del used to delete a set and return nothing.
- del NOT support the delete specific value in a set by their position.
Set pop():
- The pop() method is used to remove the ending value in a set.
- Syntax: variable_name.pop()
Set clear():
- The clear() method is used to clear the all values in a set.
- Output: set().
- Output: set().
- Syntax: variable_name.clear()
Set del:
- del stands for delete.- del used to delete a set and return nothing.
- del NOT support the delete specific value in a set by their position.
Delete a Set:
- Syntax: del variable_name
- Get link
- Other Apps
Comments
Post a Comment