{"id":20014,"date":"2021-09-06T13:33:01","date_gmt":"2021-09-06T08:03:01","guid":{"rendered":"https:\/\/python-programs.com\/?p=20014"},"modified":"2021-11-22T18:36:29","modified_gmt":"2021-11-22T13:06:29","slug":"python-program-to-find-sum-of-tuple-items","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-tuple-items\/","title":{"rendered":"Python Program to find Sum of Tuple Items"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^2\/2…+X^N\/N<\/a><\/p>\n Given a tuple and the task is to find the sum of the given tuple elements.<\/p>\n Tuple in Python:<\/strong><\/p>\n A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed while the program is running.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the sum of given tuple elements:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more Example Python Programs<\/a> with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.<\/p>\n In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^2\/2…+X^N\/N Given a tuple and the task is to find the sum of the given tuple elements. Tuple in Python: A tuple is an immutable list of objects. That means the elements of a tuple cannot be modified or changed …<\/p>\nGiven Tuple = (12, 1, 45, 20, 10, 15, 35)<\/pre>\n
The Sum of the given tuple elements (12, 1, 45, 20, 10, 15, 35) = 138<\/pre>\n
Given Tuple = (30, 23, 65, 13, 45, 67, 89)<\/pre>\n
The Sum of the given tuple elements (30, 23, 65, 13, 45, 67, 89) = 332<\/pre>\n
Program To Find Sum of Tuple Items in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the tuple as static input and store it in a variable.\r\ngven_tupl = (12, 1, 45, 20, 10, 15, 35)\r\n# Take a variable say sum_tupl and initialize its value to 0.\r\nsum_tupl = 0\r\n# Loop in the above given tuple using the for loop.\r\nfor elemt in gven_tupl:\r\n # Add the iterator value to the above-initialized\u00a0sum_tupl and store it in the same\r\n # variable sum_tupl.\r\n sum_tupl = sum_tupl + elemt\r\n# Print sum_tupl to get the sum of given tuple elements.\r\nprint(\"The Sum of the given tuple elements\", gven_tupl, \"=\", sum_tupl)\r\n<\/pre>\n
The Sum of the given tuple elements (12, 1, 45, 20, 10, 15, 35) = 138<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the tuple as user input using tuple(),map(),input(),and split() functions\r\n# and Store it in a variable.\r\ngven_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))\r\n# Take a variable say sum_tupl and initialize its value to 0.\r\nsum_tupl = 0\r\n# Loop in the above given tuple using the for loop.\r\nfor elemt in gven_tupl:\r\n # Add the iterator value to the above-initialized\u00a0sum_tupl and store it in the same\r\n # variable sum_tupl.\r\n sum_tupl = sum_tupl + elemt\r\n# Print sum_tupl to get the sum of given tuple elements.\r\nprint(\"The Sum of the given tuple elements\", gven_tupl, \"=\", sum_tupl)\r\n<\/pre>\n
Enter some random tuple Elements separated by spaces = 25 34 89 75 16 3 2\r\nThe Sum of the given tuple elements (25, 34, 89, 75, 16, 3, 2) = 244<\/pre>\n
Method #3: Using sum() Function (Static Input)<\/h3>\n
\n
# Give the tuple as static input and store it in a variable.\r\ngven_tupl = (10, 20, 45, 63, 72, 91)\r\n# Calculate the sum of given tuple elements using the sum() function and\r\n# store it in another variable.\r\nsum_tupl = sum(gven_tupl)\r\n# Print the sum of the above-given tuple elements.\r\nprint(\"The Sum of the given tuple elements\", gven_tupl, \"=\", sum_tupl)\r\n<\/pre>\n
The Sum of the given tuple elements (10, 20, 45, 63, 72, 91) = 301<\/pre>\n
Method #4: Using sum() Function (User Input)<\/h3>\n
\n
# Give the tuple as user input using tuple(),map(),input(),and split() functions and\r\n# Store it in a variable.\r\ngven_tupl = tuple(map(int, input( 'Enter some random tuple Elements separated by spaces = ').split()))\r\n# Calculate the sum of given tuple elements using the sum() function and\r\n# store it in another variable.\r\nsum_tupl = sum(gven_tupl)\r\n# Print the sum of the above-given tuple elements.\r\nprint(\"The Sum of the given tuple elements\", gven_tupl, \"=\", sum_tupl)\r\n<\/pre>\n
Enter some random tuple Elements separated by spaces = 1 2 6 7 9 6\r\nThe Sum of the given tuple elements (1, 2, 6, 7, 9, 6) = 31<\/pre>\n
\n