Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "variable //= a value" syntax mean in Python? [duplicate]

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?

like image 914
Chris Avatar asked Oct 27 '16 00:10

Chris


People also ask

What happens when you assign a value to a variable in Python?

When a variable is assigned a value in Python, the variable does not store the absolute value directly. Instead, Python creates a new reference to an object representing that value. For example, the line a = 1 assigns the value 1 to the variable a .

What does == mean in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and !=

How do you assign a value to a variable in Python example?

The assignment operator, denoted by the “=” symbol, is the operator that is used to assign values to variables in Python. The line x=1 takes the known value, 1, and assigns that value to the variable with name “x”. After executing this line, this number will be stored into this variable.

Why the ID of float values are different when the same value is assigned to two different variables in Python?

In that case the float value containing variable may contain same data but reason behind is to mathematical rules. The only two value are consider after the point & remaining are value get neglected. so the we get two different addresses of the same data containing variables.


1 Answers

// 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
32

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

like image 173
Zizouz212 Avatar answered Oct 22 '22 00:10

Zizouz212