Unlock Analytics

Python for HR : If Else in Python

Python for HR : if -else

Hello and welcome to this series of Python for HR.

As an HR you make decisions every day so does the python. But it does in a little different way than humans. What are these techniques? How python takes decisions? Any guesses?

Don’t worry that is what you are here for. You will now know how actually python makes the decision. 

Decision making in python is conditional.

IF statement: It is made up of three parts. The If keyword, condition(s) and the block of statements that will be executed if the condition is true or met.

Let’s understand it with an example.

John’s_age = 33/span>

Richards’s_age = 56/span>

if Richards’s_age > John’s_age:/span>

print(“John is younger than Richard”)/span>

Output: John is younger than Richard/span>

We can also provide a block of statements that can be executed if the condition is not met or false. This is made possible by the use of else statements.

The Else statement 

What if the condition is not met? What you can do? This is where else statement comes into action.

The else keyword catches anything which isn’t caught by the preceding conditions.

John’s_age = 56/span>

Richards’s_age = 26/span>

if Richards’s_age > John’s_age:/span>

print(“John is younger than Richard”)

else /span>

print(“Richard is younger than John”)/span>

Output: Richard is younger than John/span>

If the 1st condition is not met it will not execute the block of code written in it. The python will print the other case. In the above case, if we compare the age of both employee then conditions are not met, so it printed the else statement.

The Elif Statement

The elif keyword is pythons way of saying “if the previous conditions were not true, then try this condition”

What if you have multiple other cases to test, this is where the elif statement comes in. Elif is the else-if statement in other languages. These statements allow for an arbitrary number of conditions.

John’s_age = 56/span>

Richards’s_age = 56/span>

if Richards’s_age > John’s_age:/span>

print(“John is younger than Richard”)/span>

elif Richards’s_age = John’s_age:/span>

print(“John and Richard is of the same age”)/span>

else /span>

print(“Richard is younger than John”)/span>

Output: John and Richard is of the same age/span>

Congratulations !!!

Now you know about the if-else condition and how to do that in python.

Happy learning.