In this article, we will learn How to add two numbers in Python with easy techniques and codes. Adding two numbers in Python is a very easy task because there are multiple ways to do this addition. We have provided 5+ easy and unique ways to add numbers in Python, let’s discuss each option in detail with a program code example.
We will consider there are two numbers num1 and num2, now the task is to write a Python program to add these numbers and provide results.
Examples:
Input: num1 = 10, num2 = 5
Output: 15
Input: num1 = 13, num2 = 7
Output: 20
1. Add Two Numbers in Python with the “+” Operator
In below code example, we have two variables num1 and num2 and will add both variables with the + operator in Python.
# Adding two numbers
num1 = 10
num2 = 20
result = num1 + num2
print("The sum is:", result)
Output:
The sum is: 30
2. Add Two Numbers in Python with User Input
In below code example, first user asked to enter two numbers, and then input will be captured by Python input function and will be converted into an integer by Python int function and stored in the variables num1 and num2.
Then, the variables num1 and num2 will be added with + operator and stored in the variable result.
# Adding numbers with user input
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
result = num1 + num2
print("The sum is:", result)
Output:
Enter first number: 20
Enter second number: 15
The sum is: 35
3. Add Two Numbers in Python Using a Function
In below code example, we will define a function that accepts two integer numbers and returns the sum of both integer numbers.
# Function to add two numbers
def add_numbers(a, b):
return a + b
result = add_numbers(7, 8)
print("The sum is:", result)
Output:
The sum is: 15
4. Add Two Numbers in Python Using Recursive Function
In below code example, we will define a recursive function that will call itself to sum the two integer values. The base condition b == 0 stops the recursion when the second number becomes zero, and in each recursive call, a is incremented by 1, and b is decremented by 1.
# Recursive function to add two numbers
def add_recursive(a, b):
if b == 0:
return a
return add_recursive(a + 1, b - 1)
result = add_recursive(4, 5)
print("The sum is:", result)
Output:
The sum is: 9
5. Add Two Numbers Using operator.add() Method
In below code example, we will use Python function operator.add(), we will pass two parameters as arguments and return the value assigned to result variable and display the result.
result = operator.add(10, 15)
print("The sum is:", result)
Output:
The sum is: 25
6. Add Two Numbers Using Lambda Function
In below code example, we will Python Lambda function to add two integer values. A lambda function provides a concise way to add numbers.
add = lambda x, y: x + y
result = add(12, 8)
print("The sum is:", result)
Output:
The sum is: 20
7. Add Numbers in a List Using sum()
If you are using list and want to add all elements of list, below code example is best for you. You can simply use Python sum() function which will sum all element’s values and return output.
numbers = [3, 6, 9, 12]
result = sum(numbers)
print("The sum is:", result)
Output:
The sum is: 30
Error Handling While Adding Numbers
You should implement exception handling to prevent invalid input to make code more robust.
Example:
try:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = num1 + num2
print("The sum is:", result)
except ValueError:
print("Invalid input! Please enter numeric values.")
Output:
Enter the first number: a
Invalid input! Please enter numeric values.
FAQ Section
Q1: What is the simplest way to add two numbers in Python?
The simplest way is to use the + operator. Example: result = num1 + num2.
Q2: How can I handle invalid input while adding numbers?
Use a try-except block to catch errors like non-numeric inputs.
Q3: When should I use recursion for addition?
Recursion can be used for educational purposes or when solving problems that require breaking them into smaller subproblems.
Q4: What is the difference between using + and operator.add()?
Both perform addition, but operator.add() is part of the operator module, useful for functional programming.
Q5: Can I add more than two numbers in Python?
Yes, you can use the sum() function to add multiple numbers in a list or iterable.
Conclusion
This guide covered various ways to add two numbers in Python, from the simplest + operator to recursion and error handling. These methods cater to different scenarios, ensuring flexibility in your coding journey. Explore more Python tutorials on TechieTrail and enhance your programming skills!