0 votes
1 view
in Python by (21.3k points)

I saw this in someone's code:

y = img_index // num_images

where img_index is a running index and num_images is 3.

When I mess around with // in IPython, it seems to act just like a division sign (i.e. one forward slash). I was just wondering if there is any reason for having double forward slashed?

1 Answer

0 votes
by (52.8k points)

The reason for // is because, in Python 3, they made the / operator do a floating-point division, and to get integer division we have  // operator i.e quotient without remainder; When you work with Python 2, where the / operator was simply integer division, unless one of the operands was already a floating-point number.

In Python 2.X you will do as follows:

>>> 10/3 

3.33

# to get a floating point number from integer division:

>>> 10.0/3

3.3333333333333335

>>> float(10)/3

3.3333333333333335

While in Python 3:

>>> 10/3

3.3333333333333335

>>> 10//3

3

...