In this python tuple program, we will multiply adjacent elements of a tuple.
Steps to solve the program
- Create a tuple and an empty list.
- Create a tuple having adjacent elements as a tuple inside of a tuple using zip(tup,tup[1:]).
- Use for loop to iterate over this tuple.
- Now get the product of the adjacent element and add it to the empty list.
- Convert the list back to the tuple using tuple().
- Print the output.
tup = (1,2,3,4)
list1 = []
for a,b in zip(tup,tup[1:]):
c = a*b
list1.append(c)
tup = tuple(list1)
print("Multiplying adjacent elements: ",tup)
Output :
Multiplying adjacent elements: (2, 6, 12)
create a tuple having squares of the elements from the list.
1 thought on “Python tuple program to multiply adjacent elements of a tuple”