Set data Type in Python
What Set Data Type in Python?
Set in Python is an unordered collection of unique elements. Set can be created by using {} brackets or built-in function set(). All the set elements are separated with comma.
Features of Set() datatype:
1. It is unordered collection of unique data items
2. It can be created by using {} brackets or builtin function set()
3. Unordered: Sets are unordered collections, meaning the elements have no specific order. Unlike lists or tuples, you cannot access elements in a set by indexing.
4. Mutable: Sets are mutable, allowing you to add and remove elements after creation.
5. Unique Elements: Sets can only contain unique elements. Duplicate elements are automatically removed when the set is created or modified.
6. Dynamic Size: Sets can grow or shrink in size as needed. You can add new elements or remove existing ones dynamically.
7. Mathematical Operations: Sets support various mathematical operations, such as union, intersection, difference, and symmetric difference.
How to create set:
By using curly brackets
#creating set using {} brackets
my_set={1,2,3,4,5,6,7}
print(my_set)
Output:
{1, 2, 3, 4, 5, 6, 7}
By using Built-in function set()
# creating set using set()
my_set=set([1,2,3,4,5,6,7,8,9])
print(my_set)
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Another Example,
# creating set using set()
my_set=set([1,2,2,3,3,4,5,6,7,8,9])
print(my_set)
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9}
Remember duplicated items automatically removed when set is created.
Common Methods of Set:
1. The add():
The add() method adds an element to set
Example,
my_set=set([1,2,2,3,3,4,5,6,7,8,9])
print("Original Set:",my_set)
my_set.add(1000) # add 1000 to set
print("After adding element to set my set is:",my_set)
Output:
Original Set: {1, 2, 3, 4, 5, 6, 7, 8, 9}
After adding element to set my set is: {1, 2, 3, 4, 5, 6, 7, 8, 9, 1000}
2. The remove() Method:
The remove() method helps to remove an element from set.
Example,
my_set=set([1,2,2,3,3,4,5,6,7,8,9])
print("Original Set:",my_set)
my_set.remove(9) # removing 9
print("After adding element to set my set is:",my_set)
Output:
Original Set: {1, 2, 3, 4, 5, 6, 7, 8, 9}
After adding element to set my set is: {1, 2, 3, 4, 5, 6, 7, 8}
3. The union() Method:
The union() method returns the new set with common elements of two sets
Example,
set1={1,2,3,4,5,6,7,8}
set2={1,2,3,4,5}
set3=set1.union(set2)
print("The union of set1 and set2 is:",set3)
Output:
The union of set1 and set2 is: {1, 2, 3, 4, 5, 6, 7, 8}
The union function combines all the unique elements and create new set. Here set1 and set2 are two different sets. Set3 is created with the help of union(). Remember in case of duplicate items of both the sets union will take that element only once and all other unique elements.
4. The intersection ():
The intersection() method returns new set with all common elements of both the sets
Example,
set1={1,2,3,4,5,6,7,8}
set2={1,2,3,4,5}
set3=set1.intersection(set2)
print("The intersection of set1 and set2 is:",set3)
Output:
The intersection of set1 and set2 is: {1, 2, 3, 4, 5}
5. The difference() Method:
The difference() method returns new set with the elements that are in only one set but not in another set
Example,
set1={1,2,3,4,5,6,7,8}
set2={1,2,3,4,5}
set3=set1.difference(set2)
print("The difference of set1 and set2 is:",set3)
Output:
The difference of set1 and set2 is: {8, 6, 7}
6. The issubset() Method:
This method returns true if one set is subset of another set.
Example,
my_set1={1,2,3,4,5,6}
my_set2={1,2,3,4}
print(my_set2.issubset(my_set1))
print(my_set1.issubset(my_set2))
Output:
True
False
How to input set elements at run time:?
We can input set elements at run time by using input() function.
Example,
my_set=set(map(int,input("Enter set element:").split()))
print("The set elements are:",my_set)
print("The even set elements are:",end=' ')
for i in my_set:
if isinstance(i,int):
if i%2==0:
print(i,end=' ')
Output:
Enter set element:1 2 3 4 5 6 7 8 9 10
The set elements are: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The even set elements are: 2 4 6 8 10
In this example input function is used to take set data items at run time. map() method is used to map each one of data items with integer datatype. End=’ ‘this statement is used to print the data items in front of the display statement. The end= “ “ parameter is used to print space instead of new line character after each print() statement.
0 टिप्पण्या
कृपया तुमच्या प्रियजनांना लेख शेअर करा आणि तुमचा अभिप्राय जरूर नोंदवा. 🙏 🙏