Computer Science
Carefully read the given code fragments and figure out the errors that the code may produce.
t3 = (6, 7)
t4 = t3 * 3
t5= t3 * (3)
t6 = t3 * (3,)
print(t4)
print(t5)
print(t6)
Python Tuples
5 Likes
Answer
Output
TypeError: can't multiply sequence by non-int of type 'tuple'
Explanation
The repetition operator *
replicates the tuple specified number of times. The statements t3 * 3
and t3 * (3)
are equivalent as (3)
is an integer not a tuple because of lack of comma inside parenthesis. Both the statements repeat t3
three times to form tuples t4
and t5
.
In the statement, t6 = t3 * (3,)
, (3,)
is a single element tuple and we can not multiply two tuples. Hence it will throw an error.
Answered By
2 Likes
Related Questions
Carefully read the given code fragments and figure out the errors that the code may produce.
t1 = (3,) t2 = (4, 5, 6) t3 = t1 + t2 print (t3)
Carefully read the given code fragments and figure out the errors that the code may produce.
t2 = (4, 5, 6) t3 = (6, 7) print(t3 - t2)
Carefully read the given code fragments and figure out the errors that the code may produce.
odd= 1,3,5 print(odd + [2, 4, 6])[4]
Carefully read the given code fragments and figure out the errors that the code may produce.
t = ( 'a', 'b', 'c', 'd', 'e') 1, 2, 3, 4, 5, = t