Using not to Determine Range
00:00
Here’s another example of when you might use not
in an if
statement condition. Determining if a number is inside or outside a particular range of values is a very common task.
00:15
And writing a condition to see if a number is inside a range is pretty straightforward. You usually use a compound inequality using the word and
. To be in the range 20
to 40
, including the end point of 20
, a number has to be at least 20
and at the same time, less than 40
.
00:54
And 30
is indeed inside that range. This type of comparison is so common that there are other ways to write this condition. And you’ll see at least one of those in an upcoming lesson.
01:06
But now suppose you want to check if a number is outside that range. What would that condition look like? Well, the easiest way is to just put not
in front of this condition.
01:27
If you say not
followed by the previous condition, this will now be True
when a number is outside of that range. Remember you want to do the and
first, so that part of the condition has to be put in parentheses.
01:51
And 50
is outside of that range. Next, you’ll see how not
can be used in the condition for while
loops.
Become a Member to join the conversation.