Skip to content

Division Operators in Python

  • by

Python has two types of division operators. Both allow you to divide two numbers and return a quotient, i.e., the first number (number at the left) is divided by the second number (number at the right) and returns the quotient.

  • /: Divides the number and returns a floating point value.
  • //: Divides the number and returns a whole number (round down).

Division Operators in Python

Simple example code.

# Dividing an integer by an integer
print("The answer for 5/2: ", 5 / 2)
print("The answer for 5//2: ", 5 // 2)

# Dividing an integer by a float
print("The answer for 5/2.75: ", 5 / 2.75)
print("The answer for 5//2.75: ", 5 // 2.75)

# Dividing a float by an integer
print("The answer for 5.5/2: ", 5.5 / 2)
print("The answer for 5.5//2: ", 5.5 // 2)

# Dividing a float by an float
print("The answer for 5.5/2.5: ", 5.5 / 2.5)
print("The answer for 5.5//2.5: ", 5.5 // 2.5)

Output:

Division Operators in Python

Modulo Operator (%): This operator returns the remainder of the division.

result = 5 % 2
print(result)  # Output: 1

True Division (from __future__): In Python 2, the / operator is used to perform floor division for integer operands. However, if you want to ensure that division always performs true division (returning a float), you can use the from __future__ import division statement in Python 2 or just use Python 3.

from __future__ import division

result = 5 / 2
print(result)  # Output: 2.5

These are the primary division-related operators in Python. Each serves a specific purpose and can be chosen based on the desired outcome of the division operation.

Do comment if you have any doubts or suggestions on this Python Operator topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading