J
J
Jungles2020-06-25 13:09:29
Python
Jungles, 2020-06-25 13:09:29

What is the difference between 'AND' and '&'?

Worked with Pandas.
And when I tried to enter, for example, such a condition

a=data[(data.Age > 20) and (data.Wage > 10)]. count()

an error came out. I could not understand, because everything seems to be in order.
Then out of boredom I wrote
a=data[(data.Age > 20) & (data.Wage > 10)]. count()

IT WORKED!
I got on the Internet for information,
someone already asked such a question - " https://stackoverflow.com/questions/21415661/logic... "
But I didn’t catch up with the explanation (probably, the level of English affected)
by searching the Internet already not about Pandas, I came to something.
AND - a logical element that compares True and False, or 1 and 0, if at least 1 element is False, then it returns a False answer.
False and True
False

& is a logical "AND" - that is, multiplication,
255 & 1
1

And in order not to take a steam bath that there is a boolean value or there was no confusion False answer or True, the Pandas developers removed this command altogether.
What is actually the correct answer?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pankov, 2020-06-25
@Jungles

the difference is that `and` is a logical operation, it cannot be overridden, that is, it is impossible for an object to implement a method that will implement its own algorithm for calculating the result of the operation.
This is due to the standard optimizations of logical expressions, which (optimizations) are in almost all languages.
Roughly speaking, the `and` operator (conjunction) takes two arguments (left and right) and returns the value of the first false or the last true, if all are true.
For example:

'one' and 0  # вернёт 0
'' and 13  # вернёт ''
3 and 5  # вернёт 5

The disjunction (or) works similarly, but returns the first true argument it encounters, or the last false argument if both are false:
3 or 5  # вернёт 3
'' or 'hello'  # вернёт 'hello'
None or 0 # вернёт 0

In this case, if the first suitable argument is returned, the second one will not be calculated at all. There may be an integer expression with function calls, but it will not be evaluated, since this value is not needed. In fact, it is very convenient and allows you to concisely write down many things.
Operations & | according to their principle of operation, they are no different from addition, subtraction, multiplication, division, checking for equality and inequality, and many other operations. All these operations can be blocked. That is, writing a class like this:
Class MyClass:
    def __and__(self, right_argument):
        result = 'что угодно'
        print(f'Фигасе: {self} & {right_argument} -> {result}')
        return result

a = MyClass()
b = MyClass()
print('Вот так вот:\n', a & b)

Фигасе: <__main__.MyClass object at 0x7f3df1aeb588> & <__main__.MyClass object at 0x7f3df1aeb438> -> что угодно
Вот так вот:
 что угодно

pandas uses overlap to actually form a function that will then be applied to a large number of arguments in turn. Of course, logical operations are not suitable for this.
I'm just sure that the author of the question will not fully understand what I explained here, but perhaps it will be useful to someone.
It is depressing that people do not want to learn, but want to immediately be ready-made programmers and write in production.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question