if, if else, if elif else and nested if else in Python.
if, if else, if elif else and nested if else in Python.
In this post we are going to learn about if , if else , if elif else and nested if else in Python. Let's start..!
This if else is used for checking conditions in Python. This statement ( if else ) decides a flow of program. It simply means depend upon condition next statements will execute.
Syntax : if condition :
if block statements
else :
else block statements
Here if the condition is true then if block statements will executes. Else the else block statements will executes.
for ex. Suppose
We have a variable a = 5 and we want print message that a is greater than 10 or less than 10. To achieve this we will check a condition using if else conditional statements and we will print result that a is greater than 10 or less than 10.
Here a is 5 so a is not greater than 10 and our condition is false so else block will execute and will print a is less than 10.
Here I am giving some spaces before a print statements What is that ? and Why I give spaces there ?
Indentation :
This is Indentation in Python. It simply means you don't need to give curly brackets ( { } ) there to define a block. Block means a bunch of statements or code . To define block in Python you have to give a exact four spaces ( only 4 spaces ; more than 4 or less than 4 spaces not allowed; otherwise this will raise an error ) according to the start of previous line and write all statements in same line as first statement in block You will learn this things better in further posts and programs .
Here if our condition is true then if block will execute, Else else block will execute. But when your given condition is false and still want to apply some conditions and then based on that condition print the result. For this we have a special elif statement .
Syntax : if condition :
if block of first condition
elif condition :
if block of second condition
else:
common else block
Here if the first condition is true then if block statements will executes. Else it will check second condition or elif condition and if second condition is true then second if block statements will executes. Else last else block statements will executes.
For ex. Suppose
We have a variable a = 15 and we want print message that a is greater than 20 or less than 20; and if the a is less than 20 then we want to check again that a is greater than 10 or less than 10. To achieve this we will first check a condition to know a is greater than 20 or less than 20 and if a is less than 20 then we will check a condition to know a is greater than 10 or less than 10 Now based on this we will print result.
Here a is 15 so a is not greater than 20 and our condition is false so check for second condition; here a = 15 so a is greater than 10 now we will print a is less than 20 but a is greater than 10.
Now learn about Nested if else What is this ? This is very simple concept. Simply writing if else statements inside a if block or a else block.
Syntax :
if condition :
if condition:
inner if block
outer if block
else :
else block statements
Here if the first condition is true then if block statements will executes with this inner if condition is true then inner if block will executes. Else else block statements will executes.
for ex. Suppose
We have a variable a = 10 want to check a is greater than 5 or less than 5 and if a is greater than 5 then check for a is equal to 5 or not and print result accordingly.
Here a is 10 so a is greater than 5 so our condition is true so check for second condition; here a = 10 so a is not equal to 5 now we will print a is greater than 5 a is not equal to 5.
Sometimes we want to print a message only if the condition is true and don't want print anything if condition is false. In this case we can use only if statement. Simply write a if statement and if block .
Syntax :
if condition :
if block statements
Here if the condition is true then if block statements will executes. Otherwise this will skip if block and continues the execution.
for ex. Suppose
We have a variable a = 10 and we want to check a is equal to 5. we want print a result only if condition is true else don't show anything.
Here a = 10 and a is not equal to 5 so this will not print anything.
This is all about if , if else , if elif else and nested if else conditional statements in Python. Try this all conditional statements for checking various conditions.
If you have any doubts in this post then please ask me in comment section below. I will try to solve it.
If you like these post and you have learned something from this then share this post with your friends and family.
Keep learning always.....!!! 😇😇😇