0

sorry about this basic question, I just a beginner in Python programming. From my undertsanding, & and "and" are the same things, and "&" is just a shorhand for "and" so if I use Python's dataframe

df[ (df.StateAb == "NSW") & (df.PartyAb == "LP") ]

this compliles OK,but if I type

df[ (df.StateAb == "NSW") and (df.PartyAb == "LP") ]

then it cannot be compiled correctly.

so what's the difference between "and" and "&",

  • You can refer these things. [2] [http://stackoverflow.com/questions/22646463/difference-between-and-boolean-vs-bitwise-in-python-why-difference-i] – Dhruv Bhavsar Sep 03 '16 at 05:55
  • 1
    The hint is in the name. The boolean operator behaves according to the rules of boolean logic; the bitwise operator performs bitwise arithmetic. – Karl Knechtel Sep 03 '16 at 06:27

1 Answers1

1

I found this one useful:

1 and 2
>> 2

1 & 2
>> 0

The first result is due to short circuiting. Python tests 1 and finds it true and returns the 2. But, the second part does 01 (Binary 1) & 10 (Binary 2) hence evaluating to 00 (1 & 0, 0 &1) , which is 0.

Bolajio
  • 127
  • 1
  • 15