Jump to content

How to Add Numbers in Python

Linux Hint

Recommended Posts

In the programming world, every new developer starts their programming journey by programming basic mathematical programs/operations, such as addition, subtraction, and multiplication. To do that, various built-in and user-defined approaches, such as the “+” operator, the “sum()” function, etc., can be used in Python.

This Python blog provides a thorough tutorial/guide on how to add Python numbers via the following methods:

Method 1: Add Number in Python Utilizing “+” Operator

The “+” operator is utilized to add two or multiple Python numbers. This can be utilized in Python to add integers, floats, or integers with float values. Let’s understand it better using the below example code:

Example 1: Add Two Numbers in Python

Let’s overview the following code:

print(25 + 15)
print(2.5 + 1.5)
print(2.5 + 15)

In the above code, the “print()” method is used along with the “+” operator to add and print two specified numbers.



The above output shows the addition of two numbers.

Example 2: Add Two User-Input Numbers

This example code is used to add numbers by taking the user input:

num1 = input("Type a number: ")
num2 = input("Type another number: ")
print(int(num1) + int(num2))

Here, the “user-input” number is taken from the “input()” function, which returns the number in string type. So, in order to add the user input, we need to convert it into an integer using the “int()” function.



The user input number has been added.

Method 2: Add Number in Python Using the “operator.add()” Method

The “operator.add()” method can also be employed to add Python numbers. Here is an example code:

import operator
num1 = 10
num2 = 20

In the above code, the “operator.add()” method takes the “num1” and “num2” as an argument and returns the addition of the number.



The two input numbers have been added successfully.

Method 3: Add Number in Python Using the User-Defined Function

The user-defined function can also be utilized to add specified Python numbers. For example, consider the following code:

def add_num(x,y):
    return x + y
print(add_num(10, 20))

In the above code, a user-defined “add_num()” function is created that accepts two parameters and retrieves the addition of two values. In this example code, the stated function accepts “10” and “20” as arguments and returns the addition of specified numbers.



The addition of two numbers has been displayed successfully.

Method 4: Adding Multiple Numbers in Python Using the “sum()” Function

The “sum()” function is utilized to sum all the iterable or sequence elements. This function is employed to add multiple Python numbers. Here is an example code:

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

In the above code, the “list” is initialized, and the “sum()” function is used to add all the elements of an iterable.



The addition of all the iterable elements has been displayed successfully.


The “+” operator, “operator.add()” method, user-defined function, and the “sum()” method is used to add multiple numbers in Python. The “+” operator is used to add two numbers or user-input numbers easily and efficiently. To add two or more than two numbers or all the elements of a sequence, the user-defined function or “sum()” method is used in Python. This guide delivered a detailed guide on how to add numbers in Python using numerous examples.

View the full article

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...