-: Hi guys, and welcome back.
In this video we're going to learn about if statements.
An if statement allows us to use a Boolean
to make a decision in our programme
and potentially do different things,
depending on the value of that Boolean.
Of course, if statements are most useful
when you have user input,
such that your programme can potentially get different data,
depending on who runs it and who's using your programme.
So here we've got an input that asks the user
for the day of the week,
so what day of the week is it today?
And then, we're going to check
whether the current day of the week is Monday.
So, how would you check
whether the current day of the week is Monday?
You would say day_of_week == the string "Monday".
Let's print this out to see whether our Boolean works.
So, all I've done is printed the Boolean out.
And again, remember that this here
will evaluate first, so Python will say,
"Day of week, is it equal to Monday?"
And if it is, then it will replace
this entire thing by "true."
Otherwise, it will be replaced by "false."
And, if we run this,
you see that we get our prompt,
and you can type in "Monday,"
and then you get True.
But, if you type "Tuesday,"
then you get False.
So this is what we expected.
Now, instead of printing this out,
we can actually put it in an if statement.
So the if statement is like this,
you type in your if keyword, which is special in Python.
Then you put a space, and then,
without putting brackets or anything,
you type your Boolean comparison.
And then at the end you put your colon.
This will evaluate as soon as the if statement runs,
so as soon as Python reaches line 3,
it's gonna calculate the result of this Boolean,
which is gonna be true if the user entered "Monday,"
and false if they didn't.
And if it is true,
then we will run the code under the if statement.
And here's one of the most important things
in Python programming,
which is that there are a few spaces
in front of this line here.
This is called indentation in Python,
and after a colon that defines a block of code,
such as an if statement or a function or a class,
we're gonna learn more about those later on,
you need to put indentation.
That is how Python tells which code
is inside the if statement,
i.e. which code you want to run when this is true.
So, for example, you can print
"Have a great start to your week!"
Now, this is the line of code
that is inside the if statement.
If this is true, this line of code will run,
because it's got these four spaces in front.
I'll say at this point that
you don't have to use four space in Python,
you can use two, you can use six, or eight,
whatever you want, but be consistent.
So, always use the same number of spaces
that's gonna make your code work,
otherwise it may not work,
and also it's gonna make your code look much better.
If you've got different indentations
in different parts of your programme,
people are gonna be really confused
when they look at your code.
I recommend using four spaces for Python code.
Now, if you print something and you remove the indentation,
the spaces in front,
for example, like this, what you're gonna get
is that this code is always going to run,
if this is true, or if it is false.
Because it's not in the if statement,
it's completely unrelated to the if statement.
So, Python will run this code if it's true,
it will run this code.
If it's false, it will not run this code,
and then it will jump to the next line
and it will continue running from here.
So, I'm gonna run this down here again,
and I'm gonna type "Monday,"
and then you see that we get,
"Have a great start to your week!"
and, "This always runs."
But, if we type in "Tuesday,"
then you just see "This always runs."
and you don't see the "great start to your week".
If you want to print something
if the day of the week is not Monday,
then you can do something like this:
If day_of_week is not Monday, then print
"Full speed ahead!"
Let's run that, and then I'll type "Monday,"
and you get "Have a great start to your week!"
But, if we type "Tuesday,"
then you see "Full speed ahead!"
Now this is actually not great Python code,
because you have two checks
that will always be evaluated by Python.
Here you've got day_of_week == "Monday"
and this will always be checked,
whether it is true or false, by Python.
And then you have day_of_week is not equal to "Monday"
and this will always be checked by Python,
regardless of whether this one was true.
Evidently, if this one is true,
this one cannot be, and vice versa.
So, Python provides you a very nice piece of syntax
called the else syntax.
And what this does is,
if this is true, then we run this code.
Otherwise, run this code.
So this is much nicer and also is much easier to read.
Let me say at this stage,
that only one of these two will run.
You cannot possibly run both,
because you can't have this be true
and also false at the same time.
In addition, if you wanted to print something else,
if the day of the week is Tuesday,
you can do elif day_of_week == "Tuesday"
And what this does is it creates a chained if statement,
an if statement with three different branches.
First, Python will check
whether the day of the week is Monday,
and it will print this if it is true.
But if it's not true,
then Python will check
whether the day of the week is Tuesday,
and it will print this if it is true.
But if neither of those are true,
then it will go to the else.
An important part of this syntax here is that
if the day of the week is Monday,
then Python will print this stuff,
and it will ignore the rest.
So it will never calculate this comparison.
So this is actually good for performance,
and also it lets you have a comparison here
that when it evaluates, or when it's true,
it ignores the rest.
So there's actually a lot of stuff you can do
with this knowledge,
and you'll encounter that plenty
while you're doing Python programming.
A common problem when you are dealing with user input
is that they may not enter exactly what you expect.
For example, it is quite common
for a user to enter all lowercase letters
when you ask them for something,
just because they can't be bothered
typing the uppercase letters.
So here, the user has typed "monday"
all in lowercase, and then we have printed out
"Full speed ahead!" because the day of the week
is not exactly equal to the string "Monday"
with a capital M.
So, you have to do something about it
if you want your programme to be able to detect
whether the day of the week is Monday
in uppercase and in lowercase.
What I'd recommend is that you get the user's input
and just after you type .lower
And then, this is going to turn this entire string
into it's lowercase variant.
Now, if they enter Monday with the capital M,
then it is also gonna be turned into monday
with a lowercase m,
which means that monday =="Monday"
with an uppercase M will not match.
So, instead what you should do
is turn everything to lowercase.
By turning the user's input into lowercase,
and your checks into lowercase,
you're always going to have the same case,
because they're both lowercase.
So if we save that and run it,
you'll see that now I can type "monday,"
but I can also type "MONDAY" all in uppercase,
and I can also type "Monday" as usual.
So this is a good way of insuring that your programme
will work, no matter what type of case the users type.
Let's leave it at this,
thank you for joining me in this video,
I hope it's been useful,
and I'll see you in the next one.