In Python 3, they made the / operator do a floating-point division, and added the // operator to do integer division (i.e., quotient without remainder); whereas in Python 2, the / operator was simply integer division, unless one of the operands was already a floating point number.
In Python 2.X:
>>> 10/3
3
>>> # To get a floating point number from integer division:
>>> 10.0/3
3.3333333333333335
>>> float(10)/3
3.3333333333333335
In Python 3:
>>> 10/3
3.3333333333333335
>>> 10//3
3
For further reference, see PEP238.
Answer from Mark Rushakoff on Stack OverflowCan someone explain why this line of code requires 2 slashes to work?
Does the double slash (or floor division I believe) operator round numbers ending in .4 up?
notation - How should one interpret a double slash, //? - Mathematics Stack Exchange
Can't get rid of double backslashes in filepaths -
Videos
In Python 3, they made the / operator do a floating-point division, and added the // operator to do integer division (i.e., quotient without remainder); whereas in Python 2, the / operator was simply integer division, unless one of the operands was already a floating point number.
In Python 2.X:
>>> 10/3
3
>>> # To get a floating point number from integer division:
>>> 10.0/3
3.3333333333333335
>>> float(10)/3
3.3333333333333335
In Python 3:
>>> 10/3
3.3333333333333335
>>> 10//3
3
For further reference, see PEP238.
// is unconditionally "flooring division", e.g:
>>> 4.0//1.5
2.0
As you see, even though both operands are floats, // still floors -- so you always know securely what it's going to do.
Single / may or may not floor depending on Python release, future imports, and even flags on which Python's run, e.g.:
$ python2.6 -Qold -c 'print 2/3'
0
$ python2.6 -Qnew -c 'print 2/3'
0.666666666667
As you see, single / may floor, or it may return a float, based on completely non-local issues, up to and including the value of the -Q flag...;-).
So, if and when you know you want flooring, always use //, which guarantees it. If and when you know you don't want flooring, slap a float() around other operand and use /. Any other combination, and you're at the mercy of version, imports, and flags!-)
mid_idx = len(sorted_list)//2 I feel like I know this but forgot
I'm taking my first programming course in college this semester and we've been tasked with a few basic exercises using mostly if-else statements and the odd very basic for loop. For this particular exercise, one issue I've come with that I hadn't before is when using the // operator. From my understanding + research and what has been taught to me, this operator is supposed to simply round down to the nearest whole number. I won't explain the entire exercise/project's goal, as per the rules of the subreddit, but the specific case I've just observed is when rounding -2.4, or more specifically abs(-2.4). I defined a variable as such:
digit1 = abs(num // 10)
num is a previously defined variable containing an integer input ( int(input("")) ). This variable is used to look for the first digit of the inputted number. When I input -24, the result I expected was 2, but the output ended up being 3. I'm not too sure what's going on. Is the absolute function or the fact that it's a negative number affecting the result?
Also, in case this is relevant, we're using replit as an IDE.
Thanks in advance for any responses.
Sidenote: I apologize if this is a very simple issue that I'm just not seeing. Asked similar questions in other sites and people seemed annoyed at how simple the solution was. Reddit's always been pretty nice but hey, you never know.