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 your subtitle preferences 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 refer to our video player troubleshooting guide for assistance.

Conditional Execution: if Statements

00:00 Now let’s take a look at the or operator in action, beginning with if statements.

00:06 An if statement chooses one or another branch of code to execute based on the value of a condition. We can use an or operation in a condition, for example if we’re asking the user a question and there are several ways he or she could provide an answer that we want to accept for one particular path or another.

00:30 So, if we look in this top function called answer(), we’re going to ask the user a yes or no question. We’re going to allow that the user might type the word yes or might just type the letter y. We want both of those to be acceptable, so we’re going to produce a lowercase version of the string so that we don’t have additional or statements, although that’s certainly possible.

00:56 So what we’re going to do is we’re going to see did they type, when converted to a lowercase string, the word 'yes'? Or did they type a 'y'? And we’ll allow both of those to indicate an affirmative response.

01:11 And then whatever action we want to take within this block, we can carry out. Here, we’re just using a simple print statement for illustrative purposes. Otherwise, we might want to check if they said 'no'. And again, we’ll allow two possible ways that they could say no—either they typed the word no or they just typed the letter n. I don’t know of other things we might anticipate a user typing, but if you can think of those, you can add additional or operands and more expressions to check for either of these. But if they said 'no' or 'n', in this particular function, we’re just going to take whatever action we want for a negative response, and here we’re just going to print out that they replied with a negative answer.

01:57 And if they didn’t reply yes or no, then we’re just going to ignore that. Again, in an actual program, you might have something more meaningful for them to do.

02:08 If you want to see this in action…

02:18 I run the function answer(), and Do you...? Well, I can type yes and it registered that we had a positive answer.

02:30 I can call that function again and just give a y. And because it’s in the or statement and that makes the second operation True, the whole thing evaluates to True and we record a positive answer.

02:45 And again, if I say no

02:49 or n, because neither one of these is 'yes' or 'y', it goes to the elif part, and now it’s checking to see if it was 'no' or 'n'. And since it was 'n' and that makes the second or operation True, we go down to the negative answer. And if we respond in some other way, maybe, there’s nothing to check for that

03:17 and so the function doesn’t do anything for us. Another place where the or might be used is if you want to check if a number entered is outside of a particular range.

03:34 So, entering a number between 5 and 10 would not be useful, so we want them to enter a number less than 5 or a number more than 10. And that sentence that I just read used the word “or” in it, and that should also suggest that we would want an or operation to control that logic.

03:58 And so here is another illustrative example that we want to determine if the user has typed a number inside or outside of the range from 20 to 40. And so if they type a number and that number is less than 20 or that number is greater than 40, we’ll observe that we are outside of our range, and otherwise we are inside of our range. And here the way we’ve defined these is that the endpoints are inside of the range.

04:28 Whether we use a less than (<) or less than or equal to (<=), greater than (>) or greater than or equal to (>=) in these operations is something specific to the actual problem and has nothing to do with whether the word or is being used here.

04:45 So, if I say my_range(),

04:50 and it wants a number, and so if I say 17, it’s going to return True because 17 is less than 20that means the first operand in our or statement is True.

05:04 We don’t need to evaluate whether it’s greater than 40, because we already know the result of this operation is going to be True and it’s going to print for us 'Outside'.

05:18 Similarly, if I type in a larger number, say 80, that’s going to make the first inequality False. So then we check the second condition, and 80 is greater than 40, and so the entire or statement evaluates to be True and we will get the result that we are outside of that range.

05:42 On the other hand, if I type a number that is in the range, say 35, the first condition evaluates to False35 is not less than 20—but 35 is also not greater than 40, and so this or statement has the result False, and so we go into the else branch and our program is going to print 'Inside'. And one more, because we used < and > and not <= or >= in the inequalities in the if statement, if either one of the numbers is equal to 20 or 40, it’s still going to return False, and we are inside that range.

06:31 In the next lesson, we will take a look at a while loop using a condition with the Boolean operation or.

Become a Member to join the conversation.