Open In App

Output of Python Programs | Set 20 (Tuples)

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Tuples

Note: The output of all these programs is tested on Python3

1. What will be the output of the following program?

Python3
tuple = (1, 2, 3, 4)
tuple.append( (5, 6, 7) )
print(len(tuple))

Options:

  1. 1
  2. 2
  3. 5
  4. 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 ?

Python3
tuple = {}
tuple[(1,2,4)] = 8
tuple[(4,2,1)] = 10
tuple[(1,2)] = 12
_sum = 0
for k in tuple:
    _sum += tuple[k]
print(len(tuple) + _sum)

Options:

  1. 34
  2. 12
  3. 31
  4. 33

Output:

4. 33

Explanation: Tuples can be used for keys into dictionary. The tuples can have mixed lengths 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 ?

Python3
tuple1 = (1, 2, 4, 3)
tuple2 = (1, 2, 3, 4)
print(tuple1 < tuple2)

Options:

  1. Error
  2. True
  3. False
  4. Unexpected

Output:

3. False

Explanation: In this case elements will be compared one by one. So, when it compares 4 with 3 it will return False.

4. What will be the output of the following program ?

Python3
tuple = (1, 2, 3)
print(2 * tuple)

Options:

  1. (1, 2, 3, 1, 2, 3)
  2. (1, 2, 3, 4, 5, 6)
  3. (3, 6, 9)
  4. 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 ?

Python3
tuple=("Check")*3
print(tuple) 

Options:

  1. Unexpected
  2. (3Check)
  3. CheckCheckCheck
  4. 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.


Previous Article
Next Article

Similar Reads

Output of Python Programs | Set 18 (List and Tuples)
1) What is the output of the following program? C/C++ Code L = list('123456') L[0] = L[5] = 0 L[3] = L[-2] print(L) a) [0, '2', '3', '4', '5', 0] b) ['6', '2', '3', '5', '5', '6'] c) ['0', '2', '3', '5', '5', '0'] d) [0, '2', '3', '5', '5', 0] Ans. (d) Explanation: L[0] is '1' and L[5] is '6', both of these elements will be replaced by 0 in the Lis
3 min read
Output of python program | Set 12(Lists and Tuples)
Prerequisite: List and Tuples Note: Output of all these programs is tested on Python3 1) What is the output of the following program? C/C++ Code L1 = [] L1.append([1, [2, 3], 4]) L1.extend([7, 8, 9]) print(L1[0][1][1] + L1[2]) a) Type Error: can only concatenate list (not "int") to list b) 12 c) 11 d) 38 Ans: (c) Explanation: In the print(), indexi
3 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Output of Python Programs | Set 21 (Bool)
Prerequisite : Boolean Note: Output of all these programs is tested on Python31. What is the output of the code: C/C++ Code print(bool('False')) print(bool()) False, TrueNone, NoneTrue, TrueTrue, False Output: 4. True, False Explanation: If the argument passed to the bool function does not amount to zero then the Boolean function returns true else
2 min read
Output of Python Programs | Set 22 (Loops)
Prerequisite: Loops Note: Output of all these programs is tested on Python3 1. What is the output of the following? mylist = ['geeks', 'forgeeks'] for i in mylist: i.upper() print(mylist) [‘GEEKS’, ‘FORGEEKS’]. [‘geeks’, ‘forgeeks’]. [None, None]. Unexpected Output: 2. [‘geeks’, ‘forgeeks’] Explanation: The function upper() does not modify a string
2 min read
Output of Python Programs | Set 23 (String in loops)
Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i … g e e k s f o r g e e k s Output: 1. None Explanation: 'i' is not present in string
2 min read
Output of Python Programs | Set 24 (Sets)
Prerequisite: Python-Sets 1. What is the output of the code shown below? sets = {1, 2, 3, 4, 4} print(sets) Options: {1, 2, 3} {1, 2, 3, 4} {1, 2, 3, 4, 4} Error Output: 2. {1, 2, 3, 4} Explanation : Duplicate values are not allowed in sets. Hence, the output of the code shown above will be a set containing the duplicate value only once. Hence outp
2 min read
Output of Python Programs | Set 19 (Strings)
1) What is the output of the following program? C/C++ Code str1 = '{2}, {1} and {0}'.format('a', 'b', 'c') str2 = '{0}{1}{0}'.format('abra', 'cad') print(str1, str2) a) c, b and a abracad0 b) a, b and c abracadabra c) a, b and c abracadcad d) c, b and a abracadabra Ans. (d) Explanation: String function format takes a format string and an arbitrary
3 min read
Output of C Programs | Set 1
Predict the output of below programs. Question 1 C/C++ Code #include<stdio.h> int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; } Output:Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2 C/C++ Code #include<stdio.h> int main() { pr
2 min read
Output of C Programs | Set 2
Predict the output of below programs. Question 1 C/C++ Code #include<stdio.h> char *getString() { char str[] = "Will I be printed?"; return str; } int main() { printf("%s", getString()); getchar(); } Output: Some garbage value The above program doesn't work because array variables are stored in Stack Section. So, when getS
2 min read