What Does the // Operator Do?


In Python, the // is the double slash operator i.e. Floor Divison. The // operator is used to perform division that rounds the result down to the nearest integer. The usage of the // operator is quite easy. We will also compare the results with single slash division. Let us first see the syntax:

Syntax of // (double slash) Operator

The a and b are 1st and 2nd number:

a // b

Example of // (double slash) Operator

Let us now see an example to implement the double slash operator in Python -

a = 37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)

Output

The 1st Number =  37
The end Number =  11
Result of floor division =  3

Implementing // (double slash) operator with a negative number

We will try to use the double slash operator with a negative number as input. Let’s see the example −

# A negative number with a positive number a = -37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using floor division res = a // b print("Result of floor division = ", res)

Output

The 1st Number =  -37
The end Number =  11
Result of floor division =  -4

As you can see in the above output, using the negative number did not impact the rounding off. The result rounded down

Let us check the results with single slash and compare.

Example of / Operator

The / operator is the division operator in Python. Let us see an example −

a = 37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using the / operator res = a / b print("Result of division = ", res)

Output

The 1st Number =  37
The end Number =  11
Result of division =  3.3636363636363638

Implementing / operator with a negative number

In this example, we will learn how a slash operator works with a negative number:

a = -37 b = 11 # 1st Number print("The 1st Number = ",a) # 2nd Number print("The end Number = ",b) # Dividing using the / operator res = a / b print("Result of division = ", res)

Output

The 1st Number =  -37
The end Number =  11
Result of division =  -3.3636363636363638

Updated on: 15-Sep-2022

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements