2 views
in Python

The Python 2 has two division operators:- / and //

Output:

========================================= RESTART: Shell =========================================

>>> for x in range(10):

for y in range(1, 10):

print x,'//',y,'=',x//y

print x,'/',y,'=',x/y

print

0 // 1 = 0

0 / 1 = 0

0 // 2 = 0

0 / 2 = 0

0 // 3 = 0

0 / 3 = 0

0 // 4 = 0

0 / 4 = 0

0 // 5 = 0

0 / 5 = 0

0 // 6 = 0

0 / 6 = 0

0 // 7 = 0

0 / 7 = 0

0 // 8 = 0

0 / 8 = 0

0 // 9 = 0

0 / 9 = 0

1 // 1 = 1

1 / 1 = 1

1 // 2 = 0

1 / 2 = 0

1 // 3 = 0

1 / 3 = 0

1 // 4 = 0

1 / 4 = 0

1 // 5 = 0

1 / 5 = 0

1 // 6 = 0

1 / 6 = 0

1 // 7 = 0

1 / 7 = 0

1 // 8 = 0

1 / 8 = 0

1 // 9 = 0

1 / 9 = 0

2 // 1 = 2

2 / 1 = 2

2 // 2 = 1

2 / 2 = 1

2 // 3 = 0

2 / 3 = 0

2 // 4 = 0

2 / 4 = 0

2 // 5 = 0

2 / 5 = 0

2 // 6 = 0

2 / 6 = 0

2 // 7 = 0

2 / 7 = 0

2 // 8 = 0

2 / 8 = 0

2 // 9 = 0

2 / 9 = 0

3 // 1 = 3

3 / 1 = 3

3 // 2 = 1

3 / 2 = 1

3 // 3 = 1

3 / 3 = 1

3 // 4 = 0

3 / 4 = 0

3 // 5 = 0

3 / 5 = 0

3 // 6 = 0

3 / 6 = 0

3 // 7 = 0

3 / 7 = 0

3 // 8 = 0

3 / 8 = 0

3 // 9 = 0

3 / 9 = 0

4 // 1 = 4

4 / 1 = 4

4 // 2 = 2

4 / 2 = 2

4 // 3 = 1

4 / 3 = 1

4 // 4 = 1

4 / 4 = 1

4 // 5 = 0

4 / 5 = 0

4 // 6 = 0

4 / 6 = 0

4 // 7 = 0

4 / 7 = 0

4 // 8 = 0

4 / 8 = 0

4 // 9 = 0

4 / 9 = 0

5 // 1 = 5

5 / 1 = 5

5 // 2 = 2

5 / 2 = 2

5 // 3 = 1

5 / 3 = 1

5 // 4 = 1

5 / 4 = 1

5 // 5 = 1

5 / 5 = 1

5 // 6 = 0

5 / 6 = 0

5 // 7 = 0

5 / 7 = 0

5 // 8 = 0

5 / 8 = 0

5 // 9 = 0

5 / 9 = 0

6 // 1 = 6

6 / 1 = 6

6 // 2 = 3

6 / 2 = 3

6 // 3 = 2

6 / 3 = 2

6 // 4 = 1

6 / 4 = 1

6 // 5 = 1

6 / 5 = 1

6 // 6 = 1

6 / 6 = 1

6 // 7 = 0

6 / 7 = 0

6 // 8 = 0

6 / 8 = 0

6 // 9 = 0

6 / 9 = 0

7 // 1 = 7

7 / 1 = 7

7 // 2 = 3

7 / 2 = 3

7 // 3 = 2

7 / 3 = 2

7 // 4 = 1

7 / 4 = 1

7 // 5 = 1

7 / 5 = 1

7 // 6 = 1

7 / 6 = 1

7 // 7 = 1

7 / 7 = 1

7 // 8 = 0

7 / 8 = 0

7 // 9 = 0

7 / 9 = 0

8 // 1 = 8

8 / 1 = 8

8 // 2 = 4

8 / 2 = 4

8 // 3 = 2

8 / 3 = 2

8 // 4 = 2

8 / 4 = 2

8 // 5 = 1

8 / 5 = 1

8 // 6 = 1

8 / 6 = 1

8 // 7 = 1

8 / 7 = 1

8 // 8 = 1

8 / 8 = 1

8 // 9 = 0

8 / 9 = 0

9 // 1 = 9

9 / 1 = 9

9 // 2 = 4

9 / 2 = 4

9 // 3 = 3

9 / 3 = 3

9 // 4 = 2

9 / 4 = 2

9 // 5 = 1

9 / 5 = 1

9 // 6 = 1

9 / 6 = 1

9 // 7 = 1

9 / 7 = 1

9 // 8 = 1

9 / 8 = 1

9 // 9 = 1

9 / 9 = 1

>>>

demonstrates (nearly?) all the time a/b equals a//b. Is there any time that it isn't? If not, for what reason did Python 2 incorporate two operators that do something very similar?

by (26.4k points)

The // is the floored-division operator in Python. The thing that matters is noticeable when partitioning floating-point values.

In Python2, isolating two ints utilizes integer division, which winds up getting you something very similar to floored division. Be that as it may, you can in any case utilize // to get a stunned consequence of the floating point division.

# Python 2

>>> 10.0 / 3

3.3333333333333335

>>> 10.0 // 3

3.0

But in Python3, separating two ints brings about a float, however utilizing // goes about as integer division.

# Python3

>>> 10 / 3

3.3333333333333335

>>> 10 // 3

3

Floored division implies round towards negative limitlessness. This is equivalent to truncation for positive values, yet not for negative. 3.3 adjusts down to 3, however - 3.3 adjusts down to - 4.

# Python3

>>> -10//3

-4

>>> 10//3

3

Join the python online course fast, to learn python concepts in detail and get certified.