Page 91 - TP-Play_V-2.0_Book-7
P. 91

THE IF…ELIF…ELSE LADDER

            The  if…elif…else ladder  is another
            type  of  if  statement.  It  helps us to
                                                                               True
            test multiple conditions and follows a          Test Expression 1        Indented Block 1
            top-down  approach.  In this, as soon
            as  the  condition of  the  if  evaluates  to            False
            be true,  the indented block associated
            with that  ‘if’ is  executed,  and the rest                        True
                                                            Test Expression 2       Indented Block 2
            of the ladder is avoided. If none of the
            conditions evaluates to true,  then the                  False

            final else statement gets executed.
                                                                              True
            The  syntax of  if...elif...else ladder  is     Test Expression 3        Indented Block 3
            shown below:

            Syntax:                                                  False           Indented Block 4

            if (Test Expressions_1):
                Indented block 1                                                             Statement just below
                                                                                              if...elif...else ladder
            elif (Test Expression_2):

                Indented block 2

            elif (Test Expression_3):
                Indented block 3

            else:

                Indented block 4
            Program 7: To check whether a given number is two-digit number, three-digit number or
            four-digit number






























                                                                      Conditional Statements in Python       91
   86   87   88   89   90   91   92   93   94   95   96