What division is in python?

Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. 

There are two types of division operators: 

(i) Float division: 

The quotient returns by this operator is always a float number, no matter if two numbers are integer. For example:

>>>5/5
1.0
>>>10/2
5.0
>>>-10/2
-5.0
>>>20.0/2
10.0

(ii) Integer division( Floor division): 

The quotient returned by this operator is dependent on the argument being passed. If any of the numbers is float, it returns output in float. It is also known as Floor division because, if any number is negative, then the output will be floored. For example:

>>>5//5
1
>>>3//2
1
>>>10//3
3

Consider the below statements in Python.

Python3

print (5//2)

print (-5//2)

Output:

2
-3

The first output is fine, but the second one may be surprised if we are coming Java/C++ world. In Python, the “//” operator works as a floor division for integer and float arguments. However, the division operator ‘/’ returns always a float value.

Note: The “//” operator is used to return the closest integer value which is less than or equal to a specified expression or value. So from the above code, 5//2 returns 2. You know that 5/2 is 2.5, and the closest integer which is less than or equal is 2[5//2].( it is inverse to the normal maths, in normal maths the value is 3).

Example

Python3

print (5.0/2)

print (-5.0/2)

The real floor division operator is “//”. It returns the floor value for both integer and floating-point arguments.

Python3

print (5//2)

print (-5//2)

print (5.0//2)

print (-5.0//2)

See this for example. 


Python Division – Integer Division & Float Division

Division operation is an arithmetic operation where we shall try to compute how much we have to divide dividend into equal parts, so that each of the divisor will get an equal amount.

In Python programming, you can perform division in two ways. The first one is Integer Division and the second is Float Division.

In this tutorial, we will learn how to perform integer division and float division operations with example Python programs.

Python Integer Division

Integer division means, the output of the division will be an integer. The decimal part is ignored. In other words, you would get only the quotient part. To perform integer division in Python, you can use // operator.

// operator accepts two arguments and performs integer division. A simple example would be result = a//b.

In the following example program, we shall take two variables and perform integer division using // operator.

Python Program

a, b = 7, 3
result = a//b
print(result)

Run

Output

2

You can also provide floating point values as operands for // operator. In the following example, we shall take two float values and compute integer division.

Python Program

a, b = 7.2, 3.1
result = a//b
print(result)

Run

Output

2.0

The result is a float, but only quotient is considered and the decimal part or reminder is ignored.

Python Float Division

Float division means, the division operation happens until the capacity of a float number. That is to say result contains decimal part. To perform float division in Python, you can use / operator.

Division operator / accepts two arguments and performs float division. A simple example would be result = a/b.

In the following example program, we shall take two variables and perform float division using / operator.

Python Program

a, b = 7, 3
result = a/b
print(result)

Run

Output

2.3333333333333335

For float division, you can give any number for arguments of types: int or float.

Summary

In this tutorial of Python Examples, we learned how to perform two types of Python Division namely: Integer Division and Float Division.

Why do we use division in Python?

Sometimes we expect division to generate create precise floating point number and other times we want a rounded-down integer result. In general, the python definition of division(/) depended solely on the arguments. For example in python 2.7, dividing 20/7 was 2 because both arguments where integers.

Why division in Python is float?

The division function in Python has two variations: Float division: gives a decimal answer. Integer division: gives the answer in whole numbers (the division result is rounded to the nearest whole number).

What does 2 slashes mean in Python?

In Python, you use the double slash // operator to perform floor division. This // operator divides the first number by the second number and rounds the result down to the nearest integer (or whole number).

What is Python floor division?

In Python, we can perform floor division (also sometimes known as integer division) using the // operator. This operator will divide the first argument by the second and round the result down to the nearest whole number, making it equivalent to the math. floor() function.