Page 27 - PlusGPT_V2.1_C8_Flipbook
P. 27

The if Statement

                 The if statement is the simplest conditional statement. Here, a statement or a collection of statements
                 within the if block is executed only if a certain condition or expression evaluates to True. If the condition
                 evaluates to False, then the control of execution is passed to the next statement after the if block. The
                 syntax of the if statement is given below:

                 Syntax:
                                                                                                             False
                 if (Test Expression):                                                   Test Expression
                     Indented statement block

                 # if block ends here                                                            True


                                                                                            Body of if
                                      Tech Fact


                     In Python, the non-zero value is interpreted as
                       true. None and 0 are interpreted as false.                     Statement just below if






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















                 On running the above program, we get the following output:





























                                                                                    Conditional Statements in Python  25
   22   23   24   25   26   27   28   29   30   31   32