How to divide in Python?
To divide in Python, use one of the division operators: / or //.
The difference between the division operators:
/ returns a floating value
// returns an integer (by rounding down)
divide with / operator
The / operator returns the result without any rounding.
The result 2.5 is returned as float value.
divide with // operator
The // operator rounds the result down to the next integer.
Instead of the float value of 2.5, the integer 2 is returned.
divide and round up
To divide and round up, use the / operator and the ceil() method of the math module in Python.
The math module is imported and the result is rounded up to the next integer.
divide float values
To divide float values in Python, use the / operator to perform a float division or use // to divide and round to an integer.
# divide float values
a, b = 7.2, 3.2
myFloat = a / b
myInt = a // b
In this example, we use float division and integer division by using / operator and // operator. Both results are printed for comparison.