In this python tuple program, we will calculate the average of the elements in the tuple.
Steps to solve the program
- Take a tuple as input and create a variable add and set its value equal to 0.
- Use a for loop to iterate over numbers in the tuple.
- Add the numbers to the add variable.
- Find the average of the elements in the tuple by dividing the value of add variable by the length of the tuple i.e number of elements in the tuple.
- Print the output.
tup = (5,3,9,6)
print("Original tuple: ",tup)
add = 0
for ele in tup:
add += ele
print("Average of elements in the tuple: ",add/len(tup))
Output :
Original tuple: (5, 3, 9, 6)
Average of elements in the tuple: 5.75