Subtract two integers

EasyBasic Programs · 2 of 40

Write a Python program to subtract one integer from another and print the result.

Advertisement
Input
num1 = 75, num2 = 28
Output
75 - 28 = 47

Approach

  1. Store both numbers.
  2. Subtract the second from the first with -.
  3. Print the difference.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More basic programs

← All 40 Basic Programs