Why is sum a keyword in python?

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Sum of numbers in the list is required everywhere. Python provides an inbuilt function sum() which sums up the numbers in the list. 

    Syntax:

    sum(iterable, start)  
    iterable : iterable can be anything list , tuples or dictionaries ,
     but most importantly it should be numbers.
    start : this start is added to the sum of 
    numbers in the iterable. 
    If start is not given in the syntax , it is assumed to be 0.

    Possible two syntaxes:

    sum(a)
    a is the list , it adds up all the numbers in the 
    list a and takes start to be 0, so returning 
    only the sum of the numbers in the list.
    sum(a, start)
    this returns the sum of the list + start 

    Below is the Python implementation of the sum() 

    Python3

    numbers = [1,2,3,4,5,1,4,5]

    Sum = sum(numbers)

    print(Sum)

    Sum = sum(numbers, 10)

    print(Sum)

    Output:

    25
    35

    Error and Exceptions

    TypeError : This error is raised in the case when there is anything other than numbers in the list. 

    Python3

    arr = ["a"]

    Sum = sum(arr)

    print(Sum)

    Sum = sum(arr, 10)

    print(Sum)

    Runtime Error :

    Traceback (most recent call last):
      File "/home/23f0f6c9e022aa96d6c560a7eb4cf387.py", line 6, in 
        Sum = sum(arr)
    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    Why is sum a keyword in python?
    So the list should contain numbers Practical Application: Problems where we require sum to be calculated to do further operations such as finding out the average of numbers. 

    Python3

    numbers = [1,2,3,4,5,1,4,5]

    Sum = sum(numbers)

    average= Sum/len(numbers)

    print (average)

    Output:

    3

    The Python sum() function calculates the total of all numerical values in an iterable. sum() works with both integers and floating-point numbers. The sum() function has an optional parameter to add a number to the total.


    alculating the sum of a list is a common operation in Python. For example, let’s say you are a coffee shop owner who wants to know the total value of all sales made last month. You could use the sum() function to perform this calculation.

    Why is sum a keyword in python?

    Find Your Bootcamp Match

    • Career Karma matches you with top tech bootcamps
    • Access exclusive scholarships and prep courses

    Select your interest
    First name

    Last name

    Email

    Phone number

    By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

    In Python code, the sum() function can be used to calculate the sum of all values in an iterable object. This method is useful when you need the total value of a list of items, which is common in a number of mathematical calculations.

    In this tutorial, we are going to discuss how to use the Python sum() method. We’ll go through a few examples to showcase how this method works in a real program.

    Python Sum Syntax

    The Python sum() function adds up all the numerical values in an iterable, such as a list, and returns the total of those values. sum() calculates the total of both floating-point numbers and integers.

    For instance, you could use the sum() method to calculate the total price of the products a customer purchases at a store.

    Here’s the syntax for the Python sum() method:

    sum(iterable_object, start_value)

    The function sum() takes in two parameters:

    • The iterable object that you would like to calculate the total of (required)
    • An extra number that you want to add to the value you’re calculating (optional)

    Let’s use an example to illustrate how the Python sum() function works.

    Sum Python Example

    Let’s say that we operate a local store and want to calculate the total amount to charge a customer for their shopping. We already have a list containing the prices of each individual product, but now we want to get the total value of that list.

    We could use the sum() function for this purpose. Here’s an example of the sum() function being used to calculate the total cost of a customer’s shopping:

    products_purchased = [5.40, 2.20, 9.95, 1.50, 1.50, 2.20, 4.65, 3.00, 2.00]
    
    total_price = sum(products_purchased)
    print(total_price)

    Our program returns the sum: 32.40. Let’s break down our code and discuss how it works.

    On the first line, we declare a Python variable called products_purchased which stores the prices of each product a customer has purchased.

    On the next line, we calculate the total price of the customer’s shop by using the sum() function on our products_purchased Python array. Next, our program prints out the total price of the customer’s shop that has been calculated using sum().

    Python Sum Function: Using a Tuple

    In the above example, we have used sum() to calculate the total value of several items in a list. But if our values were stored in a Python tuple, we could also have used sum(). Here’s an example of sum() being used to calculate the total value of a tuple:

    products_purchased = (5.40, 2.20, 9.95, 1.50, 1.50, 2.20, 4.65, 3.00, 2.00)
    
    total_price = sum(products_purchased)
    print(total_price)

    Our code returns: 32.40. This program is almost exactly the same as the one above. The only difference being that our products_purchased variable has been assigned a tuple rather than a list.

    sum in Python Second Argument

    The sum() function takes in an optional second parameter which adds a number to the final total calculated by the method.

    Let’s say that a customer has decided to purchase a bag after their groceries have been scanned. Each bag costs $1. To add the $1 bag to the customer’s total, you could specify a second parameter in the sum() method. 

    Here’s the code that we could use to calculate the price of a customer’s purchase, in addition to the $1 bag they have bought:

    products_purchased = [2.00, 3.00, 4.00]
    
    total_price = sum(products_purchased, 1.00)
    print(total_price)

    Our program returns: 10.00. When our final value 1 is added to the sum() method, it is added and so our program returns 10.00.

    Conclusion

    The sum() method can be used in Python to calculate the total of the items stored in an iterable object. For example, sum() could be used to calculate the cost of your coffee shop order.

    In this tutorial, we explored how to use the sum() function in Python. Then, we discussed how to use the second parameter offered by sum() to add more values to the total calculation. Now you’re equipped with the knowledge you need to use the sum() method in Python like a pro!

    To learn more about coding in Python, read our How to Learn Python guide.

    Is sum is a keyword in Python?

    sum() function in Python Python provides an inbuilt function sum() which sums up the numbers in the list. Syntax: sum(iterable, start) iterable : iterable can be anything list , tuples or dictionaries , but most importantly it should be numbers. start : this start is added to the sum of numbers in the iterable.

    What does sum in Python mean?

    Definition and Usage The sum() function returns a number, the sum of all items in an iterable.

    Why do we use sum in Python?

    The Python sum() function adds up all the numerical values in an iterable, such as a list, and returns the total of those values. sum() calculates the total of both floating-point numbers and integers.

    Can I use sum as a variable name in Python?

    Sum is considered a Python keyword and should not be used as a variable name.