Python Practice › Basic Programs › Program 40

Arithmetic operations on two numbers

EasyBasic Programs · 40 of 40

Write a Python program to perform all the basic arithmetic operations on two numbers.

Advertisement
Input
a = 17, b = 5
Output
Addition        22
Subtraction     12
Multiplication  85
Division        3.4
Floor division  3
Remainder       2
Power           1419857

Approach

  1. Apply each arithmetic operator and print the results in a table.

Solution

Python
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}")
Output
Addition        22
Subtraction     12
Multiplication  85
Division        3.4
Floor division  3
Remainder       2
Power           1419857

Line by line

Lines 2-10

A dictionary maps each operation’s name to its result.

Lines 11-12

The loop prints each pair; :<15 pads the name to 15 characters so the values line up.

Advertisement

Learn the concept

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

More basic programs

← All 40 Basic Programs