Sets Methods in Python | Python Tutorials for Beginners #lec41 Part3

Sets Methods in Python | Python Tutorials for Beginners #lec41 Part3

Operations on Sets in Python: Difference and Symmetric Difference

Understanding Set Operations

  • The video discusses operations on sets in Python, focusing on difference and symmetric difference, following previous lessons on union and intersection.
  • The presenter introduces the concept of set difference using both methods and operators, demonstrating how to find items in one set that are not present in another.

Set Difference Explained

  • The difference method is explained: set1.difference(set2) returns elements in set1 that are not in set2, akin to a mathematical subtraction.
  • An example illustrates this with names; it shows that "Ram" and "Shyam" are unique to set1, while "Jenny" is excluded as it exists in both sets.

Using Operators for Set Difference

  • The operator approach (set1 - set2) yields the same result as the method but requires both operands to be sets.
  • It’s noted that any iterable can be passed into the difference method, allowing flexibility beyond just sets.

Multiple Sets and Updates

  • When using multiple sets with the difference method (e.g., set1.difference(set2, set3)), operations occur left to right, affecting results based on order.
  • A practical example demonstrates how only "Shyam" remains after applying differences across three sets due to overlapping values.

Updating Sets with Differences

  • The difference_update method modifies a set directly by removing elements found in another set. For instance, updating set1 removes common elements from set2.
  • After executing a difference update on set2, it becomes empty if all its elements exist in another compared set.

Introduction to Symmetric Difference

  • Symmetric difference is defined as returning elements unique to either of two sets but not both. This can be expressed mathematically as (set1 ∪ set2) - (set1 ∩ set2) for clarity.

Practical Application of Symmetric Difference

  • Using the symmetric difference method (set1.symmetric_difference(set2)), results show which items belong exclusively to one of the two sets without overlap.
  • An example reveals that "Jenny," being present in both sets, is excluded from the result while other unique names like "Ram," "Akash," and "Shyam" are included.

Understanding Symmetric Difference in Sets

Basic Concepts of Symmetric Difference

  • The symmetric difference between two sets can be defined as the union of both sets minus their intersection. This means it includes elements that are in either set but not in both.
  • Attempting to perform a symmetric difference operation on multiple sets directly will result in an error, as this function only accepts one argument at a time.

Using Operators for Symmetric Difference

  • An operator can be used to compute the symmetric difference between two sets, allowing for easier implementation compared to method calls. This operator is applicable even when dealing with multiple sets.
  • When applying the symmetric difference operator across three sets, it successfully computes the desired output without errors, demonstrating its flexibility compared to method-based approaches.

Updating Sets with Symmetric Difference

  • The symmetric_difference_update method allows for updating a set by passing another set as an argument. This updates the original set to include only those elements found in either set but not in both.
  • You can also update any set using this method by passing different types of collections (like tuples), which adds new elements while retaining existing ones that meet the criteria of being unique to each collection.

Practical Understanding and Recommendations

  • To fully grasp these operations, it's recommended to study theoretical concepts such as union, intersection, and differences using Venn diagrams before applying them practically through coding exercises.
  • A suggestion is made to conduct dry runs on paper first before executing code on a computer. This helps ensure understanding and accuracy of expected outputs when running actual code.
Video description

In this lecture we will learn: - set Methods in Python - set difference and difference_update - set symmetric difference and symmetric_difference_update Best Python Tutorials for Beginners: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bZSiqiOL5ta39vSnBxpOPT ********************************************* Connect & Contact Me: My Second Channel Link: https://bit.ly/354n7C7 Facebook: https://www.facebook.com/Jennys-Lectures-CSIT-Netjrf-316814368950701/ Quora: https://www.quora.com/profile/Jayanti-Khatri-Lamba Instagram: https://www.instagram.com/jayantikhatrilamba/ Twitter: https://twitter.com/KhatriJenny ******************************************* More Playlists: Programming in C Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31a8UcMN9-35ghv8qyFWD9_S C++ Tutorials for beginners: https://www.youtube.com/watch?v=KYxLEDF6kjs&list=PLdo5W4Nhv31YU5Wx1dopka58teWP9aCee Placement Series: https://www.youtube.com/playlist?list=PLdo5W4Nhv31YvlDpJhvOYbM9Ap8UypgEy Data Structures and Algorithms: https: https://www.youtube.com/playlist?list=PLdo5W4Nhv31bbKJzrsKfMpo_grxuLl8LU Design and Analysis of Algorithms(DAA): https://www.youtube.com/playlist?list=PLdo5W4Nhv31ZTn2P9vF02bkb3SC8uiUUn Dynamic Programming: https://www.youtube.com/playlist?list=PLdo5W4Nhv31aBrJE1WS4MR9LRfbmZrAQu Operating Systems tutorials: //www.youtube.com/playlist?list=PLdo5W4Nhv31a5ucW_S1K3-x6ztBRD-PNa DBMS Tutorials: https://www.youtube.com/playlist?list=PLdo5W4Nhv31b33kF46f9aFjoJPOkdlsRc #coding #pythonforbeginners #python #jennyslectures #pythonprogramming