What is Set in Python?
A set is a data structure used to store multiple unique values in a single variable. No Duplicate Values.
Example
numbers = {10, 20, 30, 40}
names = {"Kavi","Arjun","Ashok"}
Sets do not store values by index. The items are not ordered, so you cannot access values using position.
names = {"kavi","arjun","akash"}
print([0])
It does not work because Sets dont have Index numbers.
Sets do not allow duplicate values. even if you input duplicate values, output will always have unique values.
numbers = {10, 20, 20, 30, 30, 40}
print(numbers)
Output will contain only unique values.
Sets are changeable (mutable). You can add or remove values.
numbers = {10, 20, 30}
numbers.add(40)
numbers.remove(10)
print(numbers)
Comments
Post a Comment