- [Narrator] In this video, we're going to talk a
little bit more about the if statement
before we begin looking at its alternative versions.
Now, back in the preceding lesson we demonstrated
the basic functionality of the if statement.
Let's do that again here by first declaring a variable
called "grade" and we'll give it the value of 85,
and let's write an if statement that's going to check
whether the grade value is greater than or equal to 60,
which when you're taking a course is normally an indication
that you've passed the course, and in the body or the suite
of that if statement, let's go ahead
and simply display the string passed,
if in fact the grade is greater than or equal to 60.
Now, based on what we define the grade to be up above,
this condition will be true and as I execute this statement
it's going to show me the word "passed."
Now, before I continue, I just want to point out here
that when you press enter on an indented suite line
inside of a control statement like the if statement,
it assumes that you'd like to continue defining the body
or the suite of that statement.
So in order to actually execute the code,
I now have to press enter once again,
and at this point I can see
the string "passed" gets displayed.
Now, we started to briefly mention
in the preceding lesson this issue of indentation in Python.
Unlike most C-based languages,
which typically use curly braces,
like the left curly brace and the right curly brace
that I just typed here,
to delimit the body of a control statement
like the if statement, in Python, the suite
of an if statement, and similarly other control statements,
is defined by indentation only.
The default indentation level is four spaces,
and that's what I got here automatically
when I simply pressed enter while typing the code
in the context of the interactive I-Python interpreter.
If you're working in an integrated development environment,
then you'll typically have four spaces by default,
although that is configurable,
and there's nothing that says you have to use four spaces.
What Python does require, however,
is that whatever indentation level you choose,
and whether you choose to use spaces or tabs,
you have to do that uniformly
for everything in a given suite.
So, if, for example, I recalled the statement
that I just did, and, let me go back up to the print line
and get rid of the indentation.
So when you're defining an if statement,
it expects its suite to be indented.
This is actually an error in Python,
and to demonstrate that, let me go ahead and press enter,
and now you can see that I have an indentation error
and it's telling me in this statement
that was not indented properly.
Now, similarly, going back into my if statement here,
let me put the four spaces back in,
if the indentation is not consistent,
that also is a problem.
So, for example, in this first suite statement,
I have four spaces before the print.
On the next line, I've removed two of those spaces,
and let's create another print statement that says,
"Good job!" and if I now go ahead
and try to execute this statement, once again,
I get an indentation error, this time pointing
to the "Good job!" line and the reason
is there are only two spaces in front of print
in this case as opposed to four spaces up above.
So, not only do you have to maintain the consistency
of your spacing, the interpreter is going to check
that spacing for you, and it turns out,
there's also static code analysis tools that can scan
larger code files for things like this, as well.
So, that's the basics of the if statement.
Now, another thing I want to point out
about the if statement is the fact
that the condition can be any expression
that evaluates to true or false,
and it turns out that in Python everything has a value
of true or false associated with it.
So for example, if I go ahead and say,
"If one," any value that's nonzero is going
to be treated as true, and zero is treated as false.
So, if I say, "If one," and I want
to do a print statement here that simply says,
"Nonzero values are true, so this will print."
So we got a little if statement there.
I expect to see the print happen in this case
because one is considered to be true,
so when I execute that, sure enough it does execute.
On the other hand, if I use zero,
which is false by default in Python,
and I do a little print statement and say,
"Zero is false, so this will not print."
You can see that indeed nothing is displayed
as a result of that condition.
Now as we move forward through subsequent lessons,
we're going to see that more complex objects
also can be evaluated as true or false.
So for example, a string that contains characters
would be considered true, and an empty string
that contains no characters would be considered false,
and similarly, when we get into
the various Python data structures,
which we'll start to do here in this lesson,
they, too, can be evaluated that way.
So, a collection of items that has elements in it,
has items in it, like a collection of random integers,
that would be considered true, whereas an empty collection
would generally be considered false.
Now, there's one other thing
that I want to point out about conditions
specifically with the double equals operator.
If we go and write an expression like grade is assigned 85,
which is what we did up above,
that is going to assign the value 85 to the variable grade.
Now, before I execute that let me get rid of that.
Let's assume grade already exists,
which it does up above we defined it.
So if I go and write by accident,
"Grade double equals 85," you can see that I get a result
of true because if I scroll back up here,
you can see that indeed, grade was 85.
So, if I mistakenly accidentally typed two equal signs,
then I would get a boolean value.
Similarly, if I mistakenly was trying to assign a value,
and let's say we made it 58.
In this case, I get back false,
when what I really wanted to do
was change the value of grade,
so of course I need a single equal sign
in order to be able to do that,
and now grade has a new value associated with it.
Another potential logic error, would be,
let's say I want to compare "X" to 85,
and I have not defined "X" yet,
in that case as we demonstrated once before,
you get a name error because the name "X"
does not yet exist in this interactive session.