-1

I mistakenly wrote the line of code, I expected it to give me an error but it returned an answer.

Codx = [num for num in range(1,9)     if num & 2 = 0]
Print (codx)

I got the answer [1,4,5,8]

Then I did

Print(3&2)

Answer was 2

Print(5&2)

Answer was 0

What’s the role of the ampersand?

Chris Doyle
  • 10,703
  • 2
  • 23
  • 42
  • 1
    related: https://stackoverflow.com/questions/1746613/bitwise-operation-and-usage – EdChum Dec 09 '19 at 15:16
  • This is a bitwise AND operator. Please consult some Python book/tutorial, operators section. https://wiki.python.org/moin/BitwiseOperators – Eugene Sh. Dec 09 '19 at 15:17
  • It's a bitwise AND, not a conditional and, which is what you likely intended. By the way the conditional and in python is simply `and`. – h0r53 Dec 09 '19 at 15:18

1 Answers1

3

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.

jdinunzio
  • 1,506
  • 1
  • 11
  • 26