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

Using and in Boolean while Loops

00:00 In this lesson, you’ll see an example of and used in the condition for a while loop. And as you’ll see, it’s another case of any time you have two expressions that you need to be true to continue executing, you’ll want to use an and operation.

00:16 In this example, taken from the tutorial, you see a section of code that deals with the control system for some environment. This segment looks at the pressure within that system and calls specific functions based on what that pressure is.

00:30 You won’t be to see this in action since we don’t have the other functions which supply the value for pressure, but you can still dissect this and see what’s supposed to happen.

00:39 This segment is supposed to run until the pressure in the system is at or below 500 units, likely kilopascals. At those pressures, the system is considered safe and further checks aren’t necessary.

00:51 There are three while loops in this program, but it’s the second one that has the and operation in its condition. If the pressure gets a little too high, a function called run_standard_safeties() needs to be used to try to reduce the pressure. The pressure range to use this function is from 500 to 700.

01:11 So just as in the previous lesson, you need two expressions connected with the word and to specify the exact condition necessary to use the standard safeties function For completeness, let’s briefly look at the other two loops.

01:26 The final loop is used when the pressure gets way high, over 700 kilopascals, where a more critical safeties function is used. And the first loop simply stops the repetition any time the pressure is considered safe.

01:41 In summary, the point of using and is when you have two expressions that have to be true for either selection or repetition. Very often these involve ranges, but you’ll see in future lessons other situations.

01:56 The first of which is using an and operator outside of an if or while statement.

Become a Member to join the conversation.