Hey guy's R4GE VipeRzZ here and welcome back to episode 3 of my Python 3 series
so in this episode we're going to go over if statements so basically what if
statement does is it tests a condition and sees if it's true or false, now
depending on how you code an if statement you can make an if statement
run in a section of code if a condition is true or you can make an if statement
not run in a section of code if a statement is true, now before we start
creating any if statements we're first going to look at the comparison
operators that you can use in an if statement, so the comparison operators
that you can use are the equal to, not equal to, greater than, less than, greater
than or equal to and less than or equal to, now rather than me explaining each
one of these operators, it's probably better I just show you how they work
within an if statement, so to do that we're gonna first create a variable and
we're going to call this variable number, and we're going to set it equal to the
number five and as you can see I've not put any speech marks around the five and
that's because we want to store the actual number five in the number
variable and not the character five, so now we've stored the actual number five
in the number variable we're now gonna go down two lines and we're gonna create
our if statement, so to do that you want to type if and then space and the
condition that you want to test so we're gonna first test to see if the number
variable is equal to five, so to do that we're just going to type number
because that's the name of the variable, then space double equals for the equal
to operator and then space five because we want to see if the number variable is
equal to five, and then once we've finished writing out our condition we
then want to end it with a colon, now it's important that you end it with a colon
else if you don't then you will get a syntax error as Python won't know where the
if ends, so once you put a colon on the end of your condition you then
want to just press ENTER, and as you can see the cursor has now gone down a line
and it's also indented one, now it's important that it's indented one as the
code needs to be indented one for the if so that it knows that's the code it's
supposed to run when the condition is true
if the code was in line with the if, then the if statement wouldn't know what code
is supposed to run when the statement is true, now he shouldn't have to worry
about indent in it as long as you put the colon on the end it should indent
it automatically but if it doesn't just make sure that it is indented one, it
should be found anywhere but as long as you put the colon in the right place,
so now we can start typing out the code that we want to run when the statement
is true so I'm just going to type print, and then the number is five, so
now if we save the code and we run it, as you can see it prints the number is five
that's because when it comes into the if statement it gets a number from the
number variable and at the minute that is five so then it goes is five equal to
five well yes it is so then it runs this code here now if we change the number in
the number variable to ten, and then we save it and when you run it as you can
see nothing happens now that's because when it gets to the if
statement it gets a number from the number variable so ten, and it says is
ten equal to five well no it isn't so that means the statement is false so then it
doesn't run this code here, now what do you do if you want the if statement to
run some code when the statement is false, well to do that you use an else on the
end of your if, so to add an else onto the end of your if you just go down a
line and then remove the tab, and then just type else and then colon, now it's
important that the else is in line with the if, if the else isn't in line with the
if then the if wouldn't know that that's the else it's supposed to use when the
statement is false, so just make sure that your else statement here is in line
with your if and once you've made sure that it is in line you can then
press ENTER and just like we did before you can type out the code that you want
to run but this time this it's the code that you want to run when a statement is
false, so when a statement is false I want it to print the number isn't
five, so now if we save the code and we run it as you can see it prints the
number isn't five now that's because when it gets into the if it gets a
number which is ten and it goes is ten equal to five
well no it isn't that's false so then it runs this code here if the statement is
true then it will run this code if the statement is false then it will run this
code, so now we've gone over the equal to operator we can now go over the not
equal to operator now the not equal to operator is
basically just the opposite to the equal to operator
now I've changed the if statement here so the number variable is being tested
against five like it was before but this time the if statement says if the
number isn't equal to five then run this code here and then if it is equal to
five you run this code here, so at the minute the number is ten so if we save
the code and we run it, as you can see it prints the number isn't five now that's
because it comes into the if statement and it gets a number which is ten and it
goes is ten not equal to five, well ten is not equal to five so the statement is
true so then it runs this line of code now if we change the number variable to
five and we save it and we run the code it now prints the number is five that's
because it comes into the if and it goes is the number so five is five not equal
to five, well five is equal to five so that means that this statement is false so
then it runs this code here, so now we've gone over the not equal to operator we
can now go over the greater than operator, so let's just replace this if
statement here and now the if statement says if the number variable is greater
than five, then it will run this code here if it's five are less then it will
run this code here, so at the minute the number is five so if we save the code
and we run it, it now prints the number is five or less that's because it comes
into the if statement and it gets the number five from the number variable and
it goes is five greater than five well no five is not great than
five, five is equal to five so that statement is false so then it runs this
code here, now if we change the number to say four and we save it and run it, we
get the same output again that's because the if goes okay the number so this is
four is four greater than five no four is less than five so the statement is false,
now if we change a number in the number variable to say something like ten then
if we save the code and we run it, as you can see it now prints the number is bigger
than five, now that's because it comes into the if
and it gets the number ten and it goes it's ten greater than five well yes ten
is bigger than five so the statement is true, so then it runs this line of code, so
now we've gone over the greater than operator we can now go over the less
than operator, so let's just change our if statement again, so now our if
statement says if the number is less than five then it will run this line of
code, if the number is five or more then it will run this line of code, so at the minute
the number is ten so if we save the code and we run it as you can see it prints the
number is five or more that's because it comes into the if and it gets the number
so ten and it says is ten less than five well no ten is not less than five so the
statement is false, so it runs this line of code here, now if we're change
number to five and then we save it and we run it, as you can see it prints the
number is five or more, now that's because when it comes into the if it gets the
number and it goes is five less than five, well no
five is not less than five, five is equal to five so the statement is false so it runs this
line of code, but now if we change the number to four and we save it and we run it
this time it prints the number is less than five and that's because when it comes
into if it goes is four less than five, well yes
four is less than five, so then the statement is true so it prints this line here, so
now we've gone over the less than operator we can now go over the greater
than or equal to, so we're gonna replace our if statement again, like so
and this time it says if the number is greater than or equal to five then it will
run this line of code, and if it's false then it will run this line of code, so at
the minute the number is four so if we save it and we run it, it prints the number
is less than five, now that's because it gets the number so
four and it's four greater than or equal to five
well no it's not greater than or equal to five, so that is false so it runs this
line of code here, now if we change the number to five and we save it
and we run it this time it prints the number is five are bigger, now that's
because when it comes into the if it goes is five greater than or equal to five
well five is equal to five so that statement is true so it runs this line of code, and
then if we change the number to ten and we save the code and we run it this time
it prints the number is five or bigger just like it did before, now that's because
when it comes into the if it goes is ten greater than or equal to five
well ten is bigger than five, so the statement is true so then it runs this
line of code here, now the last operator that we have to go over is the less than
or equal to, so we're going to replace our if statement again, so we'll
replace it like so and this time it says if the number variable is less than or
equal to five then it will run this line of code and if the statement is false then
it will run this line of code, so at the minute the number is ten, so if we save it
and we run it it prints a number is bigger than five now that's because when it
comes into the if it gets the number so ten and it says it's ten less than or equal
to five well ten is bigger than five so it's not equal to it and it's not less than
it so that means this statement is false, so then it runs this line of code, now if
we're change the number to five and we save it and we run it, this time it prints the
number is five or less, now that's because when it comes into the if it goes is five
less than or equal to five well five is equal to five so the statement is true so it runs
this line, and then if we change the number to say four and
save it and we run it as you can see it prints the number is five or less just
like it did before, now that's because it gets the number four and it goes is four
less than or equal to five, well four is less than five so this statement
is true so it runs this line of code here, so now we've gone over all the
operators I'll just quickly show you what happens if the code isn't indented
properly, so if we remove our indent and then we save the code and we try to run
it, as you can see it tells you it expected an indented block, so if it's
telling you it expected an indented block, you need to check that you have
indented your code properly, and I will just quickly show you what it looks like
if you don't put a colon on the end so I've now removed the colon so if I save it
and I run it, it now tells you it's an invalid syntax and it clearly highlights
where the invalid syntax is, so if we now put a colon and then we save it and
run it as you can see it works like it did before and it says the number is five
or less, so guys this is going to be the end of the video if you liked it don't
forget to hit the like button, if you disliked is then hit the dislike button, subscribe for more
content like this and I'll see you another time, bye.
For more infomation >> The One From Cleveland Ohio For The GKIC Info Summit... Rise Of An Empire Episode #14 - Duration: 21:12. 


Không có nhận xét nào:
Đăng nhận xét