Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Higher-Order Functions
Python Sets
Common Set operations
Set operations allow you to manipulate and combine sets in Python to create new sets based on specific criteria. Here's a breakdown of the four common set operations:
1. Union: combining elements
The union of two sets (denoted by |) combines all the unique elements from both sets into a new set. There are no duplicates in the resulting set.You can perform the union using the | operator or the union() method.
Example:
set1 = {"apple", "banana", "cherry"} set2 = {"mango", "banana", "grape"} union_set = set1 | set2 # OR union_set = set1.union(set2) print(union_set) # Output: {'apple', 'banana', 'cherry', 'grape', 'mango'}
2. Intersection: common elements
The intersection of two sets (denoted by &) contains only the elements that are common to both sets. It identifies the elements that exist in both sets.
You can perform the intersection using the & operator or the intersection() method.
Example:
set1 = {"apple", "banana", "cherry"} set2 = {"mango", "banana", "grape"} intersection_set = set1 & set2 # OR intersection_set = set1.intersection(set2) print(intersection_set) # Output: {'banana'}
3. Difference: elements in one set:
The difference of two sets (denoted by -) contains elements that are present in the first set but not in the second set. It excludes elements common to both sets.
You can perform the difference using the - operator or the difference() method.
Example:
set1 = {"apple", "banana", "cherry"} set2 = {"mango", "banana", "grape"} difference_set = set1 - set2 # OR difference_set = set1.difference(set2) print(difference_set) # Output: {'apple', 'cherry'}
4. Symmetric difference: elements in either set:
The symmetric difference of two sets (denoted by ^) contains elements that are present in either set but not in both sets. It combines elements unique to each set.
You can perform the symmetric difference using the ^ operator or the symmetric_difference() method.
Example:
set1 = {"apple", "banana", "cherry"} set2 = {"mango", "banana", "grape"} symmetric_difference = set1 ^ set2 # OR symmetric_difference = set1.symmetric_difference(set2) print(symmetric_difference) # Output: {'apple', 'cherry', 'grape', 'mango'}
Remember that these operations create new sets and don't modify the original sets. They provide a powerful way to analyze and manipulate collections of unique elements.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What is the result of the expression "set1 | set2" where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?
D{"mango", "grape"}
A{"apple", "banana", "cherry"}
B{"banana", "grape"}
C{"apple", "banana", "cherry", "mango", "grape"}
Check Answer
What does the expression "set1 & set2" return where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?
A{"banana"}
B{"apple", "cherry"}
C{"mango", "grape"}
D{"apple", "banana", "cherry", "mango", "grape"}
Check Answer
What is the result of "set1 - set2" where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?
D{"apple", "banana", "cherry"}
A{"banana", "grape"}
B{"apple", "cherry"}
C{"mango"}
Check Answer
What does the expression "set1 ^ set2" return where set1 = {"apple", "banana", "cherry"} and set2 = {"mango", "banana", "grape"}?
A{"apple", "cherry", "mango"}
B{"banana", "grape"}
C{"apple", "grape"}
D{"apple", "cherry", "grape", "mango"}
Check Answer
What do set operations in Python return?
CThey return a list of elements.
DThey only return True or False.
AThey modify the original sets.
BThey return a new set without affecting the original sets.
Check Answer