{"id":24847,"date":"2021-11-02T09:54:35","date_gmt":"2021-11-02T04:24:35","guid":{"rendered":"https:\/\/python-programs.com\/?p=24847"},"modified":"2021-11-02T09:54:35","modified_gmt":"2021-11-02T04:24:35","slug":"python-sum-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-sum-function-with-examples\/","title":{"rendered":"Python sum() Function with Examples"},"content":{"rendered":"
sum() Function in Python:<\/strong><\/p>\n The sum() function returns a number that represents the sum of all items in an iterable.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n iterable<\/strong>: This is required. It is the sequence\u00a0 to be sumed.<\/p>\n start<\/strong>: This is optional. It is a value added to the return value. The value of start is 0 by default (if omitted)<\/p>\n Return Value:<\/strong><\/p>\n sum() returns the sum of the iterable’s start and items.<\/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 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 sum() Function in Python: The sum() function returns a number that represents the sum of all items in an iterable. Syntax: sum(iterable, start) Parameters iterable: This is required. It is the sequence\u00a0 to be sumed. start: This is optional. It is a value added to the return value. The value of start is 0 by …<\/p>\nsum(iterable, start)<\/pre>\n
Given List = [1, 2, 6, 15, 5]<\/pre>\n
The sum of all the items in a given list = 29<\/pre>\n
Given List = [1, 2, 3, 5, 10]\r\nstart value = 4<\/pre>\n
The sum of all the items in a given list with given start value = 25<\/pre>\n
sum() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
1)Without start value<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 2, 6, 15, 5]\r\n# Pass the given list as an argument to the sum() function to get the sum of all\r\n# the items in a given list\r\n# Store it in another variable.\r\nsumm_lst = sum(gvn_lst)\r\n# Print the sum of all the items in a given list.\r\nprint(\"The sum of all the items in a given list =\", summ_lst)\r\n<\/pre>\n
The sum of all the items in a given list = 29<\/pre>\n
2)With start value<\/h5>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [1, 2, 3, 5, 10]\r\n# Give the start value as static input and store it in a variable.\r\ngvn_strt = 4\r\n# Pass the given list, start value as the arguments to the sum() function to\r\n# get the sum of all the items in a given list.\r\n# Begin with the given start value and add all of the items in a list to it.\r\n# Store it in another variable.\r\nsumm_lst = sum(gvn_lst, gvn_strt)\r\n# Print the sum of all the items in a given list with the given start value.\r\nprint(\"The sum of all the items in a given list with given start value = \", summ_lst)\r\n<\/pre>\n
The sum of all the items in a given list with given start value = 25<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
1)Without start value<\/h5>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given list as an argument to the sum() function to get the sum of all\r\n# the items in a given list\r\n# Store it in another variable.\r\nsumm_lst = sum(gvn_lst)\r\n# Print the sum of all the items in a given list.\r\nprint(\"The sum of all the items in a given list =\", summ_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30 40 10\r\nThe sum of all the items in a given list = 110<\/pre>\n
2)With start value<\/h5>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the start value as user input using the int(input()) function\r\n# and store it in a variable.\r\ngvn_strt = int(input(\"Enter some random number = \"))\r\n# Pass the given list, start value as the arguments to the sum() function to\r\n# get the sum of all the items in a given list.\r\n# Begin with the given start value and add all of the items in a list to it.\r\n# Store it in another variable.\r\nsumm_lst = sum(gvn_lst, gvn_strt)\r\n# Print the sum of all the items in a given list with the given start value.\r\nprint(\"The sum of all the items in a given list with given start value = \", summ_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30 40 10\r\nEnter some random number = 20\r\nThe sum of all the items in a given list with given start value = 130<\/pre>\n","protected":false},"excerpt":{"rendered":"