To find the difference between two numbers using Python, you can simply subtract one number from the other using the -
operator. Here’s an example:
num1 = 10
num2 = 5
diff = num1 - num2
print(diff) # Output: 5
In this example, we define two variables num1
and num2
with the values of 10 and 5, respectively. We then subtract num2
from num1
using the -
operator and store the result in a new variable diff
. Finally, we print the value of diff
, which is 5 in this case.
You can also take input from the user to find the difference between two numbers using the input()
function. Here’s an example:
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
diff = num1 - num2
print(diff)
Enter the first number: 20 Enter the second number: 19 1
In this example, we use the input()
function to take two integer inputs from the user and store them in the variables num1
and num2
. We then subtract num2
from num1
using the -
operator and store the result in a new variable diff
. Finally, we print the value of diff
, which is the difference between the two numbers entered by the user.
Read more Loops in Python
Watch Youtube video for this tutorial.
1 thought on “Finding the difference between two numbers using Python”