Chapter 14-Tuples, Sets, Dictionaries Flashcards

1
Q

What does a tuple do?

A

Tuples store a fixed list of elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does a dictionary do?

A

A dictionary stores key/value pairs and for accessing elements quickly using the keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are tuples?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you create a tuple?

A

By enclosing a tuple’s elements inside a pair of parentheses. The elements are separated by commas.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Make a basic tuple…NAOW!!

A

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 ‘]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are sets?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you create a set?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you create a set from a list or tuple?

A

list(set)

tuple(set)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the output of this set? s1= {‘abac’}

A

{‘a’, ‘b’, ‘a’}

Remember that sets do not store duplicates.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can a set contain different elements like strings and numbers?

A

Yes they can: s2= {1,2,3, ‘one’, ‘two’, ‘three’}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are some ways you can manipulate a set?

A
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a dictionary(more detailed explanation)?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you add, modify and retrieve values in a dictionary?

A

To add, you use the syntax: dictionaryName[key]=value
ex. students[“234-56-9010”]= “Susan”

If the key is already in the dictionary, the preceding statement replaces the value for the key.

To retrieve a value, write: dictionaryName[key]. The value of the key will be returned.

17
Q

How do you delete an item in a dictionary?

A

del dictionaryName[key]

ex. del students[“234-56-9010”]

18
Q

How do you find the number of items in a dictionary?

A

with the len function:

students={“12-34-5”=”John”, “98-76-5”=”Peter”}
»>len(students)
2

19
Q

How do you test whether a key is in a dictionary or not?

A

use the in, not in functions.

20
Q

What is the python class for dictionary?

A

dict( )

21
Q
Here are the list of methods that can be invoked from a dictionary:
keys( ): tuple
values( ): tuple
items( ): tuple
clear( ): None
get(key): value
pop(key): value
popitem( ): tuple

What do each of them do?

A

keys( )-returns sequences of keys

values( )-return sequences of values

items( )- Returns sequences of tuples. Each tuple is a (key,value) for an item.

clear( )- Deletes all entries

get(key)- Returns the value for the key. The get(key) method is similar to dictionaryName[key]. Except that the get method returns None if the key is not in the dictionary rather than raising an exception.

pop(key)-Removes the item for the key and returns its value. The pop(key) method is the same as
del dictionaryName[key[

popitem( )- Returns a randomly selected key/value pair as a tuple and removes the selected item.

22
Q
Which one of these dictionaries are created correctly?
d = {1:[1, 2], 3:[3, 4]} 
d = {[1, 2]:1, [3, 4]:3}  
d = {(1, 2):1, (3, 4):3} 
d = {1:"john", 3:"peter"} 
d = {"john":1, "peter":3} 

If any are incorrect, explain why it is incorrect?

A
d = {1:[1, 2], 3:[3, 4]}  # Correct
#d = {[1, 2]:1, [3, 4]:3}  # Incorrect, key must be immutable
d = {(1, 2):1, (3, 4):3}  # Correct
d = {1:"john", 3:"peter"} # Correct
d = {"john":1, "peter":3} # Correct

I am assuming that keys must be immutable and values are able to be mutated. Keys are immutable because it is the ‘index’ of the dictionary.

23
Q

What can you use to transverse all elements in a set?

A

A for loop