Simple Arithmetic, Input and Output in Python

Basic Mathematical Operators

Operator Usage
Addition + 3 + 5 = 8
Subtraction - 9 - 7 = 2
Multiplication * 2 * 22 = 44
Division / 9.0 / 2 = 4.5
Modulus % 11 % 5 = 1
Floor Division // 9.0 // 2 = 4.0
Power ** 2 ** 3 = 8

If you have a working Python installation in your system, you can type

python

in the command line terminal to start the Python interpreter. Then you can type the following commands followed by an enter key and check their output accordingly:

1+2

4-7

4*6

2**3

13/2

13/2.0

13.0/2

13.0//2

The input 1+2will produce 3 as output, similarly

4-7 will produce -3,

4*6 will produce 24,

2**3 will produce 8,

13/2 will produce 6.5,

13/2.0 will produce 6.5,

13.0/2 will produce 6.5, and finally

13.0//2 will produce 6.


There are two division operators in Python 3 namely true division / and floor division //. The / (true division) operator always produces a float result even when the two operands are integers (e.g. 4/2 yields 2.0). On the other hand, the // (floor division) operator performs division and returns a floor value for both integer and float operands (e.g. 9/2 produces 4, while 9.0//2 yields 4.0).

Note that division operators behave differently in Python 3 and Python 2 which reached end-of-life on 1st of January, 2020. On the other hand you might still have to maintain a legacy code some time in the future.


When the operands are provided as numbers, e.g. written by hand, the operators may seem redundant, however, programmers seldom use explicit numbers as operands. Instead, many calculations depend on variables, which may either be integer or floating point numbers, but the expected value may be an integer. For instance, integers are useful for finding the positioning order of an entity in a list of items.


You can alternatively work on the following example if you do not have the terminal access at the moment:

print(1+2)
print(4-7)
print(4*6)
print(2**3)
print(13/2)
print(13/2.0)
print(13.0/2)
print(13.0//2)

Division by Zero Error

When the denominator is zero in a division operation, the result is inconclusive. Most of the programming languages raise an error if such a calculation occurs during the execution, however, it may cause the program to halt as well. Programmers must be cautious when performing a division operation and check whether the denominator is equal to zero before the division operation. Necessary warnings and precautions must be planned for such cases.