Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Checking Numeric Ranges

00:00 In this lesson, you’ll see another example of using and to determine if a value is within a numeric range. Plus, see a feature of Python which can simplify these types of expressions.

00:12 As you’ve seen before, and is frequently used to connect conditions which describe the lower and upper limits of a range of numbers. To be between 0 and 10 inclusively, a number must be at least 0 and less than or equal to 10.

00:27 This file, called range.py, has this function defined, using default values for the lower and upper limits, but allowing specific ones to also be provided. You can see how it works.

00:41 After importing it,

00:45 you can call it a few times. 5 is greater than or equal to 0, and it’s less than or equal to 10. Since both our expressions are true, the and of them is True, and this function will return True.

01:03 20 is greater than or equal to 0, but it is not less than or equal to 10. So that part is false, making the and of both of them False, so this function returns False.

01:16 You can test if a number is in a different range by supplying values for those parameters. In this case, 20 is greater than or equal to 10, and it is less than or equal to 40, so the whole thing is true.

01:34 and is used when you want to check if a number is within a certain range. However, if you want to check if a value is outside a given range, you have to use or. If a number is not within the range of 0 to 10 inclusively, then it’s either less than 0 or greater than 10.

01:55 It can’t be both. So you have to use or, where the or of two expressions is True if at least one of those expressions is true.

02:04 There is a tutorial and a video course on how to use Python’s or operation here at Real Python. As you’ve seen, these types of and conditions are quite frequent.

02:15 So Python has actually defined a simpler syntax when you want to use one. Perhaps you recall from math that the compound inequality 0 ≤ x and x ≤ 10 can be shortened to 0 ≤ x ≤ 10.

02:34 You can do this in Python as well. The expression 0 <= x <= 10 is valid in Python. So I could replace the and operation in this function with start <= number <= end. Save this, restart the REPL, and import it again.

03:11 And the previous interactions should work the same.

03:28 And they do. Please notice that most other languages do not allow this syntax. Next, and finally, you’ll look at how and can be used to chain function calls where one function call actually depends on the success of a previous one.

Become a Member to join the conversation.