Page 93 - Touchcode_C8_Flipbook
P. 93

Program 2: To check whether a given number is positive.


















                 Nested if Statement
                 Python allows the nested if statement. This means the if indented block inside another
                 if…else indented block. The second nested if indented block will run only when the first if
                 condition evaluates to be true. The syntax of nested if statement is shown below:

                 Syntax:

                 if (Test Expression1):                                     Test        False     Statement 3
                                                                        Expression 1
                      if (Test Expression2):
                                                                               True
                           Indented block 1

                      else:                                                 Test        False
                                                                        Expression 2              Statement 2
                           Indented block 2

                 else:                                                          True

                      Indented block 3                                  Statement 1




                  Program 3: To find out if the student is selected for the college or not based on the given
                  conditions:
                  1.  Selected if age > 18 and marks > 65.

                  2.  Not selected if age > 18 and marks < 65.

                  3.  Not selected if age < 18.


















                                                                              Python—Conditionals and Loops      91
   88   89   90   91   92   93   94   95   96   97   98