Question 32: Write a Python program to create a set, add member(s) in a set and remove one item from set. Download hole Program / Project code, by clicking following link: 👉 Click Here: How to create, read, update and remove set values in Python
What is Set in Python ? In Python, a set is an unordered collection of unique elements. It is a built-in data type used for storing a collection of items, but unlike lists or tuples, sets do not allow duplicate values. Sets are defined by enclosing a comma-separated sequence of elements within curly braces {} or by using the built-in set() constructor.
Here's how you can create a set in Python:
Using curly braces:
my_set = {1, 2, 3, 4, 5}
Using the set() constructor:
my_set = set([1, 2, 3, 4, 5])
Key characteristics of sets in Python: - Uniqueness: Sets only store unique elements. If you attempt to add a duplicate element to a set, it won't be added.
my_set = {1, 2, 3, 4, 5, 2}
# Result: my_set will contain {1, 2, 3, 4, 5} (no duplicates)
- Unordered: Sets are unordered, which means the elements have no specific order. You cannot access elements by index, as you would with lists or tuples.
- Mutability: Sets themselves are mutable, which means you can add or remove elements. However, the set elements must be immutable (e.g., numbers, strings, or tuples), as sets are based on a hash table.
Common operations you can perform with sets include: - Adding elements using add() or update().
- Removing elements using remove() or discard().
- Checking for membership using in.
- Performing set operations such as union, intersection, difference, and symmetric difference.
Here are some examples of set operations:
# Creating sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# Union of sets
union_set = set1 | set2 # or set1.union(set2)
# Intersection of sets
intersection_set = set1 & set2 # or set1.intersection(set2)
# Difference of sets
difference_set = set1 - set2 # or set1.difference(set2)
# Symmetric difference of sets
symmetric_difference_set = set1 ^ set2 # or set1.symmetric_difference(set2) Sets are particularly useful when you need to perform operations that require unique elements, such as finding the intersection of two collections or removing duplicates from a list. Programming Code: Following code write in: BP_P32.py # Set in Python
# create set
set1 = set()
print("set1 type: ", type(set1))
# add items in set
set1.add(60)
print("set1 is: ", set1)
set1.add(50)
print("set1 is: ", set1)
set1.add(70)
print("set1 is: ", set1)
# Auto manage number in ascending order
set1.discard(60)
print("After remove item from set1 is: ", set1)
# add() method used to add items in set
# discard() method is used to remove items from set
# Thnaks for Reading.
Output:
What is Set in Python ?
In Python, a set is an unordered collection of unique elements. It is a built-in data type used for storing a collection of items, but unlike lists or tuples, sets do not allow duplicate values. Sets are defined by enclosing a comma-separated sequence of elements within curly braces {} or by using the built-in set() constructor.
Here's how you can create a set in Python:
Here's how you can create a set in Python:
Using curly braces:
my_set = {1, 2, 3, 4, 5}
Using the set() constructor:
my_set = set([1, 2, 3, 4, 5])
Key characteristics of sets in Python:
- Uniqueness: Sets only store unique elements. If you attempt to add a duplicate element to a set, it won't be added.
my_set = {1, 2, 3, 4, 5, 2}
# Result: my_set will contain {1, 2, 3, 4, 5} (no duplicates) - Unordered: Sets are unordered, which means the elements have no specific order. You cannot access elements by index, as you would with lists or tuples.
- Mutability: Sets themselves are mutable, which means you can add or remove elements. However, the set elements must be immutable (e.g., numbers, strings, or tuples), as sets are based on a hash table.
- Adding elements using add() or update().
- Removing elements using remove() or discard().
- Checking for membership using in.
- Performing set operations such as union, intersection, difference, and symmetric difference.
Here are some examples of set operations:
# Creating sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# Union of sets
union_set = set1 | set2 # or set1.union(set2)
# Intersection of sets
intersection_set = set1 & set2 # or set1.intersection(set2)
# Difference of sets
difference_set = set1 - set2 # or set1.difference(set2)
# Symmetric difference of sets
symmetric_difference_set = set1 ^ set2 # or set1.symmetric_difference(set2)
# Creating sets
set1 = {1, 2, 3, 4, 5}
set2 = {3, 4, 5, 6, 7}
# Union of sets
union_set = set1 | set2 # or set1.union(set2)
# Intersection of sets
intersection_set = set1 & set2 # or set1.intersection(set2)
# Difference of sets
difference_set = set1 - set2 # or set1.difference(set2)
# Symmetric difference of sets
symmetric_difference_set = set1 ^ set2 # or set1.symmetric_difference(set2)
Sets are particularly useful when you need to perform operations that require unique elements, such as finding the intersection of two collections or removing duplicates from a list.
# Set in Python
# create set
set1 = set()
print("set1 type: ", type(set1))
# add items in set
set1.add(60)
print("set1 is: ", set1)
set1.add(50)
print("set1 is: ", set1)
set1.add(70)
print("set1 is: ", set1)
# Auto manage number in ascending order
set1.discard(60)
print("After remove item from set1 is: ", set1)
# add() method used to add items in set
# discard() method is used to remove items from set
# Thnaks for Reading.
Output:
0 Comments