Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Set basics
Relationships between sets
Summary
15. Summary

Instruction

All right, it's time to wrap things up. First, a summary of what we've learned:

  1. Basic set operations are given below with comments:

    # define a non-empty set
    prime_numbers = {2, 3, 5, 7}
    
    # define an empty set
    prime_numbers = set()
    
    # add an element
    prime_numbers.add(11)
    
    # remove an element
    prime_numbers.discard(11)
    
    # remove all elements
    prime_numbers.clear()
    
    # iterate over a set
    for number in prime_numbers: ...
    
    # check if an element is in the set
    if number in prime_numbers: ...
    
  2. The following functions are used to create a new set from two sets: intersection(), union(), difference(), and symmetric_difference().
  3. To specify the relationship between two sets, we can use issubset() and issuperset().

Are you ready for a short quiz?

Exercise

Click Next exercise to continue.