- Python Program to Add Two Numbers
- Example 1: Add Two Numbers
- Example 2: Add Two Numbers With User Input
- How to add two numbers in Python [6 different methods]
- How to add two numbers in Python
- Method-1: How to add two numbers in Python using the arithmetic operator ‘+’
- Method-2: How to add two numbers in Python using ‘+=’
- Method-3: How to add two numbers in Python using the function reduce() and operator.add
- Method-4: How to add two numbers in Python using the add function
- Method-5: How to add two numbers in Python using the user input
- Method-6: How to add two numbers in Python using the sum() method
- Addition of two numbers in Python using a function
- Sum of two numbers in Python using Lambda Functions
Python Program to Add Two Numbers
To understand this example, you should have the knowledge of the following Python programming topics:
In the program below, we’ve used the + operator to add two numbers.
Example 1: Add Two Numbers
# This program adds two numbers num1 = 1.5 num2 = 6.3 # Add two numbers sum = num1 + num2 # Display the sum print('The sum of and is '.format(num1, num2, sum))
The sum of 1.5 and 6.3 is 7.8
The program below calculates the sum of two numbers entered by the user..
Example 2: Add Two Numbers With User Input
# Store input numbers num1 = input('Enter first number: ') num2 = input('Enter second number: ') # Add two numbers sum = float(num1) + float(num2) # Display the sum print('The sum of and is '.format(num1, num2, sum))
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8
In this program, we asked the user to enter two numbers and this program displays the sum of two numbers entered by user.
We use the built-in function input() to take the input. Since, input() returns a string, we convert the string into number using the float() function. Then, the numbers are added.
Alternative to this, we can perform this addition in a single statement without using any variables as follows.
print('The sum is %.1f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))
Enter first number: 1.5 Enter second number: 6.3 The sum of 1.5 and 6.3 is 7.8
Although this program uses no variable (memory efficient), it is harder to read.
How to add two numbers in Python [6 different methods]
In this python tutorial, you will learn about Python programs to add two numbers.
There are six methods to add two numbers in Python, which are shown below.
- Using the arithmetic operator ‘+’
- Using ‘+=’
- Using the function reduce() and operator.add
- Using the add function
- Using the user input
- Using the sum() method
How to add two numbers in Python
Here we will discuss all 6 methods to add two numbers in Python/ Let us first start with the arithmetic operator.
Method-1: How to add two numbers in Python using the arithmetic operator ‘+’
This is the most basic and straightforward method to add two numbers in Python. Simply use the ‘+’ operator between the two numbers you want to add, and Python will return the sum.
# Calculating the sum of two numbers num1 = 5 # First number num2 = 10 # Second number # Adding the two numbers sum = num1 + num2 # Printing the result print("The sum of", num1, "and", num2, "is", sum)
The above code calculates the sum of two numbers, num1, and num2, and stores the result in a variable named sum. The result is then printed to the console with a message. num1 is assigned the value of 5, and num2 is assigned the value of 10.
Method-2: How to add two numbers in Python using ‘+=’
The ‘+=’ operator is a shorthand operator in Python that can be used to add a value to a variable. This operator adds the value on the right to the variable on the left, and the result is stored in the left variable.
# Addition of two variables num1 = 5 num2 = 10 # Perform addition using '+=' operator num1 += num2 # Print the result print("The sum of", num1, "and", num2, "is", num1)
The above code adds two numbers num1 and num2 using the “+=” operator. The result of the addition is then stored in the num1 variable, which is then printed as the “The sum of 5 and 10 is 15”.
Output: The sum of 15 and 10 is 15
Method-3: How to add two numbers in Python using the function reduce() and operator.add
The reduce() function is a built-in function in Python that can be used to apply a given function to all elements of a list. To add two numbers, you can use the reduce() function along with the operator.add function.
# Importing the operator module and the reduce function from functools library import operator from functools import reduce # Initializing two numbers num1 = 5 num2 = 10 # Calculating the sum of the two numbers using reduce() and operator.add sum = reduce(operator.add, [num1, num2]) # Printing the result print("The sum of", num1, "and", num2, "is", sum)
The above code imports the operator module and the reduce function from the functools module.
- It defines two variables num1 and num2 with values 5 and 10 respectively.
- Then it calculates the sum of the two variables using the reduce function with operator.add as the operator to apply to the elements of the list [num1, num2].
- The result is printed with a message that states “The sum of 5 and 10 is 15”.
Output: The sum of 5 and 10 is 15
Method-4: How to add two numbers in Python using the add function
# Define a function that takes in two parameters, num1 and num2 def add(num1, num2): # Return the result of adding the two numbers return num1 + num2 # Store the result of calling the add function with arguments 5 and 10 sum = add(5, 10) # Print the sum of 5 and 10 print("The sum of 5 and 10 is", sum)
The above code defines a function add() that takes two arguments, num1 and num2.
- The function returns the sum of the two arguments by using the “+” operator.
- Then the function is called with arguments 5 and 10 and the result is stored in the sum variable. Finally, the sum is printed.
Output: The sum of 5 and 10 is 15
Method-5: How to add two numbers in Python using the user input
To add two numbers provided by the user, you can use the input() function to get the input from the user and then use any of the methods mentioned above to add the two numbers.
# This code takes two input numbers from the user # And calculates their sum and prints the result num1 = int(input("Enter the first number: ")) num2 = int(input("Enter the second number: ")) # Sum of the two input numbers sum = num1 + num2 # Output the result print("The sum of", num1, "and", num2, "is", sum)
The above code takes two integer inputs from the user and stores them as num1 and num2.
- Then, it calculates the sum of these two numbers and stores it in the variable sum. Finally, it prints the result of the sum by including the values of num1, num2, and sum in the output string.
Output: Enter the first number: 3 Enter the second number: 8 The sum of 3 and 8 is 11
Method-6: How to add two numbers in Python using the sum() method
The sum() function is a built-in function in Python that can be used to add numbers. You can pass a list of numbers to this function, and it will return the sum of all numbers in the list.
# Program to calculate the sum of two numbers # using the sum() function # Define the two numbers num1 = 5 num2 = 10 # Calculate the sum of the two numbers # by passing them as a list to the sum() function sum = sum([num1, num2]) # Print the sum of the two numbers print("The sum of", num1, "and", num2, "is", sum)
The above code calculates the sum of two numbers num1 and num2. The numbers are assigned the values of 5 and 10, respectively.
- The sum function from the builtins of the module is then used to find the sum of the two numbers and store it in the sum variable.
Output: The sum of 5 and 10 is 15
Addition of two numbers in Python using a function
Python also allows you to define your own functions. We can create a function to add two numbers:
def add_two_numbers(num1, num2): return num1 + num2 print('The sum is', add_two_numbers(3, 5))
In this script, we first defined a function add_two_numbers which takes two arguments and returns their sum. We then called this function with the numbers 3 and 5, and printed the result.
When you run the above Python addition code, you can see the output like below:
Sum of two numbers in Python using Lambda Functions
Lambda functions are a way of creating small, anonymous functions in Python. They can be used in places where you need a function, but don’t want to bother defining it with a full def statement.
Here’s how to add two numbers using a lambda function in Python:
# define the lambda function add_two_numbers = lambda num1, num2: num1 + num2 # use the lambda function print('The sum is', add_two_numbers(3, 5))
You may also like to read the following Python tutorials.
In this Python tutorial, we have learned How to add two numbers in Python by using the below methods:
- Using the arithmetic operator ‘+’
- Using ‘+=’
- Using the function reduce() and operator.add
- Using the add function
- Using the user input
- Using the sum() method
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.