Python Practice › Basic Programs › Program 2
Subtract two integers
Write a Python program to subtract one integer from another and print the result.
Input
num1 = 75, num2 = 28
Output
75 - 28 = 47
Approach
- Store both numbers.
- Subtract the second from the first with
-. - Print the difference.
Solution
num1 = 75num2 = 28 difference = num1 - num2print(f"{num1} - {num2} = {difference}")Output
75 - 28 = 47
Line by line
Line 4
- subtracts num2 from num1. The order matters: num2 - num1 would give -47.
Line 5
The f-string fills each {} with the variable’s value, which makes the output read like the calculation.
Learn the concept
New to this topic? Read the lesson, then come back.