Output of Python Programs | Set 20 (Tuples)
Prerequisite : Tuples
Note: Output of all these programs is tested on Python3
1. What will be the output of the following program ?
tuple = (1, 2, 3, 4) tuple.append( (5, 6, 7) ) print(len(my_tuple)) |
Options:
- 1
- 2
- 5
- Error
Output:
4. Error
Explanation: In this case an exception will be thrown as tuples are immutable and don’t have an append method.
2. What will be the output of the following program ?
tuple = {} tuple[(1,2,4)] = 8tuple[(4,2,1)] = 10tuple[(1,2)] = 12_sum = 0for k in tuple: _sum += tuple[k] print(len(tuple) + _sum) |
Options:
- 34
- 12
- 31
- 33
Output:
4. 33
Explanation: Tuples can be used for keys into dictionary. The tuples can have mixed length and the order of the items in the tuple is considered when comparing the equality of the keys.
3. What will be the output of the following program ?
tuple1 = (1, 2, 4, 3) tuple2 = (1, 2, 3, 4) print(tuple1 < tuple2) |
Options:
- Error
- True
- False
- Unexpected
Output:
3. False
Explanation: In this case elements will be compared one by one. So, when it compare 4 with 3 it will return False.
4. What will be the output of the following program ?
tuple = (1, 2, 3) print(2 * tuple) |
Options:
- (1, 2, 3, 1, 2, 3)
- (1, 2, 3, 4, 5, 6)
- (3, 6, 9)
- Error
Output:
1. (1, 2, 3, 1, 2, 3)
Explanation: ‘*’ operator is used to concatenate tuples.
5. What will be the output of the following program ?
tuple=("Check")*3print(tuple) |
Options:
- Unexpected
- (3Check)
- (CheckCheckCheck)
- Syntax Error
Output:
3. (CheckCheckCheck)
Explanation: Here (“Check”) will be treated as is a string not a tuple as there is no comma after the element.
Recommended Posts:
- Output of Python Programs | Set 18 (List and Tuples)
- Output of python program | Set 13(Lists and Tuples)
- Output of python program | Set 12(Lists and Tuples)
- Output of Python programs | Set 8
- Output of Python programs | Set 7
- Output of Python Programs | Set 24 (Sets)
- Output of Python Programs | Set 19 (Strings)
- Output of Python Programs | (Dictionary)
- Output of Python Programs | Set 21 (Bool)
- Output of Python programs | Set 9 (Dictionary)
- Output of Python Programs | Set 22 (Loops)
- Output of Python Programs | Set 23 (String in loops)
- Output of Python programs | Set 10 (Exception Handling)
- Python | Find the tuples containing the given element from a list of tuples
- Output of C++ programs | Set 37
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



