Skip to main content

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

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search Arguments. Key Fiel

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the specified Ke

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short form. Below i