Page 33 - tp_Modula_v2.0
P. 33

Clickipedia




                   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 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, you will get the following output:

                     Output

                  Enter a number: 6
                  Entered number is positive
                  All numbers greater than 0 are positive


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





                     IF…ELSE STATEMENT


                 The  if…else  statement checks for a condition. If the             Start
                 condition evaluates to  True, the indented  block
                 following the if statement is executed, otherwise the
                 indented block after the else statement is executed.             if condition
                                                                                                             False
                 Syntax:                                                               True
                                                                                                    Statements in else
                 if (conditional expression):                                  Statements in if       block execute
                                                                                block execute
                     statement(s)
                 else:
                                                                                    Stop
                     statement(s)

                                                                               Conditional Statements in Python   31
   28   29   30   31   32   33   34   35   36   37   38