Add Two Numbers with User Input
Another way to add two numbers in Python is by taking input from the user. This allows the user to provide the numbers they want to add at runtime. Let’s see how you can add two numbers using user input:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
result = num1 + num2
print("The sum of", num1, "&", num2, "is", result)

You can also try this code with Online Python Compiler
Run Code
In this code, we use the `input()` function to prompt the user to enter two numbers. The `input()` function returns the user's input as a string, so we use the `int()` function to convert the input to integers. We store the user's input in the variables `num1` & `num2`.
Next, we add `num1` & `num2` using the "+" operator & store the result in the `result` variable. Finally, we print a message displaying the sum of the two numbers.
When you run this code, it will prompt the user to enter the first and second numbers. After the user provides the input, it will calculate the sum and display the result.
For example, if the user enters 10 as the first number & 5 as the second number, the output will be:
Enter the first number: 10
Enter the second number: 5
The sum of 10 & 5 is 15
Add Two Numbers in Python Using Function:
Functions in Python allow you to encapsulate a block of code that performs a specific task. You can create a function to add two numbers & reuse it whenever needed.
For example:
def add_numbers(a, b):
sum = a + b
return sum
num1 = 10
num2 = 20
result = add_numbers(num1, num2)
print("The sum of", num1, "&", num2, "is", result)

You can also try this code with Online Python Compiler
Run Code
Output
The sum of 10 & 20 is 30
In this code, we define a function called `add_numbers()` that takes two parameters, `a` and `b`. Inside the function, we add the values of `a` and `b` using the "+" operator and store the result in the `sum` variable. Finally, we use the `return` statement to return the value of `sum`.
Outside the function, we have two variables, `num1` and `num2`, which hold the values 10 and 20, respectively. We call the add_numbers () function and pass num1 and num2 as arguments. The function returns the sum of `num1` and `num2`, which we store in the `result` variable.
Finally, we print a message displaying the sum of the two numbers.
Note: The function to add two numbers makes the code more modular and reusable. Whenever you need to add two numbers, you can call the `add_numbers()` function with different arguments.
Add Two Numbers Using operator.add() Method
Python provides the `operator` module, which contains various functions for performing mathematical operations. One of these functions is `operator.add()`, which can be used to add two numbers. For example:
import operator
num1 = 15
num2 = 25
result = operator.add(num1, num2)
print("The sum of", num1, "&", num2, "is", result)

You can also try this code with Online Python Compiler
Run Code
In this code, we first import the `operator` module. Then, we have two variables, `num1` and `num2`, which hold the values 15 and 25, respectively.
To add `num1` and `num2`, we use the `operator.add()` function. We pass `num1` and `num2` as arguments to the `operator.add()` function, which returns their sum. We store the result in the `result` variable.
Finally, we print a message displaying the sum of the two numbers.
Note: The `operator.add()` function provides a more readable & expressive way to add two numbers in Python. It can be useful when you need to perform addition based on a dynamic operation or when you want to make the code more explicit.
Add Two Number Using Lambda Function
Lambda functions, also known as anonymous functions, are small & inline functions in Python. They can be used to perform simple operations like adding two numbers. Let’s look at an example of using a lambda function to add two numbers:
add_numbers = lambda x, y: x + y
num1 = 30
num2 = 40
result = add_numbers(num1, num2)
print("The sum of", num1, "&", num2, "is", result)

You can also try this code with Online Python Compiler
Run Code
Output
The sum of 15 & 25 is 40
In this code, we define a lambda function called `add_numbers`. The lambda function takes two parameters, `x` and `y`, and returns their sum using the "+" operator.
We have two variables, `num1` and `num2`, which hold the values 30 and 40, respectively.
To add `num1` & `num2`, we call the `add_numbers` lambda function & pass `num1` & `num2` as arguments. The lambda function returns the sum of `num1` & `num2`, which we store in the `result` variable.
Finally, we print a message displaying the sum of the two numbers.
Note: Lambda functions provide a concise way to define small, one-line functions. They are very useful when you need to perform a simple operation like addition without defining a separate named function.
Add Two Numbers Using the Recursive Function
Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. You can use recursion to add two numbers by breaking down the problem into smaller subproblems. Let’s take an example of adding two numbers using a recursive function:
def add_numbers(x, y):
if y == 0:
return x
else:
return add_numbers(x + 1, y - 1)
num1 = 50
num2 = 60
result = add_numbers(num1, num2)
print("The sum of", num1, "&", num2, "is", result)

You can also try this code with Online Python Compiler
Run Code
Output
The sum of 30 & 40 is 70
In this code, we define a recursive function called `add_numbers`. The function takes two parameters, `x` and `y`.
The base case of the recursion is when `y` becomes 0. In this case, we simply return the value of `x`, which represents the sum of the two numbers.
If `y` is not 0, we recursively call the `add_numbers` function with `x + 1` & `y - 1`. This means we increment `x` by 1 & decrement `y` by 1 in each recursive call. We keep doing this until `y` becomes 0.
We have two variables, `num1` and `num2`, which hold the values 50 and 60, respectively.
To add `num1` & `num2`, we call the `add_numbers` function with `num1` & `num2` as arguments. The function recursively computes the sum of the two numbers & returns the result, which we store in the `result` variable.
Finally, we print a message displaying the sum of the two numbers.
Note: The recursion technique of adding two numbers is a more advanced method and may not be the most efficient for simple addition. However, it shows the concept of recursion and how it can be used to solve problems by breaking them down into smaller subproblems.
Frequently Asked Questions
Can we add more than two numbers using the same methods?
Yes, you can extend the methods to add multiple numbers by modifying the code accordingly.
Is it possible to add floating-point numbers using these methods?
Yes, these methods can be used to add floating-point numbers as well. Just use float() instead of int() when necessary.
Which method is the most efficient for adding two numbers in Python?
The most efficient method is using the "+" operator directly, as it has the least overhead compared to other methods.
Conclusion
In this article, we discussed different methods to add two numbers in Python. We learned how to use the "+" operator, take user input, define functions, and use the operator.add() method, work with lambda functions, & implement recursive functions. These methods provide different ways to perform addition based on your specific needs & preferences.