Chapter 14-Tuples, Sets, Dictionaries Flashcards
What does a tuple do?
Tuples store a fixed list of elements
What does a dictionary do?
A dictionary stores key/value pairs and for accessing elements quickly using the keys.
What are tuples?
Tuples are like lists, but their elements are fixed;that is, once a tuple is created, you cannot add new elements, delete elements, replace elements or reorder elements in the tuple.
Just like lists, tuples are sequences so you may use functions like len, min, max, sum, in, not in. A for loop can be used to traverse all elements in a tuple and the tuple elements can be accessed using an index operator.
How do you create a tuple?
By enclosing a tuple’s elements inside a pair of parentheses. The elements are separated by commas.
Make a basic tuple…NAOW!!
looks like anything:
t1=( )
t2=(1,3,5)
t3=tuple([2 * x for x in range(1 , 5)])
You can also create a tuple from a string. Each character in the string becomes an element in the tuple:
t4=tuple(‘abac’) #t4 is [’ a ‘, ‘ b ‘, ‘ a ‘, ‘ c ‘]
What are sets?
Sets are like lists in that you use them for storing a collection of elements. Unlike lists however, the elements in a set are nonduplicates and are not placed in any particular order.
How do you create a set?
You create a set of elements by enclosing the elements inside a pair of curly braces. The elements are separated by commas.
You can create an empty set or from a list or a tuple.
Create an empty set, a set from a list and a set from a tuple.
Also, create a set from a list, what would the output look like?
s1=set( )
s2= {1,3,5} #set with three elements
s3=set( [ 1,3,5] ) #set from a tuple
s4=set( [x*2 for x in range(1,10) ] )
s5=set(‘aba’)
output–> {‘a’, ‘b’, ‘a’}
How do you create a set from a list or tuple?
list(set)
tuple(set)
What is the output of this set? s1= {‘abac’}
{‘a’, ‘b’, ‘a’}
Remember that sets do not store duplicates.
Can a set contain different elements like strings and numbers?
Yes they can: s2= {1,2,3, ‘one’, ‘two’, ‘three’}
What are some ways you can manipulate a set?
add(e) ex. s2= {1,2,4} s1.add(6) >>s1 output--> {1,2,4,6} remove(e) #the above add and remove elements from a set
len ,min,max, sum
you can use a for loop to transverse all elements in a set
in, not in (used to determine whether or not an element is in a set)
s1= {1,2,4}
s2={1,4,5,2,6}
How do you check if s2 is a subset of s1? What is a subset?
s1= {1,2,4}
s2={1,4,5,2,6}
–>s1.issubset(s2)
True
A set is a subset of the other if every element, say in s1 is in s2. Which in this case is true. (The bigger set has some elements that match with the smaller one)
s1= {1,2,4}
s2={1,4,5,2,6}
How do you check s1 us a super set of s2? What is a superset?
s1= {1,2,4}
s2={1,4,5,2,6}
s2.issuperset(s1)
True
A super set is if a set like s1 is a superset of set s2; if every element in s2 is also in s1. (The smaller set is in the bigger set)
(ignore the super and subset, it’s being confusing on page 480) ( I am skipping sets, they have that subset shit in it)
What is a dictionary(more detailed explanation)?
A dictionary is a container object that stores a collection of key/value pairs. It enables fast retrieval, deletion and updating of the value by using the key.