Exploring Python Sets: Operations, Creation, and Manipulation

python set n.w
1 / 14
Embed
Share

Learn about Python sets - an unordered collection of unique and immutable elements that allows for mathematical set operations like union, intersection, and more. Discover how to create sets, add elements, and perform various set operations in Python programming.

  • Python Sets
  • Set Operations
  • Data Structures
  • Python Programming

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Python Set By Dr. Ziad Al-Sharif

  2. Python Sets A set is an unordered collection of items. Every element is unique and must be immutable. Python sets are mutable; you can add and remove items from it. Sets can be used to perform mathematical set operations like: Union : Intersection symmetric difference etc.

  3. Creating a Set A set is created by placing all the elements inside curly braces {}, separated by comma or by using the built-in function set(). The elements can be of different types (integer, float, tuple, string etc.). But a set cannot have a mutable element, like list, set or dictionary, as its element.

  4. A set can contain elements of different type A set can contain elements of different type

  5. We can convert a list to set using set function

  6. Adding elements to a set Adding elements to a set Sets are mutable. But since they are unordered, indexing have no meaning. We cannot access or change an element of set using indexing or slicing. We can add single element using the add() method and multiple elements using the update()method. add() for single elements update() for iterable The update() method can take tuples, lists, strings or othersets as its argument. In all cases, duplicates are avoided.

  7. Python Set Operations Sets can be used to carry out mathematical set operations like union, intersection, difference and symmetricdifference. We can do this with operators or methods. union | intersection & difference - Symmetric difference ^

  8. Set Operations If A and B are two python sets A = {1,2,3,4} B = {2,4,6,8, 6, 6} print('A= ',A) print('B= ',B) s1 = A | B # elements in a or b or both print( Union: A | B =',s1) s2 = A & B # elements in both a and b print( Intersection: A & B =',s2) s3 = A - B # elements in a but not in b print( Difference: A - B =',s3) s4 = A ^ B # elements in a or b but not both print( Symmetric Diff: A ^ B =',s4)

  9. Updating the set We have seen the applications of union, intersection, difference and symmetric difference operations, but these operations do not make any changes or mutations to the set. We can use the following operations to create mutations to a set Method Operator Update the set by adding elements from an iterable/another set. Update |= Update the set by keeping only the elements found in it and an iterable/another set. Intersection Update &= Update the set by removing elements found in an iterable/another set. Difference Update -= Update the set by only keeping the elements found in either set, but not in both. Symmetric Difference Update ^=

  10. Operations on a set operation len(s) x in s x not in s set <= other set < other set >= other set > other Return the number of elements in set s (cardinality of s). Test x for membership in s. Test x for non-membership in s. Test whether every element in the set is in other. Test whether the set is a propersubset of other Test whether every element in other is in the set. Return a new set with elements from the set and all others.

  11. Methods Method add(elem) remove(elem) discard(elem) clear() pop() Description Add element elem to the set. Remove element elem from the set. Error if elem is not contained in the set. Remove element elem from the set if it is present. Remove all elements from the set. Remove and return an arbitrary element from the set. Error if the set is empty. Method isdisjoint(other) Description Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set. issubset(other) set <= other issuperset(other) set >= other Test whether every element in the set is in other. Test whether every element in other is in the set

  12. Adding elements to a set

  13. Set Comprehensions >>> >>> L = [1,3,2,6,4] >>> ll = [x*10 for x in L] >>> ll [10, 30, 20, 60, 40] >>> ss = {x*10 for x in L} >>> ss {40, 10, 20, 60, 30} >>> L.extend([1,2]) >>> L [1, 3, 2, 6, 4, 1, 2] >>> ss = {x*10 for x in L} >>> ss {40, 10, 20, 60, 30} >>> >>> a = {1,2,3} >>> b = {x*2 for x in a|{4,5}} >>> b {2, 4, 6, 8, 10} >>> >>> type(b) <class 'set'> >>>

  14. Reference Set Types set, frozenset https://docs.python.org/3/library/stdtypes.html?highlight=set%20isdisjoint#s et-types-set-frozenset

More Related Content