Page 129 - TP_Pluse_V2.2_Class_7
P. 129

THE if STATEMENT                                                     Start


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

                 if (Test Expression):
                                                                                              Stop
                     Indented statement block
                 # if block ends here
                                                                                  Tech Fact
                                                              In Python, the non-zero value is interpreted as true.
                                                                      None and 0 are interpreted as false.




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



                     Program1.py
                  File  Edit  Format   Run    Options   Window    Help

                  #Program to check if a number is positive
                  n = int (input ('Enter a number:'))
                  if(n > 0):
                      print('Entered number is positive')


                  #Statement outside the if statement
                  print('All numbers greater than 0 are positive')





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

                     Output
                  Enter a number:6

                  Entered number is positive
                  All numbers greater than 0 are positive


                  Enter a number:-9
                  All numbers greater than 0 are positive







                                                                                    Conditional Statements in Python  127
   124   125   126   127   128   129   130   131   132   133   134