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
course content

Conteúdo do Curso

Ultimate NumPy

Ultimate NumPy

1. NumPy Basics
2. Indexing and Slicing
3. Commonly used NumPy Functions
4. Math with NumPy

bookMore 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:

1234567891011121314
import numpy as np # Creating an array of integers from 1 to 10 inclusive array = np.arange(1, 11) # Retrieving elements greater than or equal to 5 AND less than 9 print(array[(array >= 5) & (array < 9)]) print('-' * 12) # Retrieving elements less than or equal to 4 AND not equal to 2 print(array[(array != 2) & (array <= 4)]) print('-' * 12) # Retrieving elements less than 3 OR equal to 8 print(array[(array < 3) | (array == 8)]) print('-' * 12) # Retrieving elements between 2 inclusive AND 5 inclusive OR equal to 9 print(array[(array >= 2) & (array <= 5) | (array == 9)])
copy

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

Tarefa

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 8
toggle bottom row

bookMore 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:

1234567891011121314
import numpy as np # Creating an array of integers from 1 to 10 inclusive array = np.arange(1, 11) # Retrieving elements greater than or equal to 5 AND less than 9 print(array[(array >= 5) & (array < 9)]) print('-' * 12) # Retrieving elements less than or equal to 4 AND not equal to 2 print(array[(array != 2) & (array <= 4)]) print('-' * 12) # Retrieving elements less than 3 OR equal to 8 print(array[(array < 3) | (array == 8)]) print('-' * 12) # Retrieving elements between 2 inclusive AND 5 inclusive OR equal to 9 print(array[(array >= 2) & (array <= 5) | (array == 9)])
copy

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

Tarefa

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 8
toggle bottom row

bookMore 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:

1234567891011121314
import numpy as np # Creating an array of integers from 1 to 10 inclusive array = np.arange(1, 11) # Retrieving elements greater than or equal to 5 AND less than 9 print(array[(array >= 5) & (array < 9)]) print('-' * 12) # Retrieving elements less than or equal to 4 AND not equal to 2 print(array[(array != 2) & (array <= 4)]) print('-' * 12) # Retrieving elements less than 3 OR equal to 8 print(array[(array < 3) | (array == 8)]) print('-' * 12) # Retrieving elements between 2 inclusive AND 5 inclusive OR equal to 9 print(array[(array >= 2) & (array <= 5) | (array == 9)])
copy

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

Tarefa

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

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:

1234567891011121314
import numpy as np # Creating an array of integers from 1 to 10 inclusive array = np.arange(1, 11) # Retrieving elements greater than or equal to 5 AND less than 9 print(array[(array >= 5) & (array < 9)]) print('-' * 12) # Retrieving elements less than or equal to 4 AND not equal to 2 print(array[(array != 2) & (array <= 4)]) print('-' * 12) # Retrieving elements less than 3 OR equal to 8 print(array[(array < 3) | (array == 8)]) print('-' * 12) # Retrieving elements between 2 inclusive AND 5 inclusive OR equal to 9 print(array[(array >= 2) & (array <= 5) | (array == 9)])
copy

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

Tarefa

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.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 2. Capítulo 8
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt