Confused about the given hint

Screen Link: https://app.dataquest.io/m/1027/numpy-boolean-masks-practice-problems/16/grade-categories

My Code:

def grade_category_count(grades):
    grade_dict = dict()
    grade_dict['A'] = len(grades[grades >= 90])
    grade_dict['B'] = grades[np.logical_and(grades >= 80,grades <= 89)].size
    grade_dict['C'] = grades[np.logical_and(grades >= 70, grades <= 79)].size
    grade_dict['D'] = grades[np.logical_and(grades >= 65,grades <= 69)].size
    grade_dict['E'] = grades[np.logical_and(grades >= 50,grades <= 64)].size
    grade_dict['F'] = grades[np.logical_and(grades >= 0,grades <= 49)].size
    return grade_dict

So, attempting to solve this I found this function that works OK for me, np.logical_and(…)
However, the hint provided for the exercise states
“Use the boolean operator & to combine the two conditions that define each range. Remember to use parentheses to surround each condition.”
This might be silly, but i couldn’t find any reference regarding simple ‘&’ as a boolean operator. “and” , yes, but not “&”. Maybe I missed something but can you explain point me to some place where it states when can I use “&” ?
Thanks!

I am unsure if this is specifically covered with numpy, but it is for pandas and that’s not too different than how it works in numpy - Using boolean operatotrs

It’s also a good opportunity to start looking up such new information through official documentation or just a simple search. For example, numpy's logical_and specifies the following -

The & operator can be used as a shorthand for np.logical_and on boolean ndarrays.

So, & is a more convenient alternative to np.logical_and

Comparisons, Masks, and Boolean Logic is also a good resource that could come up in a regular search that you might find valuable.

1 Like

It’s also a good opportunity to start looking up such new information through official documentation or just a simple search. For example, numpy's logical_and specifies the following -

Thanks a lot, i don’t know how I missed that part from the documentation :sweat_smile:

1 Like