Python Program to Print Sum of all Elements in Tuple

Python Program to Print Sum of all Elements in Tuple 


tuple3=(1,2,3,4,5,6,7,'hello')

sum=0

print("The tuple elements are:")

for i in tuple3:

    if isinstance(i,int):

            print(i)

            sum=sum+i

print("The sum is :",sum)


Output:

The tuple elements are:

1

2

3

4

5

6

7

The sum is : 28


Python Program to Print Sum of all Elements in Tuple  by taking user input for Tuple


my_tuple=tuple(map(int,input("Enter tuple data items:").split()))
sum=0
for i in my_tuple:
    if isinstance(i,int):
            print(i)
            sum=sum+i
print("The sum of tuple elements is:",sum)


Output:
Enter tuple data items:1 2 3 4 5
1
2
3
4
5
The sum of tuple elements is: 15



टिप्पणी पोस्ट करा

0 टिप्पण्या