Hello welcome to this tutorial on IF statement
conditional statements.
Give us an ability to check conditions and change the behavior of the program accordingly and if statements
are also known as Control floor statements which help us to run a particular chord.
Only when a certain condition is met this is the syntax for the if statement if it is the key word then
we have a boolean expression and then a set of statements.
The first line of statement that is the if condition is known as if clause and the condition your is
a boolean expression that is evaluated to either true or false and in the next line.
We have a block of statements and notice the colon after the if clause in the first line.
This is very important because it separates the if clause with the body that is the block of statements
here.
Next the line after the colon must be indented.
That means the block of code should be positioned by the same amount to the right of the.
If keyword and Biden recommends to indent each statement in the block by four spaces so beginning from
the if key word.
We need to indent each statement by Force bases.
So you need to leave four spaces and then you can type your code after that.
So all of this code gets executed.
If the if condition is true and these statements followed by the if clause which are not indented that
means the statement here.
For example the non belong to the if block so these statements are not part of the block.
So as a result it will always execute no matter whether the condition in the if clause is true or false.
So let's now look at some examples.
I have the if clause here.
This boolean expression age is less than or equal to fly.
And if this condition is true the print function print the text here.
And as you can see there is only one statement in this if clause going to the next example.
I have two variables and a is assigned to two and then B is assigned to 5 and the boolean expression
your checks.
If it is greater than B Since 2 is not greater than 5 the indented code here is not executed.
The condition is false and this block of code is not executed.
So decontrol goes to the first UN indented statement in the program and this is the statement and the
program flaw goes like this.
So here I assign why you do to the variable each and then using the if condition.
I'm going to check if this particular boolean expression is true or not and there's one statement in
the if clause notice the indentation here I have left some spaces to the right of the if keyword so
that this gets executed.
If the condition is true so now let execute this piece of code.
So this condition is true and the text has been printed.
Let's consider in another example.
So in this example I have assigned devalued tend to debatable each and the boolean expression in the
if clause checks if the variable is less than or equal to 5.
And if this boolean expression is true the print function print this text or another print function
but just not indented which means it does not belong to the if clause and it gets executed no matter
whether the condition in the clause is true or not.
So let's execute this piece of code devalue 10 is not less than equal to 5.
That means the boolean expression used is false.
So this print function is not executed.
The next statement in the program which is not indented is the print function so the program executes
this line of code.
It does now discuss another exam but so I have a variable flag that is assigned to the Boolean value
true and then I have the if statement the if statement checks if the variable flag is true or not.
And if this is true it prints this text we have discussed before that the if statement always checks
if the condition is true only if the condition is true the statement within the if block are executed
or not executed at all.
So comparing variable flag here with the Boolean value true is redundant so we can put this if statement
like this.
If flat and then print.
We'll see.
So in this if statement we have simply parsed the variable flag to the if clause because if always checks
if the condition is true.
So here in this example I have assigned the Boolean value true to the variable flag and in the if condition
I have simply parsed the variable flag.
That's because the if condition gets executed only if this particular condition is true we don't have
to compare the variable flag with the Boolean value true and if this is true the print function prints
the text.
So let's execute this set of.
So this is the output.
Now we have discussed in previous tutorials that in Python the value 0 evaluates to false.
Similarly an empty string evaluates to false and empty list evaluate to false and the integer 1 2 3
for any integer greater than 0 evaluates to true so.
So here to the if a word I have passed an empty string and an empty string and Python evaluates true
false.
So this brain function will not get executed.
So let's execute this in and check.
As you can see this is false.
An empty string evaluates to file and so d if block did not get executed.