Tutorial Playlist
200 Lessons1. Introduction to Python
2. Features of Python
3. How to install python in windows
4. How to Install Python on macOS
5. Install Python on Linux
6. Hello World Program in Python
7. Python Variables
8. Global Variable in Python
9. Python Keywords and Identifiers
10. Assert Keyword in Python
11. Comments in Python
12. Escape Sequence in Python
13. Print In Python
14. Python-if-else-statement
15. Python for Loop
16. Nested for loop in Python
17. While Loop in Python
18. Python’s do-while Loop
19. Break in Python
20. Break Pass and Continue Statement in Python
21. Python Try Except
22. Data Types in Python
23. Float in Python
24. String Methods Python
25. List in Python
26. List Methods in Python
27. Tuples in Python
28. Dictionary in Python
29. Set in Python
Now Reading
30. Operators in Python
31. Boolean Operators in Python
32. Arithmetic Operators in Python
33. Assignment Operator in Python
34. Bitwise operators in Python
35. Identity Operator in Python
36. Operator Precedence in Python
37. Functions in Python
38. Lambda and Anonymous Function in Python
39. Range Function in Python
40. len() Function in Python
41. How to Use Lambda Functions in Python?
42. Random Function in Python
43. Python __init__() Function
44. String Split function in Python
45. Round function in Python
46. Find Function in Python
47. How to Call a Function in Python?
48. Python Functions Scope
49. Method Overloading in Python
50. Method Overriding in Python
51. Static Method in Python
52. Python List Index Method
53. Python Modules
54. Math Module in Python
55. Module and Package in Python
56. OS module in Python
57. Python Packages
58. OOPs Concepts in Python
59. Class in Python
60. Abstract Class in Python
61. Object in Python
62. Constructor in Python
63. Inheritance in Python
64. Multiple Inheritance in Python
65. Encapsulation in Python
66. Data Abstraction in Python
67. Opening and closing files in Python
68. How to open JSON file in Python
69. Read CSV Files in Python
70. How to Read a File in Python
71. How to Open a File in Python?
72. Python Write to File
73. JSON Python
74. Python JSON – How to Convert a String to JSON
75. Python JSON Encoding and Decoding
76. Exception Handling in Python
77. Recursion in Python
78. Python Decorators
79. Python Threading
80. Multithreading in Python
81. Multiprocеssing in Python
82. Python Regular Expressions
83. Enumerate() in Python
84. Map in Python
85. Filter in Python
86. Eval in Python
87. Difference Between List, Tuple, Set, and Dictionary in Python
88. List to String in Python
89. Linked List in Python
90. Length of list in Python
91. Reverse a List in Python
92. Python List remove() Method
93. How to Add Elements in a List in Python
94. How to Reverse a List in Python?
95. Difference Between List and Tuple in Python
96. List Slicing in Python
97. Sort in Python
98. Merge Sort in Python
99. Selection Sort in Python
100. Sort Array in Python
101. Sort Dictionary by Value in Python
102. Datetime Python
103. Random Number in Python
104. 2D Array in Python
105. Abs in Python
106. Advantages of Python
107. Anagram Program in Python
108. Append in Python
109. Applications of Python
110. Armstrong Number in Python
111. Assert in Python
112. Binary Search in Python
113. Binary to Decimal in Python
114. Bool in Python
115. Calculator Program in Python
116. chr in Python
117. Control Flow Statements in Python
118. Convert String to Datetime Python
119. Count in python
120. Counter in Python
121. Data Visualization in Python
122. Datetime in Python
123. Extend in Python
124. F-string in Python
125. Fibonacci Series in Python
126. Format in Python
127. GCD of Two Numbers in Python
128. How to Become a Python Developer
129. How to Run Python Program
130. In Which Year Was the Python Language Developed?
131. Indentation in Python
132. Index in Python
133. Interface in Python
134. Is Python Case Sensitive?
135. Isalpha in Python
136. Isinstance() in Python
137. Iterator in Python
138. Join in Python
139. Leap Year Program in Python
140. Lexicographical Order in Python
141. Literals in Python
142. Matplotlib
143. Matrix Multiplication in Python
144. Memory Management in Python
145. Modulus in Python
146. Mutable and Immutable in Python
147. Namespace and Scope in Python
148. OpenCV Python
149. Operator Overloading in Python
150. ord in Python
151. Palindrome in Python
152. Pass in Python
153. Pattern Program in Python
154. Perfect Number in Python
155. Permutation and Combination in Python
156. Prime Number Program in Python
157. Python Arrays
158. Python Automation Projects Ideas
159. Python Frameworks
160. Python Graphical User Interface GUI
161. Python IDE
162. Python input and output
163. Python Installation on Windows
164. Python Object-Oriented Programming
165. Python PIP
166. Python Seaborn
167. Python Slicing
168. type() function in Python
169. Queue in Python
170. Replace in Python
171. Reverse a Number in Python
172. Reverse a string in Python
173. Reverse String in Python
174. Stack in Python
175. scikit-learn
176. Selenium with Python
177. Self in Python
178. Sleep in Python
179. Speech Recognition in Python
180. Split in Python
181. Square Root in Python
182. String Comparison in Python
183. String Formatting in Python
184. String Slicing in Python
185. Strip in Python
186. Subprocess in Python
187. Substring in Python
188. Sum of Digits of a Number in Python
189. Sum of n Natural Numbers in Python
190. Sum of Prime Numbers in Python
191. Switch Case in Python
192. Python Program to Transpose a Matrix
193. Type Casting in Python
194. What are Lists in Python?
195. Ways to Define a Block of Code
196. What is Pygame
197. Why Python is Interpreted Language?
198. XOR in Python
199. Yield in Python
200. Zip in Python
Python is known for its flexibility and popularity, offering various tools to simplify coding. Among these tools, sets are useful for managing groups of items. We will detail sets in Python, looking at their characteristics, construction, manipulation, and numerous operations. Starting with the basics and progressing to advanced concepts, we'll cover creating sets, manipulating them, and comparing them. By the end of this tutorial, you'll gain a firm understanding of how sets in Python function.
A set in Python is an unordered collection of unique elements that doesn't allow duplicates. Sets come in handy when dealing with distinct items. They're mutable, so you can add or remove elements after creation. The related data structure frozenset in Python resembles a set but is immutable.
In Python, a set is a collection of unique elements, perfect for handling items without duplicates. Sets are represented with curly braces {} and commas separate elements.
Creating a Set or set() function in Python:
In Python, you can create sets using {}. Here's an example:
fruits = {"apple", "banana", "orange", "apple", "pear"}
print(fruits)
Here, "apple" is duplicated, but the set will only contain one instance. When you print 'fruits', duplicates are automatically removed.
Output:
Basic Operations:
Sets support various operations like adding, removing, and checking membership.
# Creating a set
my_set = {1, 2, 3, 4, 5,}
# Adding an element
my_set.add(6)
# Removing an element
my_set.remove(3)
# Checking membership
print(2 in my_set) # Output: True
print(3 in my_set) # Output: False
Set Operations:
Sets also allow for mathematical operations like union, intersection, and difference.
set_A = {1, 2, 3, 4}
set B = [3, 4, 5, 6]
# Union of sets A and B
union = set_A | set_B
# Intersection of sets A and B
intersection = set_A & set_B
# Difference between sets A and B
difference = set_A set_B
print(union) # Output: {1, 2, 3, 4, 5, 6}
print(intersection) # Output: {3, 4}
print(difference) # Output: {1, 2}
Use Case Example:
Consider a practical scenario: you have a shopping cart with a list of items, and you want to eliminate duplicates before proceeding to checkout.
shopping_cart = ["apple", "banana", "apple", "orange", "banana", "pear"]
unique_items = set(shopping_cart)
print(unique_items)
Output:
Set in Python is Mutable or Immutable?
Sets in Python are mutable, allowing the addition, removal, and modification of elements. Unlike immutable data types, sets can be changed after creation, making them suitable for dynamic collections. However, their mutability should be considered when shared across code sections or in concurrent programming.
Set Items
In Python, sets store unique items, prohibiting duplicates. They can hold various data types, enclosed in {} with items separated by commas.
Creating Set Items:
You can create a set using curly braces like this:
my_set = [1, 2, 3, 4 3 5)
print(my set)
In this example, 'my_set' holds numbers 1 to 5.
Checking Membership:
To check if an item is in a set, use the 'in' keyword.
numbers = {1, 2, 3, 4, 5}
print(3 in numbers) # Output: True
print(6 in numbers) # Output: False
Iterating Through Set Items:
Use loops to iterate through the items in a set:
fruits = {apple", "banana", "orange"}
for fruit in fruits:
print(fruit)
Example with Mixed Data Types:
Sets can contain items of different types:
mixed_set = {"apple", 123, 3.14, (1, 2, 3)}
print(mixed set)
In this case, the set ‘mixed_set’ contains a string, an integer, a float, and a tuple.
Output:
Converting List to Set:
You can convert a list into a set to remove duplicate items:
my_list = [1, 2, 2, 3, 3, 4, 5]
unique_set = set (my_list)
print(unique_set)
Create 'my_list' with duplicates and convert it to 'unique_set'. Python sets automatically remove duplicates, resulting in a set with only unique elements. Here's the output:
Dictionary in Python
A dictionary in Python is an unordered collection of key-value pairs. It allows you to store and retrieve data using unique keys. Keys are used to identify and access their corresponding values. Here's an example:
# Creating a dictionary
my_dict = {
'name': 'Alice',
'age': 30,
'city': 'New York'
}
# Accessing values using keys
print("Name:", my_dict['name'])
print("Age:", my_dict['age'])
print("City:", my_dict['city'])
Output:
Python set contains methods like add (), clear (), remove () and discard (). Sets can be modified using these.
Adding Elements:
Add elements to a set using the add() method. If the element is already in the set, it won't be duplicated because sets reject duplicates.
fruits (apple", "banana", "orange")
fruits.add("grape")
print (fruits) # Output: {'orange', 'banana', 'apple', 'grape'}
#Adding a duplicate element
fruits.add("banana")
print (fruits) # Output: ('orange', 'banana', 'apple', 'grape'}
Removing Elements:
Remove elements from a set using the remove() method: It removes an element if present but raises a KeyError if not found in the set.
my_set = {1, 2, 3}
my set.remove(2) # Removes 2 from the set
try:
my_set.remove(4) # Element 4 is not present, raises KeyError
except KeyError as e:
print("KeyError:", e)
print(my set) # Output: (1, 3)
Discarding Elements:
To remove an element from a set without causing an error if it's absent, employ the 'discard()' method.
my_set = {1, 2, 3}
my set.discard (2) # Removes 2 from the set
my_set.discard (4) # Element 4 is not present, no error
print(my set) # Output: (1, 3)
Clearing a Set:
To empty a set and remove all its elements, use the 'clear()' method.
fruits = {"apple", "banana', 'orange"}
fruits.clear()
print (fruits) # Output: set()
In Python, you can find a set's length (number of elements) with the 'len()' function.
Example.
fruits = {"apple", "banana", "orange", "pear"}
length = len(fruits)
print("The length of the 'fruits' set is:", length)
Here, the len() function is applied to the fruits set, and the result is stored in the variable length. It displays the length of the set.
Output:
The value 4 indicates that the 'fruits' set contains four unique elements.
Empty Set Length:
If you apply the len() function to an empty set, the result will be 0.
empty_set = set()
length empty = len(empty_set)
print("The length of the 'empty_set' is: ", length_empty)
Output:
Using Length in Conditional Statements:
You can employ a set's length in conditional statements to verify if it's empty or has elements.
my_set = {1, 2, 3, 4, 5}
if len(my_set) > 0:
print("The set is not empty.")
else:
print("The set is empty.")
Output:
In Python, you can perform type casting to change data types. For sets, use the 'set()' constructor to create a set from iterable types like lists, tuples, and strings.
Type Casting from List to Set:
Convert a list to a set using 'set()'. Duplicates are automatically removed, leaving only unique elements.
my list [1, 2, 2, 3, 3, 4, 5]
my set set (my_list)
print("Original list:", my_list)
print("Set from list: ", my.set)
Output:
Type Casting from Tuple to Set:
Similarly convert a tuple to a set using the set() constructor.
my_tuple = (5, 6, 7, 7, 8)
my set = set (my_tuple)
print("Original tuple:", my_tuple)
print("Set from tuple:", my set)
Output:
Type Casting from String to Set:
Convert a string to a set, where each character becomes an element in the set.
my_string = "hello"
my set = set (my_string)
print("Original string:", my_string)
print("Set from string: ", my_set)
Output:
Type Casting from Dictionary Keys to Set:
When you convert dictionary keys to a set, you get a set containing the unique keys.
my_dict = {'a': 1, 'b': 2, 'c': 3}
key_set = set(my_dict)
print("Original dictionary: ", my_dict)
print("Set from dictionary keys: ", key_set)
Output:
Uniqueness in Sets:
Sets in Python inherently enforce uniqueness. This feature makes sets useful for handling collections of distinct items.
Example:
# Creating a set with duplicate elements
my_set = {1, 2, 2, 3, 3, 4, 5}
print("Original set:", my_set)
Output:
Immutability of Set Elements:
Sets are mutable, allowing element additions and removals. However, elements within a set must be of immutable data types, ensuring reliability for indexing and hashing, which is critical for set operations.
Example:
# Creating a set with different data types
my_set = {1, 3.14, "hello", (2, 4)}
# Attempting to add a mutable list to the set
try:
my_set.add([5, 6])
except TypeError as e:
print("TypeError:", e)
Output:
Here, 'my_set' comprises immutable elements like integers, floats, strings, and tuples. However, trying to add the mutable list [5, 6] to the set results in a 'TypeError' because lists, being mutable, cannot be used as set elements.
Heterogeneous Element with Python Set
In Python sets, you can combine elements of different data types, creating "heterogeneous" sets.
Example:
# Creating a set with heterogeneous elements
my_set = {1, 3.14, "hello", (2, 4)}
print("My set:", my_set)
Output:
In this example, the set ‘my_set’ contains elements of various data types:
Python Frozen Sets
A frozen set in Python is an immutable type of set, where elements can't be altered after creation. This trait is handy for creating unchangeable collections of unique items.
Creating a Frozen Set:
Create a frozen set using 'frozenset()' by passing an iterable (e.g., list, tuple, or another set) as an argument.
my_list = [1, 2, 3, 3, 4, 5]
my_frozen_set = frozenset (my_list)
print("My frozen set:", my_frozen_set)
Output:
Frozen sets, once created, you can't add, remove, or modify their elements, ensuring their contents stay constant.
my_frozen_set.add(6) # This will raise an AttributeError
Internal Working of Set
Python sets use hash tables, offering fast average-case time complexity for actions like addition, removal, and membership checks.
Hashing:
Hashing transforms an element into a fixed-size value (hash code) representing it. Strings, integers, and tuples in Python are hashable and usable as keys in dictionaries and elements in sets.
# Hash codes of integers
hash_code_1 = hash(42)
hash_code_2 = hash (99)
print(hash_code_1, hash_code_2)
The 'hash()' function in Python provides an object's hash value, which can vary across program runs. Typically, for integers, the hash value matches the integer value.
Output:
Here, 'hash_code_1' will equal the hash value of '42', and 'hash_code_2' will equal the hash value of '99', which corresponds to the integer values.
Hash Tables:
A hash table stores elements with their hash codes in an array of "buckets" determined by the hash codes.
# Creating a simple hash table (illustration)
hash_table = [None] * 10 # Initialize with empty slots
# Adding elements to hash table
element_1 = "apple".
element_2 = "banana"
hash_code_1 = hash (element_1)
hash_code_2 = hash (element_2)
bucket_1 = hash_code_1 % len (hash_table)
bucket_2 = hash_code_2% len (hash_table)
hash_table[bucket_1] = element_1
hash_table[bucket_2] = element_2
print (hash_table)
Output:
Explanation:
Collision Resolution (Chaining):
Collisions happen when two elements share the same hash code. Python manages collisions with chaining, where each bucket holds a linked list of elements with the same hash code.
#Chaining (illustration)
bucket with collision = 3
linked_list = ["apple", "banana"]
hash_table[bucket_with_collision] = linked_list
print(hash_table)
Internal Working of Sets:
Python's set functions as a hash table implementation. When you create a set, Python employs the hash codes of elements to put them in suitable hash table buckets, enabling rapid retrieval and efficient membership checks.
# Creating a set and adding elements
fruits = {"apple", "banana", "orange"}
#Hash codes and buckets (illustration)
hash_code_apple = hash("apple").
hash_code_banana = hash("banana")
hash_code_orange = hash("orange")
bucket_apple = hash_code_apple % len (hash_table)
bucket_banana = hash_code_banana % len(hash_table)
bucket_orange =hash_code_orange % len (hash_table)
print("Hash codes: ", hash_code_apple, hash_code_banana, hash_code_orange) print("Buckets: ", bucket_apple, bucket_banana, bucket_orange)
This calculates hash codes for "apple," "banana," and "orange" elements and determines the hash table buckets where they'd be placed.
Output:
Explanation:
Python sets come with various methods to perform operations. Some of the key methods include:
1. Union Operation on Python Sets (union() or | operator):
The union of two sets produces a new set containing all unique elements from both sets.
set1 = [1, 2, 3)
set2= [3, 4, 5}
union_set = set1.union (set2)
# Or: union_set = set1 | set2
print(union_set) # Output: (1, 2, 3, 4, 5}
2. Intersection Operation on Python Sets (intersection() or & operator):
The Python set intersection of two sets returns a new set with elements shared by both sets.
set1= {1, 2, 3}
set2= [3, 4, 5}
intersection_set = set1. intersection (set2)
#Or: intersection_set = set1 & set2
print(intersection_set) # Output: (3)
3. Finding Differences of Sets in Python (difference() or - operator):
The difference between the two sets results in a new set containing elements from the first set but not the second.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
difference_set = set1.difference(set2)
#Or: difference_set = set1 - set2
print(difference_set) # Output: (1, 2)
Time complexities for common set operations in Python can vary:
1. Adding an Element (add() method):
my_set = {1, 2, 3}
my_set.add(4) # 0(1) average case
2. Removing an Element (remove() or discard() methods):
my_set = {1, 2, 3}
my_set.remove(2) # 0(1) average case
3. Membership Test (in operator):
Membership checks using the 'in' operator are typically O(1) on average.
my_set = {1, 2, 3}
if 2 in my set: # 0(1) average case
print("Element found!")
4. Union Operation (union() or | operator):
The union of two sets takes O(len(s)) time, where s is the larger set.
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union (set2)
# 0(len(set1))
5. Intersection Operation (intersection() or & operator):
The intersection of two sets takes O(min(len(s1), len(s2))) time.
set1 = (1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2) # 0(min(len(set1), len(set2)))
6. Difference Operation (difference() or - operator):
The difference between two sets takes O(len(s1)) time.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
difference_set = set1.difference (set2) # 0(len(set1))
7. Clearing a Set (clear() method):
Clearing a set using the clear() method is an O(1) operation.
my_set = (1, 2, 3}
my_set.clear() # 0(1)
Operators for Sets
Sets provide a range of operators, including union (|), intersection (&), and difference (-), which have been detailed earlier.
Python sets are a valuable tool for managing unique data and performing set-based operations. They ensure uniqueness and offer practical solutions for various applications. With this Python set documentation, you can gain the expertise to use sets effectively in your Python applications.
1. How to create a tuple in Python?
You can create a tuple by enclosing elements in parentheses. The elements are separated by commas. Example: my_tuple = (1, 2, 3).
2. What are the common uses of Python frozen sets?
set_of_frozen_sets = {frozenset({1, 2}), frozenset({3, 4})}
print(frozenset({1, 2}) in set_of_frozen_sets) # Output: True
PAVAN VADAPALLI
Director of Engineering
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working …Read More
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...