Computer Science
What will be the output of the following code ?
tuple1 = (1, 2, 3)
tuple2 = tuple1
tuple1 += (4, )
print(tuple1 == tuple2)
- True
- False
- tuple1
- Error
Python Tuples
2 Likes
Answer
False
Reason — In the above code, tuple1
is initially assigned the tuple (1, 2, 3), and tuple2
references the same tuple. When the line tuple1 += (4, )
is executed, it modifies tuple1
to (1, 2, 3, 4) because the +=
operator creates a new tuple rather than modifying the existing one. Thus, tuple1
now contains (1, 2, 3, 4), while tuple2
remains (1, 2, 3). Therefore, the expression tuple1 == tuple2
evaluates to False.
Answered By
2 Likes
Related Questions
What is the output of the expression ?
country = 'International' print(country.split("n"))
('I', 'ter', 'atio', 'aI')
['I', 'ter', 'atio', 'al']
['I', 'n', 'ter', 'n', 'atio', 'n', 'al']
Error
What will be the output of the following code snippet ?
message = "World Peace" print(message[-2::-2])
If my_dict is a dictionary as defined below, then which of the following statements will raise an exception ?
my_dict = {'apple' : 10, 'banana' : 20, 'orange' : 30}
- my_dict.get('orange')
- print(my_dict['apple', 'banana'])
- my_dict['apple'] = 20
- print(str(my_dict))
What does the list.remove(x) method do in Python ?
- Removes the element at index x from the list
- Removes the first occurrence of value x from the list
- Removes all occurrences of value x from the list
- Removes the last occurrence of value x from the list