In this video I'm gonna explain how to write simple conditional statements
using Python. A conditional statement is simply a statement that you write where
you check against a condition to execute a certain piece of code. So you basically
say "if this value equals this then execute this, otherwise execute this". So
these are called if statements and I'm going to show you how to write a simple
if statement using Python. Let's go ahead and open up our project using the Atom
editor so we'll open up the project that we had in the previous lesson where we created
our variables and our hello world file and we're gonna create a new file called
conditions.py and in here we're gonna learn how to make a simple if
statement. Before we write an if statement I'm gonna explain the two most
basic conditions that you might use in your Python code. I'm going to use
comments to explain how to use the different conditions so a
comment is just a line preceded by a hash symbol and all this does is tells
python "do not execute this line of code", it's just a bit of text for
informational purposes. Okay so the first condition is the equals so you check if
one value equals another value. Now because we use the single equals
operator to assign a variable the way that you need to do an equals if you
are comparing values is you do two equals. So if you say equal equal then this is a
comparison, you are checking one side of the equals equals the other. The next is
the "does not equal" so the not equals, the way that you write that is you do exclamation
mark and equals so this is if does equal and this is does
not equal. Okay so these are the two most simple operators that you would use in
Python. There are more operators that you can use but we'll cover them in later
videos. Okay now I'm going to show you how to form a simple if statement using
Python. Python uses indentation to separate the different blocks of code. So
indentation is just when you write...it's just how many spaces you put before the
line of code you write. So the way you do an if statement is you type if and then
the condition, so let's say we want to check if one equals equals one...one not
two... so if one equal equals one then you do a colon and then you write the code
that you want to execute if this is true so let's just do a simple print and
we'll just print one equals one. Save that and let's run our code, so open up
your command prompt or your terminal window and then if you remember from the
previous lesson the way that you run it is you type Python and then the name of
the file you want to execute, we'll run that. Now, of course one equals one we already
knew that. I'm just trying to demonstrate how you write an if
statement. So this is the most simple if statement here. So...an if statement
can have three components to it. The first is the the first check that you
want to do. Then you write any secondary checks that you want to do so that's
called 'else if' and then you write an else statement.
So it's better if you compare... if we demonstrate by comparing a variable so
you don't actually need to specify the numbers because this wouldn't be much
use but where it becomes useful is when you check the condition of a variable
that may be set elsewhere in the code. So let's define a variable called
apples and let's set it to seven and let's change our if statement to compare
if apples equals seven. So we'll remove this text here and we'll just write if
Apple's equals seven then print apples is seven. Save that. Run it and you can
see it outputs apples is seven. So next let's write an else statement so let's
say if apples equals seven do this, otherwise execute this.
Let's type else print apples does not equal seven. Alright so of course
apples does equal seven so let's just change this to eight, so if we set apples
to eight and then we print this it will say apples does not equal seven. Okay so
now we can try inverting this using the not equals. So if we say if apples
does not equal seven, apples does not equal seven. And if this is true, so
if it's not equal seven then print apples is not equal seven otherwise
print apples does equals seven, or equal seven (not equals*). And save and
run and you can see apples does not equal
seven. We change it to seven, it says apples does not equal seven...or sorry it says Apple
does equal seven. Yeah I'm confusing myself here because it can get quite confusing
when you work with negative operators like this.
Okay so let's change this back to double equals. So we'll check if apples equals
seven
and let's add an 'else if'. The 'else if' is a secondary condition that we check with
or we check against underneath this before we reach the else statement. So
let's type elif: the syntax for writing an else if in python is you simply type
elif. They shortened it and just merged them together
I think they did that just because it's cleaner than writing something like
else if or it's shorter. You just type elif and then the secondary condition that we
want to check. Just wanna check if apples equals 8 and then colon and then the
line of code underneath that we want to execute if this is true: apples equal 8
change this to just apples equals 7 and then our if statement now should or our
else statement should become apples does not equal 7 or 8. And you can add as many
of these if statements as you want so you can go and write elif
apples equals 9 and you can go 10 and so on
we're just going to keep the 2 here just to demonstrate it working in its
simplest form and let's run this. It says apples equals 7, if we change it to 8 it
says apples equal 8 if we change it to 56
it says apples equal does not equal 7 or 8. Okay so that's how you formulate a
simple if statement in Python. I hope you found this useful? Thank you so much for
watching and I'll see you in the next video.