Contents
- Introduction
- Syntax
- Example 1: Subtraction of Numbers
- Chaining of Subtraction Operator
- Example 2: Subtraction of Floating Point Numbers
- Example 3: Subtraction of Complex Numbers
- Summary
Python Subtraction – Arithmetic Operator
Python Subtraction Operator takes two operands, first one on left and second one on right, and returns difference of the the second operand from the first operand.
The symbol used for Python Subtraction operator is -
.
Syntax
Following is the syntax of Python Subtraction Arithmetic Operator with two operands.
result = operand_1 - operand_2
where operand_1
and operand_2
are numbers and the result is the difference of operand_2
from operand_1
.
Example 1: Subtraction of Numbers
In this example, we shall take two integers, subtract them, and print the result.
Python Program
a = 20
b = 12
result = a - b
print[result]
Run
Output
8
Chaining of Subtraction Operator
You can subtract more than one number from a number in a single statement. This is because, Subtraction Operator supports chaining.
Python Program
a = 63
b = 12
c = 5
d = 10
result = a - b - c - d
print[result]
Run
Output
36
Example 2: Subtraction of Floating Point Numbers
Float is one of the numeric datatypes. You can compute the difference of floating point numbers using Python Subtraction operator.
Python Program
a = 12.5
b = 10.2
result = a - b
print[result]
Run
Output
2.3000000000000007
Example 3: Subtraction of Complex Numbers
You can find the subtraction of complex numbers using -
. In the following example program, we shall take two complex numbers and find their difference.
Python Program
a = 1 + 8j
b = 3 + 5j
result = a - b
print[result]
Run
Output
[-2+3j]
Summary
In this tutorial of Python Examples, we learned what Python Subtraction Arithmetic Operator is, how to use it to find the difference of two or more numbers.
In this python tutorial, you will learn about the Python program to subtract two numbers and also we will check:
- Python program to subtract two numbers
- Python program to subtract two numbers using a function
- Python program to subtract two number user input
- How to subtract variables in python
- Program to subtract two float number using a function
- Program to subtract the complex number in python
- Program to subtract the numbers using class
- Program to subtract the numbers without using arithmetic operator
- Python program to subtract two numbers binary
Here, we can see program to subtract two numbers in python.
- In this example, I have taken two numbers as number1 = 10 and number2 = 7.
- The “-“ operator is used to subtract the two numbers.
- I have used print[number] to get the output.
Example:
number1 = 10
number2 = 7
number = number1 - number2
print[number]
The below screenshot shows the result as the output.
This is how to subtract two numbers in Python.
Read, Python Program to Check Leap Year.
Python program to subtract two numbers using a function
Here, we can see how to write a program to subtract two numbers using a function in python.
- In this example, I have defined a function as def sub[num1,num2].
- The function is returned with the expression as a return[num1-num2].
- The values to be subtracted are passed as the parameter in the function like print[sub[90,70]].
Example:
def sub[num1,num2]:
return[num1-num2]
print[sub[90,70]]
You can refer to the below screenshot for the ouptut.
This code, we can use to subtract two numbers using a function in Python.
Python program to subtract two number user input
Now, we can see how to write a program to subtract two number user input in python.
- In this example, I have used the input[] method. To take the inputs from the user.
- I have used c = a-b to subtract the numbers.
- The print[“The result is”,c] is used to get the output.
Example:
a = int[input["Enter a first number\n"]]
b = int[input["Enter a second number\n"]]
c = a-b
print["The result is",c]
The below screenshot shows the output.
How to subtract variables in python
Here, we can see how to subtract variables in python.
- In this example, I have taken two variables as variable1 and variable2.
- The variable1 is assigned as variable1 = 100, variable2 is assigned as variable2 = 65.
- To subtract the variables, I have used the “-“ operator.
- I have used print[variable] to get the output.
Example:
variable1 = 100
variable2 = 65
variable = variable1 - variable2
print[variable]
The result is shown as the output. You can refer to the below screenshot for the output.
The above code, we can use to subtract variables in Python.
Program to subtract two float number using a function
Here, we can see program to substract two float number using a function in python.
- In this example, I have used the input[] method. To take the inputs from the user.
- The float data type is used to enter the decimal numbers as the input.
- I have defined a function called subtraction as def subtraction[a,b]:
- To subtract the numbers, I have used sub=a-b.
- The function is returned as return sub.
- To get the output, I have used print[“Result is: “,subtraction[number1,number2]].
Example:
number1=float[input["Enter first number: "]]
number2=float[input["Enter second number: "]]
def subtraction[a,b]:
sub=a-b
return sub
print["Result is: ",subtraction[number1,number2]]
The below screenshot shows the substraction of two numbers as the output.
This code, we can use to subtract two float number using a function in Python.
Program to subtract the complex number in python
Here, we can see how to write a program to subtract the complex number in python.
- In this example, I have taken two complex numbers as a = complex[9, 8] and b = complex[3, 5].
- The complex[] function is used to return the complex number by specifying a real number and an imaginary number.
- The function is defined as def subComplex[ a, b].
- The function is returned as return a – b.
- I have used print[ “Result is : “, subComplex[a, b]] to get the output.
Example:
a = complex[9, 8]
b = complex[3, 5]
def subComplex[ a, b]:
return a - b
print[ "Result is : ", subComplex[a, b]]
We can see the subtraction of real and imaginary number as the output. The below screenshot shows the output.
The above code, we can use to subtract the complex number in Python.
Program to subtract numbers using class
Here, we can see how to write a program to subtract numbers using class in python.
- In this example, I have used the input[] method. to take the inputs from the user.
- I have created a class using a constructor to initialize the value of the class
- I have created a method to subtract the numbers.
- The object is created for the class to pass the parameter.
- The self is the keyword used to pass the attributes and methods to the class.
- To get the output, I have used print[“Result: “,obj.num[]].
Example:
a=int[input["Enter first number: "]]
b=int[input["Enter second number: "]]
class sub[]:
def __init__[self,a,b]:
self.a=a
self.b=b
def num[self]:
return self.a-self.b
obj=sub[a,b]
print["Result: ",obj.num[]]
The below screenshot shows the output.
This is how we can subtract numbers using class in Python.
Program to subtract the numbers without using arithmetic operator
Here, we can see how to write a program to subtract the numbers without using arithmetic operator in python.
- In this example, I have defined a function as def subtract[a, b]
- The if condition is used, If the condition is true it returns a.
- If the condition
is not true it returns return subtract[a ^ b, [~a & b]