Arithmetic operations on two numbers
Write a Python program to perform all the basic arithmetic operations on two numbers.
a = 17, b = 5
Addition 22 Subtraction 12 Multiplication 85 Division 3.4 Floor division 3 Remainder 2 Power 1419857
Approach
- Apply each arithmetic operator and print the results in a table.
Solution
a, b = 17, 5operations = { "Addition": a + b, "Subtraction": a - b, "Multiplication": a * b, "Division": a / b, "Floor division": a // b, "Remainder": a % b, "Power": a ** b,}for name, value in operations.items(): print(f"{name:<15} {value}")Addition 22 Subtraction 12 Multiplication 85 Division 3.4 Floor division 3 Remainder 2 Power 1419857
Line by line
A dictionary maps each operation’s name to its result.
The loop prints each pair; :<15 pads the name to 15 characters so the values line up.
Learn the concept
New to this topic? Read the lesson, then come back.