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:
- Using “+” Operator
- Using the “operator.add()” Method
- Using the User-Defined Function
- Using the “sum()” Function
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(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.
Output
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:
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.
Output
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:
num1 = 10
num2 = 20
print(operator.add(num1,num2))
In the above code, the “operator.add()” method takes the “num1” and “num2” as an argument and returns the addition of the number.
Output
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:
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.
Output
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]
print(sum(number1))
In the above code, the “list” is initialized, and the “sum()” function is used to add all the elements of an iterable.
Output
The addition of all the iterable elements has been displayed successfully.
Conclusion
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.