How to Add Two Numbers in Python – Easy Programs
Last Updated :
02 Jul, 2024
Learn easy techniques and codes to add numbers in Python. Adding numbers in Python is a very easy task, and we have provided you 7 different ways to add numbers in Python.
Given two numbers num1 and num2. The task is to write a Python program to find the addition of these two numbers.
Examples:
Input: num1 = 5, num2 = 3
Output: 8
Input: num1 = 13, num2 = 6
Output: 19
There are many methods to add two number in Python:
- Using “+” operator
- Defining a function that adds two number
- Using operator.add method
- Using lambda function
- Using Recursive function
Each of these methods have been explained with their program below.
Add Two Numbers with “+” Operator
Here num1 and num2 are variables and we are going to add both variables with the + operator in Python.
Python
# Python3 program to add two numbers
num1 = 15
num2 = 12
# Adding two nos
sum = num1 + num2
# printing values
print("Sum of", num1, "and", num2 , "is", sum)
Output:
Sum of 15 and 12 is 27
We will now see Python programs to add two numbers without using + operator.
Add Two Numbers with User Input
In the below program to add two numbers in Python, the user is first asked to enter two numbers, and the input is scanned using the Python input() function and stored in the variables number1 and number2.
Then, the variable’s number1 and number2 are added using the arithmetic operator +, and the result is stored in the variable sum.
Python
# Python3 program to add two numbers
number1 = input("First number: ")
number2 = input("\nSecond number: ")
# Adding two numbers
# User might also enter float numbers
sum = float(number1) + float(number2)
# Display the sum
# will print value in float
print("The sum of {0} and {1} is {2}" .format(number1,
number2, sum))
Output:
First number: 13.5 Second number: 1.54
The sum of 13.5 and 1.54 is 15.04
Add Two Numbers in Python Using Function
This program show adding two numbers in Python using function. We can define a function that accepts two integers and returns their sum.
Python
#To define a function that take two integers
# and return the sum of those two numbers
def add(a,b):
return a+b
#initializing the variables
num1 = 10
num2 = 5
#function calling and store the result into sum_of_twonumbers
sum_of_twonumbers = add(num1,num2)
#To print the result
print("Sum of {0} and {1} is {2};" .format(num1,
num2, sum_of_twonumbers))
Output
Sum of 10 and 5 is 15;
Add Two Numbers Using operator.add() Method
Initialize two variables num1, and num2. Find sum using the operator.add() by passing num1, and num2 as arguments and assign to su. Display num1,num2 and su
Python
# Python3 program to add two numbers
num1 = 15
num2 = 12
# Adding two nos
import operator
su = operator.add(num1,num2)
# printing values
print("Sum of {0} and {1} is {2}" .format(num1,
num2, su))
Output
Sum of 15 and 12 is 27
Read More: Operator Functions
Add Two Number Using Lambda Function
Here we are going to use Python Lambda function for addition of two number with Python
Python
# Define a lambda function to add two numbers
add_numbers = lambda x, y: x + y
# Take input from the user
num1 = 1
num2 = 2
# Call the lambda function to add the two numbers
result = add_numbers(num1, num2)
# Print the result
print("The sum of", num1, "and", num2, "is", result)
Output:
The sum of 1 and 2 is 3
Add Two Numbers Using Recursive Function
This code defines a recursive function to add two numbers by incrementing one number and decrementing the other. User inputs two numbers, and the sum is calculated and displayed.
Python
# Define a recursive function to add two numbers
def add_numbers_recursive(x, y):
if y == 0:
return x
else:
return add_numbers_recursive(x + 1, y - 1)
# Take input from the user
num1 = 1
num2 = 2
# Call the recursive function to add the two numbers
result = add_numbers_recursive(num1, num2)
# Print the result
print("The sum of", num1, "and", num2, "is", result)
Output:
The sum of 1 and 2 is 3
We have covered multiple programs to add two number in Python. You can use any one of the above method, to perform the task. Adding two numbers is a very easy task in Python, can be done using multiple functionalities.
This tutorial showed you the basic of addition of two numbers in Python. You can be creative and customize these programs to add multiple numbers in Python.
You can also check other operations:
How to Add Two Numbers in Python – FAQs
Can we add integers and floating-point numbers together in Python?
Yes, Python allows you to add integers and floating-point numbers together seamlessly. When you perform arithmetic operations between integers and floats, Python automatically handles the conversion and performs the addition.
# Example:
a = 5 # Integer
b = 2.5 # Floating-point number
result = a + b
print(result) # Output: 7.5
Can we use bitwise operators to add numbers in Python?
Bitwise operators (&, |, ^, <<, >>, ~) perform operations at the bit level and are typically used for manipulating binary data rather than adding numbers in the conventional sense. While some bitwise operations might seem like addition in specific cases (such as | for bitwise OR), they are not used for general arithmetic addition.
# Example:
a = 5 # 101 in binary
b = 3 # 011 in binary
result = a | b # Bitwise OR operation
print(result) # Output: 7 (which is 111 in binary)
How to sum two numbers in a class in Python?
To sum two numbers within a class in Python, you can define a method that performs the addition.
class Calculator:
def add(self, num1, num2):
return num1 + num2
# Example usage:
calc = Calculator()
result = calc.add(10, 20)
print(result) # Output: 30
Can we add 2 lists together in Python?
Yes, you can concatenate (combine) two lists together using the + operator in Python.
# Example:
list1 = [1, 2, 3]
list2 = [4, 5, 6]
combined_list = list1 + list2
print(combined_list) # Output: [1, 2, 3, 4, 5, 6]
How to add numbers from different data types in Python?
Python supports automatic type conversion during arithmetic operations. When adding numbers of different types (like integers, floats, or even other types), Python will convert them to a compatible type if necessary.
# Example:
int_num = 10
float_num = 2.5
complex_num = 1 + 3j
result = int_num + float_num
print(result) # Output: 12.5 (integer + float)
result = float_num + complex_num
print(result) # Output: (3.5+3j) (float + complex)