Sets

Calvin (Deutschbein)

W11Wed: 06 Nov

Announcements

  • Midterm Friday.
    • Practice Exam posted.
    • Tuples
    • Pixels
    • Lists
    • Types

Today

  • Review
    • Tuples
    • Pixels
    • Lists
    • Types
  • Set
    • Set Operations
    • Venn Diagrams
    • Set Methods

Review: Tuples

  • You need to be able to figure out tuple addition and assignment. >>> x = 1,2 >>> x (1, 2) >>> x,y = 1,2 >>> x 1 >>> y 2 >>> x + x,y (2, 2)
  • Think of it more as x+x, y
  • (1+1, 2)

Review: Pixels

  • You will need to be able to work with pixels and colors. def to_color(rgbs:list[int]) -> str: hexes = [hex(brightness) for brightness in rgbs] hexes = [color.replace('x','0') for color in hexes] hexes = [color[-2:] for color in hexes] # last two return '#' + "".join(hexes)
  • We can test this easily enough... >>> to_color([100,0xff,255]) '#64ffff'
  • From here

Review: Pixels

  • You will need to be able to work with pixel arrays. pix = 800 # number of pixels gw = GWindow(pix, pix) image = GImage("cat.png") gw.add(image) pixels = image.get_pixel_array() print(type(pixels))
  • From here

Review: Pixels

  • You will need to be able to check types. if type(data) == int: self._val = data else: self._val = 0
  • From here

Today

  • ✓ Review
  • Set
    • Set Operations
    • Venn Diagrams
    • Set Methods

Sets

  • !!! Sets cannot contain duplicates!!!
  • Enclosed within squiggly brackets like dictionaries
  • Values only - no indices or selectors.
  • >>> films = {"Alien","Aliens", "Alien 3", "Alien Resurrection"} >>> films {'Aliens', 'Alien', 'Alien 3', 'Alien Resurrection'}
  • Don't try to use indices!
  • >>> films[0] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'set' object is not subscriptable >>>

Sets

  • !!! Sets cannot contain duplicates!!!
  • Set elements must be immutable
  • Sets themselves are generally mutable
  • Can not create an empty set just using s = { }
    • Python assumes this to be an empty dictionary!
  • Must instead use s = set()

Set Operations

  • !!! Sets cannot contain duplicates!!!
  • The fundamental set operation is membership (∈)
    • AlienAlien Franchise
    • AlienAvatar Franchise

Set Operations

  • !!! Sets cannot contain duplicates!!!
  • The union of the sets A and B (A ∪ B) consists of all elements in either A or B or both.
    • AlienAlien filmsAvatar films
    • That is, Alien is in either of the Alien or Avatar franchises (sets of films)
  • The intersection of the sets A and B (A ∩ B) consists of all elements in both A and B.
    • Sigourney Weaver ∈ Alien starsAvatar stars
    • That is, Sigourney Weaver starred in both an Alien film and an Avatar film (sets of actors)

Set Operations

  • !!! Sets cannot contain duplicates!!!
  • The set difference of A and B (A − B) consists of all elements in A but not in B.
    • Alien: ResurrectionAlien vs Predator filmsPredator films
    • That is, "Alien: Resurrection" is in the Alien vs Predator series but not a Predator film.
  • The symmetric set difference of A and B (A △ B) consists of all elements in A or B but not in both.
    • "Aliens" and "Predators" are both in the symetric set difference of Alien and Predator films, but Alien vs Predator would not be in this set difference (it is in both series.

Today

  • ✓ Review
  • Set
    • ✓Set Operations
    • Venn Diagrams
    • Set Methods

Venn Diagrams

  • A Venn Diagram of set union, denoted in python as A | B
image/svg+xml A B A ∪ B
  • I think of this as: >>> alien_orig # films prior to 2000 {'Aliens', 'Alien', 'Alien 3', 'Alien Resurrection'} >>> alien_preq # films after 2000 {'Alien: Covenant', 'Alien Romulus', 'Promethous'} >>> alien_orig | alien_preq # the alien series is composed of both "trilogies" {'Aliens', 'Alien Resurrection', 'Alien: Covenant', 'Alien 3', 'Alien Romulus', 'Alien', 'Promethous'}

Venn Diagrams

  • A Venn Diagram of set difference, denoted in Python via A - B
image/svg+xml A B A ∪ B A B A ∩ B B A - B A
  • I think of this as: >>> alien_all = alien_orig | alien_preq >>> avp = {"Alien vs Predator", "Alien vs Predator: Requiem"} >>> alien_both = alien_orig | alien_preq >>> alien_all = alien_both | avp >>> alien_all - alien_orig # all films since 2000 {'Alien: Covenant', 'Alien Romulus', 'Alien vs Predator: Requiem', 'Alien vs Predator', 'Promethous'}

Venn Diagrams

  • A Venn Diagram of set difference, denoted in Python via A & B
image/svg+xml A B A ∪ B A B A ∩ B
  • I think of this as: >>> pred_all = {"Predator", "Predator 2", "Predators", "Prey"} | avp >>> pred_all & alien_all {'Alien vs Predator: Requiem', 'Alien vs Predator'}

Venn Diagrams

  • A Venn Diagram of set difference, denoted in Python via A - B
image/svg+xml A B A ∪ B A B A ∩ B A B A - B A B A ∆ B
  • I think of this as: >>> pred_all ^ alien_all {'Predators', 'Alien: Covenant', 'Alien Romulus', 'Alien', 'Predator 2', 'Predator', 'Aliens', 'Alien Resurrection', 'Alien 3', 'Prey', 'Promethous'} >>> "Alien vs Predator" in pred_all ^ alien_all False

Set Relationships

  • Sets A and B are equal (A = B) if they have the same elements.
    • This would make them the same circles in a Venn diagram
  • Set A is a subset of B (A ⊆ B) if all the elements in A are also in B.
    • This would mean that the circle for A would be entirely inside (or equal to) the circle of B
  • Set A is a proper subset of B (A ⊂ B) if A is a subset of B and the two sets are not equal

Informal Proofs

  • You can use Venn diagrams to justify different set identities
  • Example: Say you wanted to show that: A − (B ∩ C) = (A − B) ∪ (A − C)
    • image/svg+xml A B C A B C B C A B C A B C A B C A

Today

  • ✓ Review
  • Set
    • ✓ Set Operations
    • ✓ Venn Diagrams
    • Set Methods

Python Set Methods

  • Can also use “set comprehension” to generate a set { x for x in range(0,100,2) }
    • Function Description
      len(s) Returns the number of elements in a set
      x in s True if the value of x is contained in the set denoted by s
      s.copy() Creates and returns a copy of the set
      s.add(x) Adds the the value of x to the set denoted by s
      s.remove(x) If the value of x is in s, removes it, or raises an error
      s.discard(x) Removes without the error case.

Why use sets?

Sets come up naturally in many situations
Many real-world applications involve unordered collections of unique elements, for which sets are the natural model.
Sets have a well-established mathematical foundation
If you can frame your application in terms of sets, you can rely on the various mathematical properties that apply to sets.
Sets can make it easier to reason about your program
One of the advantages of mathematical abstraction is that using it often makes it easy to think clearly and rigorously about what your program does.
Many important algorithms are described in terms of sets
If you look at websites that describe some of the most important algorithms in computer science, many of them base those descriptions in terms of set operations.

Representing Data

  • To use computation effectively, we frequently need to be able to represent real world data in a way that computers can easily work with
    • Real world data is often more complicated or nuanced than just “a list of numbers”
  • Python’s existing data structures are tools, which you can use to help represent certain ideas
    • Lists when you have sequential type data, wherein there is a logical ordering to the data in question (where position matters)
      • Example: Chronology of Alien films
    • Tuples or classes when you have elements that should be grouped together but which have no inherent ordering. Generally use tuples for simple records and write custom classes for more complex. Could potentially also use a dictionary.
      • Example: ??? I don't use these
      • Emphasis mine: Could potentially also use a dictionary.
    • Maps or dictionaries when you have specific keys corresponding to other values.
      • Example: Alien and Predator films where I don't know release order across the two series

Today

  • Review
    • Tuples
    • Pixels
    • Lists
    • Types
  • Set
    • Set Operations
    • Venn Diagrams
    • Set Methods

Announcements

  • Midterm Friday.
    • Practice Exam posted.
    • Tuples
    • Pixels
    • Lists
    • Types
  • Go to your sections!