Chris Chris - 1 year ago 62
Python Question

python syntax: what does the "variable //= a value" syntax mean?

I came across with the code syntax

d //= 2
where d is a variable. This is not a part of any loop, I don't quite get the expression.

Can anybody enlighten me please?

Answer Source

// is a floor division operator. The = beside it means to operate on the variable "in-place". It's similar to the += and *= operators, if you've seen those before, except for this is with division.

Suppose I have a variable called d. I set it's value to 65, like this.

>>> d = 65

Calling d //= 2 will divide d by 2, and then assign that result to d. Since, d // 2 is 32 (32.5, but with the decimal part taken off), d becomes 32:

>>> d //= 2
>>> d

It's the same as calling d = d // 2.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download