Program to find all the tuples that are divisible by a number

In this python tuple program, we will find all the tuples that are divisible by a number.

Steps to solve the program
  1. Take a list of tuples as input.
  2. Use a for loop to iterate over tuples from the list.
  3. Create a count variable and assign its value equal to 0.
  4. Use a nested for loop to iterate over elements in the tuple.
  5. If an element is divisible by 5 then add 1 to the count variable.
  6. In the end, if the value of the count variable is equal to the length of the tuple i.e. total elements in the tuples, then that entire tuple is divisible by 5.
  7. Print the output.
				
					l = [(10,5,15),(25,6,35),(20,10)]
print("Original list of tuples: ",l)
for tup in l:
    count = 0
    for ele in tup:
        if ele%5 == 0:
            count += 1
    if count == len(tup):
        print("\nTuple in which all elements are divisible by 5: \n",tup)
				
			

Output :

				
					Original list of tuples:  [(10, 5, 15), (25, 6, 35), (20, 10)]

Tuple in which all elements are divisible by 5: 
 (10, 5, 15)

Tuple in which all elements are divisible by 5: 
 (20, 10)
				
			

convert a given list of tuples to a list of lists.

find tuples having negative elements.

Leave a Comment