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.
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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
