How to add numbers in Python – with example

  • Reading time:16 mins read

How to add numbers in Python?

Python is capable of performing both complex and basic mathematical operations using the arithmetic operators or methods and constants available through the math library. In this article, we will explore how we can use the addition operator and how to add numbers in Python.

 

How to add numbers in Python by simple arithmetic operators

The addition operator is classified as an arithmetic operator among other operators such as the division operator and subtraction operator. In its simplest form, the addition operator returns the sum of two numbers written side by side with the addition sign in between them.

However, in order to display the results, we can simply create a variable that stores the sum of the two numbers. Using the print function we can then display the result as shown in the snippet below.

 

 

 

The addition operator can also be used with floating-point numbers. In a similar manner, the addition operator returns the sum of two floating-point numbers written side by side.

 

 

 

Alternatively, we can also create a more elegant version of the above code snippets by creating variables that will hold the values whose sum we want to calculate as shown in the code below. 

 

 

 

In more realistic applications or programs, arithmetic operators are not used in isolation. In most cases, they may be used as part of functions or statements that form part of larger programs.

 

How to add numbers in Python

 

In scenarios where the addition operator may be needed several times within the same program the best approach would be to create a function that can be reused multiple times.

 

 

 

In the code above we have created a function named add_these() which takes in two arguments and returns the sum of the two arguments. We have then proceeded to call the function along with two arguments and the result is stored in a variable named total.

Finally using the print function we have printed out the total, which is displayed in the terminal. Now, this is much better because we can now just use this function over and over whenever we want to get the sum of two numbers as opposed to actually having to type the number plus another each time.

 

How to add numbers in Python

 

Sum of numbers in an Iterable – How to add numbers in Python with sum() function

Suppose we want to get the sum of multiple numbers that are stored together in an iterable such as a list. Programs such as Excel make it very simple to find the sum of a sequence of numbers by simply hovering over the numbers.

In Python the use of a for loop to iterate over iterable is often the most used approach, however, Python provides a built-in function that returns the sum of all numbers in a list. In this case, we also need to create a variable to store the sum of the numbers.

 

 

 

This function is precise and all that we need to do is create a variable that will store the sum and then pass in the list of numbers to the function.  The function may take in two arguments.

The first argument is the name of the list whose sum we want to be calculated, the second argument is the start number. The start number is the number that is added to the sum of the items in the iterable.

 

 

sum(iterable,start)

 

Using a start value of 100 we can find the sum of numbers in a list as shown in the code below. 

 

 

The start number, in this case, has been added to the initial result which was 933 to make the new result 1033.

Summary

This is how to add numbers in Python. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.