What is the difference between the outputs of the code#1 and code#2? Give reasons.

code#1
t1=(1,2,3)
t2=(4,5,6) t3= t1+t2
print( “New tuple is :”=str(t3))

code #2
t1=(1,2,3),
t2=(4,5,6),
t3=t1+t2
print(“New tuple is :”+str(t3))


Answer:

Output of code #1:

New tuple is: (1,2,3,4,5,6)  

Output of code #2:

New tuple is ((1,2,3),(4,5,6)) 

The difference between the outputs of the code #1 and code #2 is that code #1 will print a tuple with six elements, while code #2 will print a tuple with two elements. The reason for this difference is that code #1 uses parentheses without commas to create tuples , while code #2 uses commas with or without parentheses to create tuples.

 

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