Question: Assume tpl to be a tuple containing elements of different types ( int , string, float and bool ) . Write some code that counts

Assume tpl to be a tuple containing elements of different types (int, string, float and bool). Write some code that counts how many elements have the same type as the first element. Store the result in a variable called count. Do not use a for loop.
Hint: You can use the type function to get the type of a value. The type function was introduced in Chapter 2.
I did this and it doesn't work:
tpl =(1,'a', True)
first_element = tpl[0]
count =0
for index in range(1, len(tpl)):
if type(tpl[index])== type(first_element):
count +=1
print(count)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!