🥳 GOSKILLS TURNS 10: Get 10 days of free access with code 10YEARS

GoSkills
Help Sign up Share
Back to course

Conditionals If/Else/Elif

Quiz me Quiz Compact player layout Large player layout
Focus video player for keyboard shortcuts
Auto
  • 720p
  • 540p
  • 360p
1.00x
  • 0.50x
  • 0.75x
  • 1.00x
  • 1.25x
  • 1.50x
  • 1.75x
  • 2.00x
cc

We hope you enjoyed this lesson.

Get the Introduction to Python course for more great video tutorials.

Start free trial

Cool lesson, huh? Share it with your friends

Facebook Twitter LinkedIn WhatsApp Email

  • Lesson resourcesResources
  • Quick referenceReference
  • Transcript
  • Notes

About this lesson

Using comparison operators to make decisions.

Exercise files

Download this lesson’s related exercise files.

Conditionals If/Else/Elif
59.1 KB
Conditionals If/Else/Elif - Solution
59 KB

Quick reference

Conditionals If/Else/Elif

Conditional statements allow us to compare things and make decisions based on those comparisons.

When to use

Anytime you need to make a decision, and take actions based on those decisions, you'll use conditional statements.

Instructions

The main conditional statements are If, If/Else, and If/Elif statements.

If statement:

if (3 < 5):

  print("3 is less than 5")

 

If/Else statement:

if (3 < 5):

  print("3 is less than 5")

else:

  print("3 is not less than 5")

 

If/Elif statement:

if (3 < 5):

  print("3 is less than 5")

elif (3 == 3):

  print("3 is equal to 3")

else:

  print("3 is not less than 5")

Hints & tips

  • Conditional statements let you make decisions and take actions based on those decisions
  • if, if/else, and if/elif
  • Tab spacing/indenting is important!!
Login to download
  • 00:04 In the last video, we looked at comparison operators.
  • 00:06 In this video, we want to take those comparison operators, and
  • 00:10 turn them into conditional statements that allow us to make decisions,
  • 00:13 do different things based on different conditions.
  • 00:16 The three main statements we're going to be looking are if, else, and elif.
  • 00:21 So let's just go ahead and create one of these, let's create a variable, and
  • 00:24 let's just call it num, and set it equal to 10.
  • 00:26 So now, let's create our conditional statement.
  • 00:28 Let's say, if num is greater than 5, and we have this colon, so
  • 00:32 this is our conditional statement.
  • 00:34 And you can see, we're using our comparison operator, greater than.
  • 00:38 So now, if this is true, we want to do something.
  • 00:42 So let's print, num is greater than 5, and that's it.
  • 00:48 So you'll notice right here, I tabbed over, this tab is important.
  • 00:53 So we're creating a block of code here, and
  • 00:55 everything after this colon that is tabbed over will get executed, if this is true.
  • 01:01 If it's not true, the program will drop down to the next line that's not indented,
  • 01:06 and continue on from there.
  • 01:08 So let's save this and run it, and we get num is greater than 5.
  • 01:12 So let's change this to 4, so now, is 4 greater than 5?
  • 01:16 No, that will evaluate to false, so what happens then?
  • 01:20 Nothing, because it skips this line, because this is false.
  • 01:24 So that is if, and you'll use this sometimes.
  • 01:26 But for the most part, when you're dealing with these conditional statements,
  • 01:30 you'll use an if-else statement in connection with each other.
  • 01:33 So what that does is it says, if something, do something,
  • 01:38 else do something else.
  • 01:39 So if this is true, do this, otherwise do something else.
  • 01:42 And to designate what that something else is, we just type in else and
  • 01:47 a colon and again, indent.
  • 01:49 And it has to be the same indenting, so you can't go like that,
  • 01:52 these things all have to line up.
  • 01:54 So we can print, num is not greater than 5,
  • 01:59 and let's wrap these in quotes, of course.
  • 02:04 So if we save this, our number is 4, 4 is not greater than 5.
  • 02:08 So we would expect this to print out, and it does, num is not greater than 5.
  • 02:13 We can change this back to 10, run it again, and
  • 02:16 we're going to get num is greater than 5, because 10 is greater than 5.
  • 02:20 And look at this flow, right, it starts up here.
  • 02:23 It says, if this is true, do this and then stop, don't do this, right?
  • 02:29 Now, otherwise, if we change this to 4, look at the flow again, it starts,
  • 02:34 is 4 greater than 5?
  • 02:35 No, it's not, so it skips this, and it goes right to this else, and
  • 02:39 then it evaluates, and does whatever we've put here.
  • 02:42 So that's if, that's if-else, the next thing is elif,
  • 02:46 and elif stands for else if.
  • 02:48 And this allows us to do other things, make other comparisons besides this.
  • 02:54 In this one right here, this if-else, we're making one comparison, right,
  • 02:58 we're saying, is 4 greater than 5?
  • 03:00 If yes, do this, if no, do that, and then it ends.
  • 03:03 We might want to test for something else, as well, so we would do that with an elif.
  • 03:07 And here we pass in another condition, another comparison operator.
  • 03:12 So if we said, if num == 4, and let's go print,
  • 03:18 num is 4, woo-hoo, right?
  • 03:22 We have a little error, we need a colon there.
  • 03:24 So every time you create a conditional, comparison operator,
  • 03:27 conditional statement, you have to put the colon after it.
  • 03:30 So let's save this and run it, and we get, num is 4.
  • 03:33 So let's look at the flow of this really quickly.
  • 03:36 We have number 4, is 4 greater than 5?
  • 03:38 No, it's not, so it drops down to the next elif statement.
  • 03:41 It runs this conditional, is num == 4?
  • 03:44 Yes it is, so it prints this out and then it ends, it does not do this.
  • 03:49 So if we change this to 3, for instance, and saved it and
  • 03:52 ran it, you see num is not greater than 5.
  • 03:55 So let's look at this, is 3 greater than 5?
  • 03:57 No, its not, so it skips this line.
  • 03:59 It runs this new conditional, is 3 equal to 4?
  • 04:03 No, it's not, so it skips this line, and then it ends with this else.
  • 04:06 You notice, a lot of programming languages,
  • 04:09 they end their if-else blocks with the word end, or something.
  • 04:12 Python does not do that, and the reason why is because of this indentation.
  • 04:16 It knows that when you stop indenting, if we were to say, print("Hello")?
  • 04:23 Python knows this print("Hello") is not part of this whole block,
  • 04:27 because it's not indented, right?
  • 04:29 So the indentation is very important, so keep that in mind.
  • 04:32 So those are conditional statements, very important.
  • 04:35 In the next video, we're going to look at multiple conditionals.

Lesson notes are only available for subscribers.

Comparison Operators
03m:58s
Multiple Conditionals
03m:35s
Share this lesson and earn rewards

Facebook Twitter LinkedIn WhatsApp Email

Gift this course
Give feedback

How is your GoSkills experience?

I need help

Your feedback has been sent

Thank you

Back to the top

© 2023 GoSkills Ltd. Skills for career advancement