Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
If This Then That5:03 with Kenneth Love
Comparisons let us start to apply logic to our code. If we can compare two things, we can cause different actions depending on how that comparison turns out.
if blocks always start the same way:
if a < b: print("A is tiny!")
It's always the keyword
if followed by the condition. In this case, the condition is
a < b but your conditions will be different. Then comes a colon (
:), a return, and the body of the
if is indented.
If you want something to happen when the condition is
False, you'd put that into an
else: block, which is just the word
else followed by a colon.
If you need another possible condition, use the
elif block, like so:
elif a == b: print("A and B are equivalent!")
elif block is built exactly like the
if block but with
elif instead of
if. All of your
elif blocks must come before any
Any programming language that you would want to use has a construct
known as an if statement.
Usually, it's the word if followed by some kind of condition,
like money is greater than cost of apples and
then a block of code describing what to do if the condition is true.
If there's an if, usually there's an else too.
The else would be another block that what to do, if the if's condition isn't true.
Python is no exception and indeed, does have an if.
We also have an else and another handy one named, badly, I might add, elif.
Let's look through all of these in workspaces.
Most of the time if we're comparing things, we're doing that because we
want to do something when the comparison is true or false or whatever.
We can do that with the if statement.
So for example if 5 > 2 and you notice when I'm typing in a command,
I've got these three chevrons and then I started a block.
And so it gives me these ellipses, cuz it knows that something comes next.
So one, two, three, four.
You don't have to do four, but I like to.
Then we print("Well, yeah"), because obviously 5 > 2.
Press return twice and we get out, Well, yeah.
That maybe wasn't the best example.
This ability comes in most handy when you're dealing with things that you're
calculating as your script runs, like maybe you're calculating the number
of days you've been alive and you wanna print out a message once you pass 10,000.
Maybe a little generic one that.
We'll say, age = 34 * 365.
Assuming there´s always 365 days in a year, which I know there's not.
So if age > 10,000 days, then their gonna print("Wow,
over 10,000 days old!") and that's what I get back.
And yeah, that's a lot of days.
Okay, but what if you're less than 10,000 days old?
Let's do a statement for that case too.
Let's set the age = 5,000, just a nice round number there.
If age > 10,000: print("Wow,
over 10,000 days old!").
Else: print("Keep going!
You'll get there!") and
we get the keep going you'll get there message.
So the else runs if the if isn't true.
Now this is pretty similar to how we'd give someone instructions in English.
We'd be like, hey, if it's after 5 PM,
turn the oven on to 325 degrees Fahrenheit.
For that, there's an implied else.
Which says, otherwise, leave it off.
Now, what about times when we need to handle multiple conditions?
Let's go back to our age thing.
Let's set the age to 22,000 days, that sets my age to right around 60 years.
So if age > 20,000:
print("Time to retire!").
Elif age > 10,000: print("Lots of time left!").
And otherwise, if neither of those conditions is true, then let's print out
("Time to get started!") and it prints out time to retire.
The elif, let's us test a second condition from the first if and
we can have as many elif statements as we want.
So if we need to test a bunch of different conditions, we can.
To add a little bit more functionality to this, I am gonna resize this again.
If we wanna get a little bit more functionality in this,
sometimes you want to test a negative.
You wanna check that a condition is false instead of if a condition is true.
For that, we can use the not keyword, and that basically flops our result.
So we'll say, if not age > 20,000 and
let's say, 25,000.
We're gonna print("Whippersnapper"),
which is like one of my favorite words and it prints up whippersnapper.
Being able to negate condition checks is often really, really handy.
We're gonna do it a little bit more in this course too.
And hey, congratulations.
The if statement is our first bit of programming logic and
logic is the second step to writing real useful software.
The first was all of those data type apps we've been going over.
Between data types and this first bit of logic,
you should be able to start writing some basic software.
In our next video, we're going to learn another handy bit of Python logic.
You need to sign up for Treehouse in order to download course files.Sign up