What does /= mean in Python?

Asked

Viewed 5,082 times

10

Can anyone tell me what //= means in python?

On line 8 of this code has the use of it.

n = int(input("Digite um numero menor que 10: "))

aux, reverso = n, 0

while aux != 0:
    dig = aux%10
    reverso = reverso*10 + aux%10
    aux //= 10

if reverso == n:
    print(n, "é palidromo ")
else:
    print(n, "não é palídromo")

4 answers

7

This is one of what we call In-place Operators, which are operators executed jointly with the allocation operator on the object itself (hence the term in-place).

In this case, the operator //= is nothing more than the operator // together with the allocation operator =, being equivalent to:

aux = aux // 10

Assignment operators were defined precisely to avoid the need to type the same object twice within the expression.

The operator // is known as floor Division because it returns only the entire part of the division between the operands, different from the operator /, so-called true Division, which returns a floating point number. It is interesting to note that implicitly the operator // will call the method __ifloordiv__ of its object, then, by making aux //= 10 you implicitly are running aux.__ifloordiv__(10), or operator.ifloordiv(aux, 10).

Other operators in-place sane:

  • x += y, equivalent to x = x + y
  • x &= y, equivalent to x = x and y
  • x <<= y, equivalent to x = x << y
  • x %= y, equivalent to x = x % y
  • x *= y, equivalent to x = x * y
  • x @= y, equivalent to x = x @ y
  • x |= y, equivalent to x = x | y
  • x **= y, equivalent to x = x **y
  • x >>= y, equivalent to x = x >> y
  • x -= y, equivalent to x = x - y
  • x /= y, equivalent to x = x / y
  • x ^= y, equivalent to x = x ^y
  • +1 great answer, but what is the name and function of x @= y ?I saw in the documentation that calls the method __imatmul__, but in practice it would be useful to?

  • 2

    @Luizaugusto It is for matrix multiplication (matrices). To my knowledge no native object uses this operator, but it exists precisely to allow it to be overloaded in user-defined classes.

5

The //, call for floor Division, ignores the rest of a division and returns only the whole of the result. For example:

x = 17 // 3 
print(x)
>> 5

Other examples:

5 / 2 = 2.5        (2)
5 / 7 = 0.714285   (0)
5 / -6 = −0.8333   (-1 já que é o inteiro mais próximo de -0.833333)
5 / -2 = −2.5      (-3)
5 / -3 = −1.6666   (-2)

Floor Division is the inverse of module (%) that imprints the rest of a division.

x = 17 % 3 
print(x)
>> 2

Documentation of floor Division here and of module here

3

Is the same as aux = aux // 10

// Integer division. python splits, returns an integer and applies the function .floor(round down) to the result .

  • Reference in the documentation: https://docs.python.org/2/reference/expressions.html#Binary-arithmetic-Operations and other Stack Overflow questions: https://stackoverflow.com/a/15194137/5846448

0

According to his doubt, the operator // of Python, that is to say that it extracts the entire part of a division, for example:

>>> 10//9 resultado: 1;

Now //=10, I understand it’s the same +=10 in programming, I hope to have helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.