In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.
Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.
You can find a detailed description at PEP 238: Changing the Division Operator.
Answer from Eli Courtwright on Stack OverflowIn Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division.
In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior.
Regardless of the future import, 5.0 // 2 will return 2.0 since that's the floor division result of the operation.
You can find a detailed description at PEP 238: Changing the Division Operator.
Python 2.x Clarification:
To clarify for the Python 2.x line, / is neither floor division nor true division.
/ is floor division when both args are int, but is true division when either of the args are float.
Why is 11//2 = 5?
In Python, why does 21*2=42 yet 84/2=42.0?
math - Why is the same both (5 % 2) and (-5 % 2) in Python - Stack Overflow
Why print(5 ** 2 ** 0 ** 1) = 5 in Python? - Stack Overflow
Videos
Fresh beginner here.
I understand // rounds the resulting number to the closest integer. I guess the type "rounding" is a different thing, but its said in the post linked below that Python 3 uses 'ties to even' rule. I'm wondering why 5.5 rounds down rather than up? Stuck in the theory, any clarification is appreciated - cheers!!
https://www.reddit.com/r/learnpython/comments/92ne2s/why_does_round05_0/?utm_source=share&utm_medium=web2x&context=3
There are other examples, such as:
-
(4+8)*(6.5-3)=42.0
-
3*7+6/2*(4+3)=42.0
-
(1+2)**(5-2)+5*3=42
It seems to me like any decimal or fraction in the expression will lead to a float answer. Is this correct?
Why? Because the modulo operator is defined that way in python.
The documentation states:
The modulo operator always yields a result with the same sign as its second operand (or zero); [...]
And:
The function
math.fmod()returns a result whose sign matches the sign of the first argument instead, [...] Which approach is more appropriate depends on the application.
You can look at the % operation in at least a couple of different ways. One important point of view is that m % n finds the element of Z[n] which is congruent to m, where Z[n] is an algebraic representation of the integers restricted to 0, 1, 2, ..., n, called the ring of integers modulo n. Note that all integers, positive, negative, and 0, are congruent to some element 0, 1, 2, ..., n in Z[n].
This ring (that is, this set plus certain operations on it) has many well-known and useful properties. For that reason, it's often advantageous to try to cast a problem in a form that leads to Z[n], where it may be easier to work. This, ultimately, is the reason the Python % was given its definition -- in the end, it has to do with operations in the ring of integers modulo n.
This article about modular arithmetic (in particular, the part about integers modulo n) could be a good starting point if you'd like to know more about this topic.
Because, like many languages, the exponentiation operator binds right-to-left:
5 ** 2 ** (0 ** 1)
==
5 ** (2 ** 0)
==
5 ** 1
==
5
Exponentiation is right-associative, so your expression is the same as
5 ** (2 ** (0 ** 1))
== 5 ** (2 ** 0)
== 5 ** 1
== 5
where any integer raised to the zeroth power is 1 by definition.