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

**Table of Contents**show

## Python program to subtract two numbers

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) << 1)**. - The
**XOR**operation is performed between a and b. - The
**~**is used to invert all the bits - The
**&**operation is used to set the bits to 1 if both bits are 1. - The
**<<**is the zero-fill left shift It shifts left by pushing zero in from the right. - The inputs are
**a = 25**and**b = 15.** - To get the output , I have used
**print(“a – b is”, subtract(a, b))**.

Example:

```
def subtract(a, b):
if (b == 0):
return a
return subtract(a ^ b, (~a & b) << 1)
a = 25
b = 15
print("a - b is", subtract(a, b))
```

We can see the substraction of two numbers as the output. You can refer to the below screenshot for the output.

This code we can use to **subtract numbers without using arithmetic operator in Python**.

## Python program to subtract two numbers binary

Now, we can** how to write a program to subtract two numbers binary** in python.

- In this example, I have taken two binary number such as
**number1 = ‘110010’**,**number2 = ‘11001’**. - To subtract the binary number the built-in function
**bin**is used. - The 2 is passed as the base value to get the binary number.
- To get the output, I have used
**print(result)**.

Example:

```
number1 = '110010'
number2 = '11001'
result = bin(int(number1,2) - int(number2,2))
print(result)
```

We can see the binary number as the output. You can refer to the below screenshot for the output.

This is the **Python program to subtract two numbers binary**.

You may like the following Python tutorials:

- How to divide two numbers in Python
- How to add two variables in Python
- How to add two numbers in Python
- Os change directory Python
- Python program to reverse a string with examples
- Python program to find sum of n numbers with examples
- Python program to print prime numbers

In this Python tutorial, we have learned about** **the** Python programs to subtract two numbers**. Also, we covered these below topics:

- How to subtract two numbers in Python
- How to subtract two numbers using a function in Python
- Python program to subtract two number user input
- How to subtract variables in python
- How to subtract two float number using a function in Python
- How to subtract the complex number in python
- Program to subtract the numbers using class
- Program to subtract the numbers without using arithmetic operator
- How to subtract two numbers binary in Python

Entrepreneur, Founder, Author, Blogger, Trainer, and more. Check out my profile.