In python, you can add, subtract, multiply and divide two numbers using the arithmetic operator +
, -
, *
, and /
respectively. Every operator takes two operands and returns the result of the operation.
The number in python could be of any data type like int, float, or complex. To find the result of the different operations performed, you can take the same data type or combination of any of the supported numeric data types: int, float, or complex.
The example below is to show how the addition of two numbers is done using Python:
# The program to add two numbers
num1 = 7
num2 = 13
result = num1 + num2
print(f"The sum of {num1} and {num2} is {result}.")
Here, the f-string is a string literal that is used as an f at the beginning and curly braces containing expression that will be replaced with its value.
Output:
The sum of 7 and 13 is 20.
The following program is to perform a subtraction between two numbers in Python:
num1 = 7
num2 = 13
result = num2 - num1
print(f"The subtraction between {num1} and {num2} is {result}.")
Output:
The subtraction between 7 and 13 is 6.
This program is to perform a multiplication operation between two numbers in Python:
num1 = 7
num2 = 13
result = num2 * num1
print(f"The multiplication of {num1} and {num2} is {result}.")
Output:
The multiplication of 7 and 13 is 91.
The division operation between two numbers in Python:
num1 = 7
num2 = 14
result = num2 / num1
print(f"The division between {num1} and {num2} is {result}.")
Output:
The division between 7 and 14 is 2.0.
In this, we demonstrated different examples in Python to perform arithmetic operations like addition, subtraction, multiplication, and division with two numbers.