How to divide in Python?

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. print(5/2) The result 2.5 is returned as float value. 2.5

divide with // operator

The // operator rounds the result down to the next integer. print(5//2) Instead of the float value of 2.5, the integer 2 is returned. 2

divide and round up

To divide and round up, use the / operator and the ceil() method of the math module in Python. import math print(math.ceil(5/2)) The math module is imported and the result is rounded up to the next integer. 3

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 print(myFloat) print(myInt) In this example, we use float division and integer division by using / operator and // operator. Both results are printed for comparison. 2.25 2.0



Click to jump to section