Python If… else Statement

Python If… else Statement


In this section you will learn decision making using if else statement, the syntax of if else and examples. The decision making construct specifies that we have to run a certain lines of code when a certain condition is true or false.

 

Python if statement

“if” is a keyword in Python programming language. The “if” statement is a decision making statement. It is the simplest form of selection construct. It is used to execute or skip a statement by checking a condition.

The condition is given as a relational expression. If the condition is true, the statement after if statement is executed. If the condition is false, the statements after “if” are not executed.

The Python if statement runs a block of statements when a condition is true, there are no other lines of codes when if the condition becomes false. The following is the syntax of if statement:

SYNTAX

If condition:

     Statement(s)

The above syntax is used for single statement. A set is statements can also be made conditional. If the above condition becomes false then the clock of statements will not be executed. The block of statements will only execute if the condition is true. In Python the body of the “if” statement is indicated by indentations. The true condition is evaluated to True by interpreter of Python and None and 0 will considered as False.

 

Python if statement Flowchart

python-if-statement-flowchart

EXAMPLE

Consider the following example in which we have assigned a value to the variable marks and if the value is greater than 40 then a message will be printed using the print statement:

CODE

marks = 60

>>> if marks > 40:

          print (“Congratulations You have passed”)

OUTPUT

Congratulations You have passed

congratulations-you-have-passed

In the above example, we assigned 60 to the variable marks and when the condition in the ‘if’ statement that is marks > 40 is true therefore, the print statement is executed.

 

Python if… else Statement

It is another type of if statement. It executes one block of statement when the condition is true and the other when the condition is false. In any situation, one block is executed and the other is skipped. In if else statement:

  • Both blocks of statement can never be executed.
  • Both blocks of statements can never be skipped.

The syntax of if else statement is given below:

SYNTAX

if condition:

   statement (s)

else:

  statement (s)

 

Python if… else Flowchart

python-if-else-flowchart

EXAMPLE of if… else Statement

Consider the following example, in which we used if else statement to print ‘you have passed’ if the marks are greater than 40 and if marks are less than 40 then it will print ‘you have failed’.

CODE

>>> marks = 54

>>> if marks > 40:

          print (‘You have passed’)

else

          print (‘you have failed’)

OUTPUT

You have passed

you-have-passed

In the above example if the marks were smaller than 40 then block of statements under else will be executed. Consider the following example, in which it is demonstrated:

CODE

>>> marks = 38

>>> if marks > 40:

          print (‘You have passed’)

else:

          print (‘You have failed’)

OUTPUT

You have failed

you-have-failed

Python if… elif… else Statement

In Python elif stands for else if. In this type of decision making structure we can check multiple conditions to execute only one block of statements.

If the condition written under ‘if’ is false then the condition under elif will be executed and if this becomes the else statement will be executed.

The following is the syntax of if elif else statement:

SYNTAX

if condition:

    statements

elif condition:

    statements

else:

    statements

In the syntax condition is any valid expression.

Flowchart of if… elif… else Statement

flowchart-of-if-elif-else-statement

In the flowchart condition 1 is under the ‘if’ statement and condition 2 is under the elif statement and condition n is the else statement.

EXAMPLE of if… elif… else Statement

Consider the following example if elif else structure is used to run one block of statement:

CODE:

>>> marks = 50

>>> if marks > 40:

          print (‘you have passed’)

elif marks == 100:

          print (‘you got full marks’)

else:

          print (‘you have failed’)

OUTPUT:

you have passed

In the above example, the marks entered are 50 and when the ‘if elif else’ structure is run the ‘if’ condition becomes true and the message is printed that you have passed. If the marks were equal to 100 then a message will be printed that you got full marks and if the marks are smaller than 50 then the message will be printed that you have failed.

 

Python nested if statement

In Python we can use nested if statements that is an ‘if’ statement in another if statement. This becomes confusing a little bit but we can nest any number and any type of the ‘if’ structure in another ‘if’ structure.

There is a difference between sequence of ‘if’ statements and nested ‘if’ statement. The sequence of ‘if statements’ is simpler than nested ‘if’ statement. That is why beginners usually prefer to use a sequence of if statements than nested if statement. However, it is sometimes useful to use nested ‘if’ instead of sequence of ifs. In nested ‘if’ statement, the remaining conditions are skipped when the control reaches a logical decision. In a sequence of ‘if statements’, all conditions are tested in any case.

EXAMPLE of Python Nested if

Consider the following example, in which we will use nested if statement to find that the numbers entered by the user are equal or not:

CODE

>>> n = int (input (“Enter a number: “))

>>> a = int (input (“Enter a number: “))

>>> b = int (input (“Enter a number: “))

>>> if (n == a):

          if(a== b):

                   print (“All numbers are equal”)

          else:

                   print (“Numbers are different”)

else:

          print (“Numbers are different”)

OUTPUT

Enter a number: 3

Enter a number: 5

Enter a number: 2

Numbers are different

numbers-are-different

In the above example, the user entered three numbers. First if statement checks that is n is equal to a then execute the body and if a is equal to b then the interpreter will print that the numbers are same and if the condition is not met it will print that numbers are different and so on.