The Wayback Machine - https://web.archive.org/web/20240213201546/https://www.geeksforgeeks.org/output-python-programs-set-20-tuples/
Open In App
Related Articles

Output of Python Programs | Set 20 (Tuples)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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. 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 ?




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 ?




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 ?




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 ?




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.


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 18 Sep, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads