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.
Read: Python program to print prime numbers
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
Read: Sum of even digits of a number in Python
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
Read: How to find the sum of digits of a number in Python
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
Read: Python Palindrome Program With Examples
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
Read: How to swap two numbers in Python
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
You may also like to read the following Python tutorials.
- Python program for a diamond pattern
- Python program for current date and time
- How to Print Python Fibonacci series
- Python Program to Check Leap Year
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
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.