-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.py
More file actions
43 lines (23 loc) · 1.39 KB
/
Set.py
File metadata and controls
43 lines (23 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# In Python, a set is a built-in data structure that represents an unordered collection of unique elements. Sets are widely used in data structures and algorithms (DSA) for various purposes due to their efficiency in handling unique values. Here's an overview of sets in Python and how they can be used in DSA:
# Creating Sets:
# You can create a set in Python using curly braces {} or the set() constructor. Sets are unordered, so the order of elements is not guaranteed.
# my_set = {'Ayush', "hrutika", 3,56}
# print(my_set)
# Operations on Sets:
# Sets support various operations, including union, intersection, difference, and more. These operations can be useful in DSA when dealing with unique data.
# set1 = {1, 2, 3,4,5}
# set2 = {3, 4, 5,6,7}
# # Union=set1.union(set2)
# # print(Union)
# Intersection=set1.intersection(set2) # its return common value in both set
# print(Intersection)
# Adding and Removing Elements:
# You can add elements to a set using the add() method and remove elements using the remove() or discard() method. These operations can be handy when implementing algorithms that require adding or removing elements dynamically.
# my_set = {1, 2, 3}
# my_set.add(4) # Add an element
# my_set.remove(2) # Remove an element
# print(my_set)
# Set using loop
# my_set = {'Ayush', "hrutika", 3,56}
# for i in(my_set):
# print(i)