
In the previous blog, pythondict- 5 methods to know, you had seen that how can you access data easily.
In this blog, you are going to see python if-else conditions.
Recently, my friend group is talking about that how can we use if-else conditions? What we have learned interesting!
What we have found that we can represent a message if the conditions match or not. It’s a pretty amazing thing!
Think about it, you have come from more computer language to human language.
Python conditions are logical conditions. If something is true that is the logic behind this.
You are going to show how to use this logic in an if-else statement.
You are going to see:-
• Python conditions and
if statement
• Else statement
• Indentation
• Elif statement
• Short Hand if
• Short Hand if-else
statement
• And keyword
• or keyword
Python conditions and if statement
There are many conditions that you can check with the help of a logical operator.
For example,
• a==b
• a>b
• a<b
• a>=b
• a<=b
• a!=b
In the practical world, we don’t use logical operators. What we use message. The simple and short message.
The message gives us a sense to the people what happened in a computer program.
So, to bring that message to cause we are going to use first if statement.
If condition
Suppose, you have to check if a certain thing will happen in a particular condition then what will happen?
To check this condition you can use python if statement.
For example,


Here, you can see that if condition has matched. So, the Message has displayed.
If suppose if condition is not matched then what will happen?
For example,


Here, you can see that nothing has displayed.
It doesn’t give a message to us.
So, basically what you need?
You need a condition that executes when if condition does not match.
To solve this problem you are going to see else statement.
Else statement
The else statement uses when the previous condition doesn’t satisfy the condition.
Remember, the else statement doesn’t check any condition. It only prints a message when the previous one failed to check the condition.
For example,


Here, you can see that if condition has not matched then else has displayed the message.
Indentation
Suppose you would not give spaces in if block. then what will happen?
For example,


Here, the Indentation error has come. because I had not put spaces in if block.
So, what these spaces represent?
If there are spaces before the if statement. It represents the same block of code.
Elif statement
The elif condition uses when we have to check another condition than the if statement.
For example,


Suppose, if and elif both statements are not matching then the else condition will match.
For example,


Here, you can see that if and elif both don’t match. The else statement is matching. So, the message of else displayed.
Short hand if statement
If the statements are simple and short then you can use a short hand if statement.
The short hand if statement is coming in one line.
For example,


Here, you can see that we have done code only in one line. In short, hand if statement we only need one line to do the code.
Short hand if-else statement
Suppose you have to represent a short if-else in one line. How would you represent it?
You can represent it in one line.
For example,


And keyword
Suppose, you have to combine two conditions. What will you do?
You can use and keyword.
For example,


Here, you can see that two conditions have combined using and keyword.
If one condition doesn’t satisfy. What will happen in and keyword?
For example,


Here, you can see that one condition has not matched in and keyword.
Or keyword
The or keyword also combine two statements. So, why we using anything other than and keyword.
In and keyword both sides should satisfy. In or keyword you can only satisfy one condition.
For example,


If one condition will not match in or keyword what will happen?
For example,


Here, you can see even one condition satisfied then output has displayed.
Conclusion:-
1. The if-else-elif condition are using to match the condition in python.
2. With the help of these logical operators you can put the message that what happens in the program.
3. You can also combine the conditions in python with the help of and and or keyword.
LIKE ❤️ ME