Get Started

Compare arrays

Out of the box, you can also use comparison operators with NumPy arrays.

Remember areas, the list of area measurements for different rooms in your house from Introduction to Python? This time there's two NumPy arrays: my_house and your_house. They both contain the areas for the kitchen, living room, bedroom and bathroom in the same order, so you can compare them.

This is a part of the course

“Intermediate Python”

View Course

Exercise instructions

Using comparison operators, generate boolean arrays that answer the following questions:

  • Which areas in my_house are greater than or equal to 18?
  • You can also compare two NumPy arrays element-wise. Which areas in my_house are smaller than the ones in your_house?
  • Make sure to wrap both commands in a print() statement so that you can inspect the output!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create arrays
import numpy as np
my_house = np.array([18.0, 20.0, 10.75, 9.50])
your_house = np.array([14.0, 24.0, 14.25, 9.0])

# my_house greater than or equal to 18


# my_house less than your_house

This exercise is part of the course

Intermediate Python

BeginnerSkill Level
4.6+
772 reviews

Level up your data science skills by creating visualizations using Matplotlib and manipulating DataFrames with pandas.

Boolean logic is the foundation of decision-making in Python programs. Learn about different comparison operators, how to combine them with Boolean operators, and how to use the Boolean outcomes in control structures. You'll also learn to filter data in pandas DataFrames using logic.

Exercise 1: Comparison OperatorsExercise 2: EqualityExercise 3: Greater and less thanExercise 4: Compare arrays
Exercise 5: Boolean OperatorsExercise 6: and, or, not (1)Exercise 7: and, or, not (2)Exercise 8: Boolean operators with NumPyExercise 9: if, elif, elseExercise 10: WarmupExercise 11: ifExercise 12: Add elseExercise 13: Customize further: elifExercise 14: Filtering pandas DataFramesExercise 15: Driving right (1)Exercise 16: Driving right (2)Exercise 17: Cars per capita (1)Exercise 18: Cars per capita (2)

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free