Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two forward slashes in Python

I came across this sample of code from a radix sort:

def getDigit(num, base, digit_num):     # pulls the selected digit     return (num // base ** digit_num) % base 

What does the // do in Python?

like image 401
Biff Avatar asked Jan 21 '13 17:01

Biff


People also ask

What are two backslashes in Python?

The double slash (//) operator is used in python for different purposes. One use of this operator is to get the division result. The division result of two numbers can be an integer or a floating-point number.

What does two forward slashes mean in code?

Lines beginning with // are comments and are ignored during action execution.

How do you split a forward slash in Python?

Use the str. split() method to split a string on the forward slashes, e.g. my_list = my_str. split('/') .

What does two division signs mean in Python?

The Double Division operator in Python returns the floor value for both integer and floating-point arguments after division.


1 Answers

// is the floor division operator. It produces the floor of the quotient of its operands, without floating-point rounding for integer operands. This is also sometimes referred to as integer division, even though you can use it with floats, because dividing integers with / used to do this by default.

In Python 3, the ordinary / division operator returns floating point values even if both operands are integers, so a different operator is needed for floor division. This is different from Python 2 where / performed floor division if both operands were integers and floating point division if at least one of the operands was a floating point value.

The // operator was first introduced for forward-compatibility in Python 2.2 when it was decided that Python 3 should have this new ability. Together with the ability to enable the Python 3 behavior via from __future__ import division (also introduced in Python 2.2), this enables you to write Python 3-compatible code in Python 2.

like image 154
sepp2k Avatar answered Sep 20 '22 05:09

sepp2k