Predict the output 

t=tuple() 
t = t + (‘Python’,) 
print t 
print len(t) 
t1= (10,20,30) 
print len(t1) 

 

Answer:

The output of the code is:

('Python',)

1

3

Explanation:

  • The code creates an empty tuple by using a pair of empty parentheses and assigns it to a variable t. 
  • The code then creates another tuple with one element ‘Python’ and assigns it to a variable t. This is done by using a comma after the element to indicate that it is not just a parenthesis around an expression. The code then uses the + operator to concatenate (join) the two tuples and assign the result back to t. 
  • The code then prints the value of t using the print() function. The print() function displays the output to the standard output device (usually the screen). The output of this line is (‘Python’,), which is a tuple with one element.
  • The code then prints the length of t using the len() function and the print() function. The len() function returns the number of elements in a sequence (such as a tuple). The output of this line is 1, which is the number of elements in t.
  • The code then creates another tuple with three elements 10, 20, and 30 and assigns it to a variable t1. This is done by using parentheses and commas to separate the elements.
  • The code then prints the length of t1 using the len() function and the print() function. The output of this line is 3, which is the number of elements in t1.
Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science and Computer Science at Teachoo