Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
More about Comparisons | Indexing and Slicing
Ultimate NumPy

More about Comparisons More about Comparisons

Most conditions you will use are comparisons, so it's important to discuss them in more detail. Comparisons are based on the following comparison operators:

  • > (greater than);
  • < (less than);
  • >= (greater than or equal to);
  • <= (less than or equal to);
  • == (equal to);
  • != (not equal to).

Moreover, you can combine multiple conditions and comparisons using the following logical operators:

  • & (logical and);
  • | (logical or).

If at least one of the conditions is True, then | returns True; otherwise, it returns False. If at least one of the conditions is False, then & returns False; otherwise, it returns True.

Note

Each condition should be put in parentheses () when combining them.

Don’t worry, here is an example to make everything clear:

Let's now take a look at the following visualization to understand the code better (purple squares represent the actual retrieved elements):

Combining comparisons

Завдання

You are analyzing the ratings of various products collected from customer feedback. The ratings are stored in a 1D NumPy array where each element represents the rating of a product. Your task is to filter out the ratings that are greater than or equal to 3 AND not equal to 5 using boolean indexing.

Все було зрозуміло?

Секція 2. Розділ 8
toggle bottom row
course content

Зміст курсу

Ultimate NumPy

More about Comparisons More about Comparisons

Most conditions you will use are comparisons, so it's important to discuss them in more detail. Comparisons are based on the following comparison operators:

  • > (greater than);
  • < (less than);
  • >= (greater than or equal to);
  • <= (less than or equal to);
  • == (equal to);
  • != (not equal to).

Moreover, you can combine multiple conditions and comparisons using the following logical operators:

  • & (logical and);
  • | (logical or).

If at least one of the conditions is True, then | returns True; otherwise, it returns False. If at least one of the conditions is False, then & returns False; otherwise, it returns True.

Note

Each condition should be put in parentheses () when combining them.

Don’t worry, here is an example to make everything clear:

Let's now take a look at the following visualization to understand the code better (purple squares represent the actual retrieved elements):

Combining comparisons

Завдання

You are analyzing the ratings of various products collected from customer feedback. The ratings are stored in a 1D NumPy array where each element represents the rating of a product. Your task is to filter out the ratings that are greater than or equal to 3 AND not equal to 5 using boolean indexing.

Все було зрозуміло?

Секція 2. Розділ 8
toggle bottom row
some-alt