Title | Chapter 9 - 9.4 Sets - Lecture notes 34 |
---|---|
Author | Kyle Frudakis |
Course | Python Programming |
Institution | Florida Atlantic University |
Pages | 5 |
File Size | 412.8 KB |
File Type | |
Total Downloads | 17 |
Total Views | 159 |
Learn what a set is (python data structure)
how to call it, what it can do, how it is different from other data structures...
Chapter 9 - 9.4 Sets Wednesday, June 30, 2021
11:31 PM
History • sets are something you learn at an early age What's in a Set? • A set is a collection of objects, regardless of the objects' type • These are the elements or numbers of the set • Only one copy of any element may exists in the set ○ useful characteristic No order of elements in a set • ○ just like the dictionary • A set with nothing in it is called an "empty set or the "null set" • A set is iterable ○ as are all collections we have seen Python Sets • a set is created by calling the set constructor or using curly braces and commas ○ it may be confusing that this too involves curly braces because a dictionary uses the same thing ○ Difference is that there is no colon in a set (:) ○ in a set, it is just separated by commas MUST use the set constructor to make an empty set • ○ empty curly braces makes is for dictionary
•
• notice the last bit^ ○ set constructor with iterable argument separates it by each value • Notice the duplicates being eradicated as well • Sets are a mutable data structure ○ like lists and dictionaries • index assignment is not possible in sets ○ not a sequence
Methods, Operators, and Functions for Python Sets • len() • in • for
○
Set Methods • typical mathematical set operations • two ways to call these operations 1. using a method i. intersection, union, difference and symmetric_difference, issubset, issuperset ii. a set calls the method (using the dot notation) with another collection as the argument 2. using a binary operator i. &, |, -, ^, = ii. Each binary operator takes two sets • Methods approach allows the argument to be any interable collection • Binary approach requires both arguments to be sets • Intersection ○ using the & operator or the intersection method ○ creates a new set of the elements that are common to both sets ○ order of sets does not matter
▪
• Union ○ using the | operator or the union method ○ creates new set that contains all the elements in both sets
○ order does not matter
▪
• Difference ○ using the - operator or the difference method ○ creates a new set whose elements are in the first(calling) set and not in the second(argument) set ○ not commutative
▪
• Symmetric Difference ○ The opposite of intersection ○ creates a new set of values that are different, not in either, of the two sets ○ operator is ^ and the method is symmetric_difference ○ order does not matter
▪
• Subset and Superset ○ a set is a subset of another set only if every element of the first set is an element of the second set ○ set A is a superset of set B only if set B is a subset of set A ○ order matters (not commutative) ○ A set is both a subset and a superset of itself ○ operator is = ▪ method names are issubset and issuperset ○ All four of these operations return a Boolean value
▪
▪...