Python Program to Subtract Two Numbers

In Python, subtraction is performed using the '-' operator. You can subtract numbers by assigning values directly or by taking input from the user.

This program demonstrates how to take user input and calculate the difference between two numbers.

Method: Using User Input

1. Take input from the user using input() function.

2. Convert the input values into integers or floats.

3. Use the '-' operator to subtract the numbers.

4. Display the result using print() function.

Python Program to Subtract Two Numbers

Python
Subtract two numbers using user input
# Take input from user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

# Subtract two numbers
difference = num1 - num2

# Display the result
print("Difference =", difference)
Sample Input:
Enter first number: 20
Enter second number: 5

Output:
Difference = 15.0

Explanation

1. The input() function is used to take user input.

2. The float() function converts the input string into a numeric value.

3. The '-' operator performs the subtraction.

4. The result is displayed using the print() function.

Advantages

Simple and easy-to-understand example.

Demonstrates arithmetic operations in Python.

Helpful for beginners learning Python basics.

Conclusion

Subtracting two numbers in Python is straightforward using the '-' operator.

By taking user input and converting it to numeric type, we can easily perform subtraction operations.