What Does & Mean in Python

What does & mean in python

Answer

The & symbol is a bitwise AND operator. Used with 1, it basically masks the value to extract the lowest bit, or in other words will tell you if the value is even or odd.

More Info on Python's & operator

For more information, see: http://wiki.python.org/moin/BitwiseOperators

Why it Works to check Odd vs. Even

EDIT: Adding this section since this answer is getting some love

The reason why ANDing a value with 1 tells if the value is odd or even may not be obvious at first.

The binary representation of a number is essentially the sum of a series of YES or NO for each power of 2 moving leftward starting in the rightmost digit with 1, 2, 4, 8, ...

There is only one way to represent any number in this way. E.g. the number 13 (base 10) can be written in binary as "1101" (or hexadecimal as 0xD, but that's beside the point). See here:

    1   1   0   1
x x x x
8 4 2 1
= = = =
8 + 4 + 0 + 1 = 13

Notice that aside from the rightmost binary digit, all other 1 digits will add an even number (i.e. a multiple of 2) to the sum. So the only way to get an odd final sum is to add that odd 1 from the rightmost digit. So if we're curious if a number is odd or even, we can look at its binary representation and ignore everything except for the rightmost digit.

To do this, we use the bitwise AND operator. The value 1 in binary is expressed as 1:

    0   0   0   1
x x x x
8 4 2 1
= = = =
0 + 0 + 0 + 1 = 1

ANDing a value with 1 like this will result in 1 if the value's rightmost bit is set, and 0 if it is not.

And because 0 is generally considered "false" in most languages, and non-zero values considered "true", we can simply say as a shortcut:

if (value & 1): do_something_with_odd_value()...

What is the role of “&” when used with integers in python?

That's the bitwise-and operator: For the official documentation, it does a "bitwise and". Each bit of the output is 1 if the corresponding bit of x AND of y is 1, otherwise it's 0.

What do these operators mean (** , ^ , %, //)?

  • **: exponentiation
  • ^: exclusive-or (bitwise)
  • %: modulus
  • //: divide with integral result (discard remainder)

^=, -= and += symbols in Python

As almost any modern language, Python has assignment operators
so they can use them every time you want to assign a value to a variable after doing some arithmetic or logical operation, both (assignment and operation) are expressed in a compact way in one statement...

Table from Tutorials Point:




















































OperatorDescriptionExample
=Assigns values from right side operands to left side operandc = a + b assigns value of a + b into c
+= Add ANDIt adds right operand to the left operand and assign the result to left operandc += a is equivalent to c = c + a
-= Subtract ANDIt subtracts right operand from the left operand and assign the result to left operandc -= a is equivalent to c = c - a
*= Multiply ANDIt multiplies right operand with the left operand and assign the result to left operandc *= a is equivalent to c = c * a
/= Divide ANDIt divides left operand with the right operand and assign the result to left operandc /= a is equivalent to c = c / a
%= Modulus ANDIt takes modulus using two operands and assign the result to left operandc %= a is equivalent to c = c % a
**= Exponent ANDPerforms exponential (power) calculation on operators and assign value to the left operandc **= a is equivalent to c = c ** a
//= Floor DivisionIt performs floor division on operators and assign value to the left operandc //= a is equivalent to c = c // a

What does the /= operator mean in Python?

It's an assignment operator shorthand for / and =.

Example:

x = 12
x /= 3
# equivalent to
x = x / 3

If you use help('/='), you can get the full amount of symbols supported by this style of syntax (including but not limited to +=, -=, and *=), which I would strongly encourage.

What Does mean in Python?

The >> operator in Python is a bitwise right-shift. If your number is 5, then its binary representation is 101. When right-shifted by 1, this becomes 10, or 2. It's basically dividing by 2 and rounding down if the result isn't exact.

Your example is bitwise-complementing c1, then right-shifting the result by 1 bit, then masking off all but the 24 low-order bits.



Related Topics



Leave a reply



Submit