Here are three ways to subtract two numbers in Python:

- Using substraction operator(-)
- Using a Function
- Using Lambda Function

**Method 1: Using substraction operator(-)**

This is the most straightforward method.

**Visual Representation**

**Example**

```
from decimal import Decimal
# Define the operands for subtraction
number1 = 21
number2 = 17
# Initialize two floating-point numbers
float_number1 = Decimal('4.3')
float_number2 = Decimal('1.7')
# Subtracting number2 from number1
difference = number1 - number2
# Print the result of the first subtraction
print(f"The result is {difference}")
# Subtracting number1 from number2
inverse_difference = number2 - number1
# Print the result of the second subtraction
print(f"The result is {inverse_difference}")
# Subtract the second float number from the first and store the result
float_difference = float_number1 - float_number2
# Print the result of the float subtraction
print(f"The result is {float_difference}")
```

**Output**

```
The result is 4
The result is -4
The result is 2.6
```

If you want to take the inputs from a user, use the input() function.

**Example**

```
number1 = int(input("Enter the first_operand: "))
number2 = int(input("Enter the second_operand: "))
difference = number1 - number2
print(f"The result is {difference}")
```

**Output**

```
Enter the first_operand: 21
Enter the second_operand: 17
The result is 4
```

**Method 2: Using a Function**

You can define a function that will be useful for performing subtraction multiple times within your code.

**Example**

```
def subtract(number1, number2):
return number1 - number2
difference = subtract(21, 17)
print(f"The result is {difference}")
```

**Output**

`The result is 4`

**Method 3: Using Lambda Function**

We can use lambda function which takes two parameters, number1 and number2, and returns the result of subtracting number2 from number1.

**Example**

```
# Define a lambda function for subtraction
subtract = lambda number1, number2: number1 - number2
difference = subtract(21, 17)
print(f"The result is {difference}")
```

**Output**

`The result is 4`

## Tom

We can’t use ‘-‘ directly for floats

>>> 3.1-2.2

0.8999999999999999

## Krunal

Due to the way floating-point numbers are represented in computer systems, you get 0.8999999999999999 instead of 0.9.

It is recommended to use a data type or library that provides arbitrary or higher precision, such as Python’s decimal.Decimal.

from decimal import Decimal

result = Decimal(‘3.1’) – Decimal(‘2.2’)

print(result) # Output: 0.9

## Newtum Solutions

WOW, I just read this article and found it very useful and informative.