Chapter 12: Tuples Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Lists are “ “ while tuples are “ “

A

Lists are mutable while tuples are immutable

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

What is the following action called?
tuple(“Safari”)
(‘S’, ‘a’, ‘f’, ‘a’, ‘r’, ‘i’)

A

Unpacking

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

What will happen in the following code?
num = (1, 2, 3, 4)
a, *b, c = num
print(a, b, c)

A

Adding * to b will create a list in the middle section.
»>print(a, b, c)
1 [2, 3] 4

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

How do you delete an item in a tuple?

A

You can’t. Tuples are immutable. You can, however, delete the entire tuple using the del tuple method.

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

I want to count how many times the number 3 shows up in the below tuple. How can I do this?

tuple1 = (1, 2, 3, 4, 3, 3, 3, 3, 66)

A

By using the count function.
»> tuple.count(3)
4

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

What if I want to find the max number listed in a tuple?

A

Use the max function.
»> tuple1 = (1, 4, 90, -177, 43)
»> max(tuple1)
90

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

How could I change a list to a tuple?

A

Using the tuple(list) method
»> my_list = [1, 2, 3, 4]
»> my_list = tuple(my_list)
»> my_list
(1, 2, 3, 4)

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

Consider the below code:
»> my_tuple = ([“Hello”, “World”], [“It’s”, “nice”])
Can we edit the list in this tuple?

A

Yes. By using append or remove.
»> my_tuple[0].append(“hello”)
»>print(my_tuple)
([“Hello”, “World”, “hello”], [“It’s”, “nice”])

or
»> my_tuple[0].remove(“hello”)
»>print(my_tuple)
([“Hello”, “World”], [“It’s”, “nice”])

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

What will the output be for the following code?
»> my_tuple = ([“Hello”, “World”], [“It’s”, “nice”])
»> my_tuple.append([‘x’, ‘y’, ‘z’])
print(my_tuple)

A

You will receive an attribute error. Since we are trying to modify the tuple and not the list we will get the following: AttributeError: ‘tuple’ object has no attribute to ‘append’

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

” “ is a built-in function that takes two or more sequences and returns a list of tuples where each tuple contains one element from each sequence.

A

zip

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

I want a sequence of tuples where each tuple is a key-value pair, how would I accomplish this? Hint: dictionary method

A

By using items which returns a sequence of tuples
tuple is a key-value pair.
»> d = {‘a’:0, ‘b’:1, ‘c’:2}
»> t = d.items()
»> t
dict_items([(‘c’, 2), (‘a’, 0), (‘b’, 1)])

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

True or False
Can you use a list of tuples to initialize a dictionary?

A

True
»> t = [(‘a’, 0), (‘c’, 2), (‘b’, 1)]
»> d = dict(t)
»> d
{‘a’: 0, ‘c’: 2, ‘b’: 1}

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

In some context, like a “ “ statement, it is syntactically simpler to create a tuple than a list.

A

return

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

Lists, dictionaries and tuples are examples of what?

A

data structures

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

An assignment with a sequence on the right side and a tuple of variables on the left. The right side is evaluated and then its elements are assigned to the
variables on the left.

A

tuple assignment

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

The operation * of assembling a variable-length argument tuple.

A

gather

17
Q

The operation of treating a sequence as a list of arguments.

A

scatter

18
Q

The result of calling a built-in function zip; an object that iterates through a
sequence of tuples.

A

zip object

19
Q

An object that can iterate through a sequence, but which does not provide list
operators and methods.

A

iterator

20
Q

A collection of related values, often organized in lists, dictionaries, tuples,
etc

A

data structure

21
Q

An error caused because a value has the wrong shape; that is, the wrong type or size.

A

shape error