manyspikes

Comparison operators

Initialising environment...

Booleans are quite useful because they can represent represent the result of a comparison between values. For instance, the following comparison operators return a boolean which determines if the comparison is true or false.

print(1 == 1) print(2 > 3)

which prints

True
False

Here is a list of comparison operators in Python:

  • <: less than
  • <=: less than or equal
  • >: more than
  • >=: more than or equal
  • ==: equal
  • !=: not equal
  • is: same object identity
  • is not: different object identity

Note: The difference between is and == is that is requires the objects to be the same, not just the values. We will see examples of this later in this module.

Instructions

Create the following variables:

  • n_points and set it to 5
  • n_min_points and set it to 0
  • n_max_points and set it to 100

Finally, create a variable called is_in_range and set it to the result of applying the comparison operators to check if n_points is between n_min_points and n_max_points (i.e. greater or equal than the minimum and smaller or equal than the maximum).

Note: You will need to use the and boolean operator (see previous items).