Something about pandas

Hi, I am working on pandas library learning in the following link:
I got stuck on task 5 so I take a look at pro’s video given by codecademy.
The pro use “&” as and operator. But I also find in this link and operator can be written in python as “and”

below is my code. Please tell me if we can use the keyword “and” as the solution to this task 5.

import codecademylib3

import pandas as pd





seed_request=inventory[(inventory.location=='Brooklyn')& (inventory.product_type=='seeds')]

The issue I assume you’re running into is that the standard keyword and is supposed to return a single value; True or False. But a numpy array like those you’re using contains numerous elements and it might be reasonable to do an element-wise comparison (most operations are performed element-wise). To do this numpy overloads certain operators to behave element-wise (addition, multiplication etc. etc.), the problem here is that and is a keyword rather than an operator and it cannot be overloaded.

So the wee workaround performed by numpy is to overload the bitwise & operator to perform an element-wise logical and operation but ONLY when the array is a boolean array since there’s no need for a separate bitwise operation.

So no you cannot just replace & with and when working with arrays as they behave differently. There is a function you could use if you prefer-

what’s the idea of overload?

Overloading crops up in a few places, in this instance we’re just talking about operator overloading, not function or method overloading (just in case you have a web search you can narrow it down a little).

You’re probably familiar with certain Python operators like +. Objects in Python make use of this operator with the .__add__ method (we’ll ignore __radd__ for now). This is how both objects like ints and objects like lists can use the + operator for what is entirely different behaviour.

What’s more custom types (like say a numpy array) can add their own __add__ method to define their own special behaviour; this is how they can behave element-wise whilst using the same operator, the relevant methods have been assigned to the numpy array type.

As a very pointless example here’s how you could define a poorly implemented custom .__add__ method in a new type to overload the + operator.

class A: def __add__(self, other): print("bang") A() + None