0% found this document useful (0 votes)
162 views2 pages

Quiz:Tuples

Lists are mutable while tuples are immutable. Both support the index() method. The variable y would be assigned the value 4 after the code x, y = 3, 4 runs. The variable y would contain a list of tuples after the code x = {'chuck': 1, 'fred': 42, 'jan': 100} and y = x.items() runs. The tuple (6, 0, 0) is greater than the tuple (5, 1, 3). The code creates a list of value, key tuples from the items of dictionary c. The data list can be sorted in reverse order using data.sort(reverse=True). The string 'Wed' can be printed from the days tuple using print(
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views2 pages

Quiz:Tuples

Lists are mutable while tuples are immutable. Both support the index() method. The variable y would be assigned the value 4 after the code x, y = 3, 4 runs. The variable y would contain a list of tuples after the code x = {'chuck': 1, 'fred': 42, 'jan': 100} and y = x.items() runs. The tuple (6, 0, 0) is greater than the tuple (5, 1, 3). The code creates a list of value, key tuples from the items of dictionary c. The data list can be sorted in reverse order using data.sort(reverse=True). The string 'Wed' can be printed from the days tuple using print(
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

1 - What is the difference between a Python tuple and Python list?

Answer: Lists are mutable and tuples are not mutable


// An advantage is that tuples are safe data by is the incapacity to change.

2 - Which of the following methods are available for both Python lists and Python
tuples?

Answer: index()
// The other methods imply changing the data in the tuple.

3 - What will end up in the variable y after this code is executed?

x , y = 3, 4

Answer: 4
// I was confused because I feel that the redaction was not clear, I think it
might be better
like this: " What will end up in the variable: y, after..." or "What will end up
in the variable "y" after...".
In any case; "x" is assigned the first value on the other side of the equal sign,
and "y" the second, if you
invert the position of the variables like "y , x = 3, 4", "y" will be 3.

4 - In the following Python code, what will end up in the variable y?

x = { 'chuck' : 1 , 'fred' : 42, 'jan': 100}


y = x.items()

Answer: A list of tuples


// items() always return a tuple, a safe data ;).

5 - Which of the following tuples is greater than x in the following Python


sequence?

x = (5, 1, 3)
if ??? > x :
...

Answer: (6, 0, 0)
//The "<" and ">" compares the first number (index[0]) of the tuples,
and only if they equal the comparison will execute the next index.

6 - What does the following Python code accomplish, assuming the c is a non-empty
dictionary?

tmp = list()
for k, v in c.items() :
tmp.append( (v, k) )
Answer: It creates a list of tuples where each tuple is a value, key pair
// It can be confusing, but is a series tuples inside of a list, for example:
lst = ({a,4}, {b, 9}, {c, 7}....). The {} represents the tuples in this case.

7 - If the variable data is a Python list, how do we sort it in reverse order?

Answer: data.sort(reverse=True)
// Without "reverse=True" condition, the .sort() will be executed in normal mode.

8 - Using the following tuple, how would you print 'Wed'?

days = ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')

Answer: print(days[2])
// Tuples work with the index system.

9 - In the following Python loop, why are there two iteration variables (k and v)?

c = {'a':10, 'b':1, 'c':22}


for k, v in c.items() :
...

Answer: Because the items() method in dictionaries returns a list of tuples


// Se question 4.

10 - Given that Python lists and Python tuples are quite similar - when might you
prefer to use a tuple over a list?

Answer: For a temporary variable that you will use and discard without modifying
// And is safe use.

You might also like