Hello everybody and welcome back.
And let us continue with our Python basics in this video we will cover the if loop.
Now if lope is basically the most simple one I would say it basically just does the same as it says.
So if something then do that and else do something else.
So I will explain a little bit better.
Don't worry.
For now on let us open up our second program and let us see how we can actually use the if loop.
So for that first of all let's open the interpreter.
So just wrap your python and you will see this.
Let's say we want to write the most simple if statement that we can write.
So for example a equals three.
This is simple to understand.
We stored value of three to eight and then if a equals equals three then two dots.
Now what this basically does the two equal sign checks.
If the value of A is three.
So if a really is three you can't really specify just one percent sign it will not work since 1 percent
sine means that you are actually giving the value of three to a.
You need to specify two percent.
Pardon me.
Two equal sign in order to check.
Two in order to perform a check on a.
If a really is three.
So if a equals three we will do what we will actually print a is three.
So we print a three and then we can add an or let's actually just leave it on this so we can see if
a equals three print a is three.
And as we can see it printed out a is three.
But let's actually switch it up.
So if a equals four print k is four.
And if we press twice enter you can see it didn't print out anything since A is in four we specified
eight eight to be three in our variable specification.
So this is the most simple use of IF statement what you can do is basically make an if statement which
will actually perform an action if a equals 4.
And it will perform a different action if a isn't equal 4.
So let me actually clear the screen so we can do that so all those dots system clear.
And let's actually do the same thing.
So a equals for example now five so we have a to be set to five.
And right now we want to write our first if else statement.
So if k equals equals five we hear habit and print.
Then we want to print a is five.
So this will only be printed out if a release five.
And what you want to do right here else and then to dot sign and then enter once again tab print a isn't
five a is some different number for example and press enter.
And now if you press Enter second time pops you will see that we get a is five printed out.
Since that is true a release is five.
And it didn't even consider this statement since it got this one to be true.
So if a equals five print a is five.
And we got printed out a equal or a five.
Now in the syntax syntax wise you need to make sure to add after if statement and also after L statement
you need to add these two dots right here since it will not work.
If you do not add that so make sure to double check that before you run your program and let's actually
run our first or not first our second program has a narrow window so as our as our real program and
not a program in interpreter or Python interpreter so unless we have our first program with this type
Python 2 that by which is our second program as we remember our previous video we need to specify the
path to Python so user been python.
And after this we can actually run our code.
So let's actually run a code that will check out if number is odd or even which is simple we will use
and if else statement.
So let's actually type your number one which is our or actually just name one number we need only one
number.
Let's use the input raw input function which will give our users of code are a choice to specify any
number so enter any puts any number two dots closed.
Double quotes then we closed two brackets right here and let's write an if Al statement right here.
So what we want to do is if open up our brackets No.
Module 2.
Now this percent sign stands for module.
Basically we use this in order to check out if our number is evil or it Your if it is odd if it has
any remaining after the division.
So this sign right here.
Make sure to remember that this sign is module we will not be using it that much but it is still a good
thing to know.
And then equals equals zero.
So if the Remain is zero then we need to specify that the number is even if it is not zero the number
will be odd.
So if we just type here two dots or remember to put these two dots right here enter and then tab it
so always tab it in order to make sure that your code looks a little bit prettier.
And in python I believe you cannot really write an if statement under the line in the same order you
need to tab it in.
So it knows.
So the python knows that this belongs to the if statement.
So right here we want to print.
Number is even.
And we want to specify here else which will happen in any other cases that the number isn't even which
is the only case that the number is odd.
So we want to print.
Else print.
Number is odd.
And then we close the double quotes.
So here we can see this is a simple program as well.
So we perform the module and then we actually print if number is even or if number is odd.
So let's control all this central control acts.
Let's make the program to be executable with this command.
So Python 2 the PI.
And right now we can see it's negative mutable and let's run it.
We hear we asked for a number we specify two it will say number is even if we just up here not Python.
If we run once again specify three it will say number is odd which is correct.
So that the simple use of R Python 3 scripts or PI pardon me of our Python 2 script with the if else
statement.
Now there is also another thing that you should know which also we will use is the L if statement which
basically allows you to specify multiple statements and not just two so let's edit our code now let's
actually write a different code.
We will have three different statements so let's delete these two statements right here.
We will leave the number to be here as a variable and we will leave the user to specify which number
it will be.
So let's see if no let's specify open brackets.
If number equals equals to zero type here.
Paw print number four given number is zero.
And then we basically just go into the next statement.
The next statement will be out if and then we need to specify an understatement right here.
If so how if number is bigger than zero then again two dots print not give a number is positive.
And then we call this double quotes and then the last statement will just be.
Else.
So in the other cases.
So any other cases would be the cases where number is not bigger than zero.
And the now and the one number is not equal to zero.
So any other case would be print given number is negative.
And then we close the double quotes so control over to save and control X to exit and you will see that
our program is still executable even though we edited it so we can actually run it right away so Python
to perhaps Python to enter any number.
Let's add three it will say a given number is positive.
If we run it again we give it minus two given number is negative.
So since minus two is a negative number.
And then once again zero even number is zero.
So you can specify as many if statements as you want.
So for example if you need to cover five different statements you would be specifying first in the if
the next three in the Elif and the last one in our statement.
Make sure to get to have the two dots at the end of each once and make sure to specify these statements
in given brackets it is easier.
You can also do it without them but it is a good practice to actually specify them in the brackets.
So that would be about it for these four or pardon me for this if statement video will cover the fourth
statement in the in the next video and I hope I see you there.
My.