If Condition in Python

If Condition

Conditions are nothing but statements used to check if a specific action is True or False. 

In this post, we will see how If condition works in Python and how to use if, elif (Else-If) and else statements. 

Syntax:

if some_condition:
    // code to be executed
elif another_condition:
    // code to be executed
elif another_condition_2:
    // code to be executed
.
.
.
else:
    // code to be executed


Let's breakdown the syntax of into three statements and see each of the statement in detail along with few examples.
  • if
  • elif
  • else

if statement

If statement is used to verify if a specific condition is True and the block of code under the if statement would only be executed if the condition is True. 

if some_condition:
    // code to be executed

'some_condition' here needs to be replaced with the actual condition to be used in the program and 'code to be executed' needs to be replaced with what needs to be executed when the condition is satisfied. 

If the condition is not satisfied (In other words is False), code under the if condition would not be executed. 

E.g.: 

Check if a number is even and print "Even Number" if True. 

if statement in Python

In the above example, 
  • Line - 5: We are declaring the variable 'number' with the value 10 (even number).
  • Line - 6: We are using if statement to check if the result of number divided by 2 (modulus) is zero.
    • Here we are using Relational operator '==' to check if 'number % 2' is equal to '0'. Have a quick look at the different Relational operators.
  • Line - 7: print statement to print "Even Number". This statement would only be executed if the number is even. 
If we are passing the number '11' instead of 10, modulus of 11 divided by 2 is '1' which is not equal to zero. 

In this case code under if condition is not executed and no output would be printed. else statement would be helpful in these cases. 

else statement

else statement is used when we need to execute set of statements when the condition of the if statement becomes False. 

if some_condition:
    // code to be executed
else:
    // code to be executed

code written under the else statement would only be executed if the condition on the if statement is False. 

E.g.:

Let's consider the same example of checking if the number is even. In the previous example, we aren't printing anything if the number passed is odd number.

if - else statements in Python

In the above example, 
  • Line - 6: We are declaring the variable 'number' with the value 11 (odd number).
  • Line - 7: We are using if statement to check if the result of number divided by 2 (modulus) is zero.
    • Here we are using Relational operator '==' to check if 'number % 2' (modulus) is equal to '0'. 
    • Modulus of 10 divided by 2 is '1', so condition is False.
  • Line - 8: print statement would not be executed as the condition is False. This statement would only be executed if the number is even.
  • Line - 9: else statement indicate the code to be executed if the condition is False. Following code under else would be executed.
  • Line - 10: print statement to print "Odd Number"
*Code under if or else would be identified based on indentation. 

This works perfectly fine if there is only one condition to be checked. However, If there are multiple conditions to be checked, we can use elif statement. 

elif statement

elif (Else If) statement is used when we have another (one or more) condition(s) to be verified before else statement if the condition on the if statement is false. We can have more than one elif statements. 

if some_condition:
    // code to be executed
elif another_condition:
    // code to be executed
elif another_condition_2:
    // code to be executed
.
.
.
else:
    // code to be executed

First elif statement would only be executed if the condition on the if statement is false, Second elif statement would only be executed if the condition on the previous elif statement is false and so on, else statement would only be executed if none of the if or elif statements are true.

Let's have a look at simple example to understand if-elif-else statements better. 

E.g.: 

Identify the color and print the name of color. 

if-elif-else statements in Python

In the above example, 
  • Line - 9: We are declaring the variable 'color' with value 'Green'.
  • Line - 11: if statement with condition on color to check if 'White'. 
    • If True, code under the if statement would be executed (Line - 12). In this example result is False, so code under this statement would not be executed.
    • If False, condition on the next elif statement would be checked. 
  • Line - 14: elif statement with condition on color to check if 'Black'.
    • If True, code under the elif statement would be executed (Line - 15). In this example result is False, so code under this statement would not be executed. 
    • If False, condition on the next elif statement would be checked. 
  • Line - 17: another elif statement with condition on color to check if 'Green'.
    • If True, code under the elif statement would be executed (Line - 18). 
    • If False, code under the else statement would be executed. In this example result is True, so code under else statement would not be executed. 
  • Line - 20: else statement would only be executed if none of the above if and elif statements are True. 

Nested if statements

Nested if statements are having a if statement with in another if statement. 

There is no limit on how many levels of nested if statements can be used. But, it is advised not to use too many nested if statements to make the program simple and easy to understand. 

if some_condition:
    // code to be executed
    if nested_condition:
        // code to be executed
        .
        .
        .
    else:
        // code to be executed
    .
    .
    .
else:
    // code to be executed

Let's have a look at the simple example to understand this better. 

E.g.: 

Check if a number is between 0 and 9, If yes, check if the number is between 0 and 4.

Nested if statements in Python

In the above example,
  • Line - 3: We are declaring the variable 'number' with value '4'. 
  • Line - 6: if statement to check if the number is between 0 and 9. 
    • range(10) would return the numbers from 0 to 9 (both inclusive).
  • Line - 8: if statement with in the previous if statement (nested) to check if the number is between 0 and 4.
    • This if statement would only be executed if the condition on previous if statement is True. 
  • Line - 9: print statement would print "Number is between 0 and 4".
    • This would only be executed  the condition on the if statement is True (Line - 8).
  • Line - 11: else statement corresponding to the nested if statement.
    • print statement to print "Number is between 5 and 9" (Line - 12) under else statement. This would only be executed if the condition on corresponding if statement is False.
  • Line 14: else statement corresponding the original if statement.
    • print statement to print "Number is greater than or equal to 10" (Line - 15) under else statement. This would be executed if the condition on corresponding if statement is False.

pass statement

pass statement works more like a placeholder where some code is mandatory by syntax and no logic needs to be executed. 

pass statement in if condition - Python

In the above example, No logic needs to be executed if the number is even. Or, In other words, it is not yet finalized on what to be done. 

In these cases, we can use pass statement as placeholder and continue with the program. 

This can be replaced later if required or leave it as is if required. 


Hope the above was a bit of help to understand the If conditions in Python. 


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Comments

Popular posts from this blog

What is the importance of User Open (USROPN) in RPGLE - IBM i

What is Deep Learning? Beyond the Basics of AI

What is Artificial Intelligence?