Locked lesson.
About this lesson
What are the logical operators AND/OR/NOT and how do we use them?
Exercise files
Download this lesson’s related exercise files.
Multiple Conditionals.docx60.4 KB Multiple Conditionals - Solution.docx
59.1 KB
Quick reference
Multiple Conditionals
It's possible to reference multiple conditional statements inside an if statement.
When to use
Use this whenver you need to make more than one type of comparison in an If statement.
Instructions
You can make multiple comparisons inside an IF statement using "and" & "or":
if (5 > 3) and (5 == 5):
print("Hi there")
if (5 > 3) or (5 == 5):
print("Hi there")
For "and" both conditions must be true for the thing to evaluate to true.
For "or" only one of the conditions must be true for the thing to evaluate to true.
Hints & tips
- Use and/or to test multiple comparisons
- 00:04 In the last video, we looked at conditional statements,
- 00:07 in this video I want to look at multiple conditional statements using and and or.
- 00:11 So we already sort of have multiple conditional statements right here by using
- 00:16 this elif.
- 00:17 So this is our first conditional statement.
- 00:19 This is our second conditional statement.
- 00:21 So that's definitely multiple conditional statements.
- 00:23 But sometimes you want to run multiple conditional statements up at the top
- 00:28 before you even go down and evaluate anything else.
- 00:31 So you do that using these logical operators and and
- 00:35 or, lowercase, and and or.
- 00:37 So let's go ahead and get rid of this and let's just start over up here.
- 00:42 So to us and and or, you just type and and then pass in whatever else you want to do.
- 00:47 So let's say, num < 100.
- 00:50 So if we save this and run it, nothing happens.
- 00:54 Because num is 3, 3 is not greater than 5, and
- 00:57 even though our next statement, is 3 less than 100, yes, it is.
- 01:03 3 is definitely less than 100.
- 01:05 But for the logical operator and, both of these conditional statements have
- 01:10 to be true in order for this whole thing to evaluate true.
- 01:13 In which case, this will print out, otherwise the program just stops.
- 01:16 So we could change this, we could say, is num greater than 5?
- 01:20 We change it to 6.
- 01:22 Now this becomes true, is 6 less than 100?
- 01:24 Yes it is, so that becomes true.
- 01:26 And if we save this and run it, num is greater than 5, which is this.
- 01:31 So that's and.
- 01:33 You can use all of your comparison operators obviously, save this and run it.
- 01:37 It's no longer going to be true because 6 is not equal 100.
- 01:40 And you can mix and match any which way you want.
- 01:43 Now you can keep going, go and and and, and just keep slapping in more
- 01:46 conditional statements between the parentheses and it'll keep working.
- 01:50 Not quite sure why you would want to do that, but you can definitely do that.
- 01:53 So that's the logical operator and.
- 01:55 The next one is or.
- 01:57 So now if we save this and run it, we get, Num is greater than 5.
- 02:02 For or, only one of these has to be true.
- 02:06 They can both be true, but only one has to be true.
- 02:08 So in this case, 6 is indeed greater than 5.
- 02:12 6 is not equal to 100, so this one is false, but this one is true.
- 02:18 And like I said, with or, either one has to true for the whole thing to be true.
- 02:22 So that's sort of the difference between and and or.
- 02:25 So, let's do it again, is 5 greater than 5?
- 02:28 No, it's not.
- 02:29 Is 5 equal to a 100?
- 02:31 No, it's not.
- 02:31 If we save this and run it, nothing happens.
- 02:34 Because in this case, both of these are false, and so absolutely nothing happens.
- 02:38 So with or, one or the other has to be true.
- 02:41 And you can keep going, you don't have to use just two.
- 02:43 You can go or () or (), just like with the and, you can do as many as you want.
- 02:48 or (num == 1000), I don't know.
- 02:54 Doesn't work, right?
- 02:55 We if we change it to 6,
- 02:56 it does work because this is true even though this is false and this is false.
- 03:01 So no matter how many of them you have,
- 03:03 at least one of them has to be true in order for or to evaluate to true.
- 03:08 So lots of different circumstances, lots of different times you're going to want to
- 03:11 do this instead of using an elif statement.
- 03:14 And you'll see that as you move on and start to learn more about programming.
- 03:17 You'll see different times when you're going to be like, I should use and
- 03:21 there or an or there instead of an elif.
- 03:22 It'll make sense to you.
- 03:23 So those are multiple conditional statements.
- 03:25 In the next video, we're going to look at Python formatting.
Lesson notes are only available for subscribers.