Page 94 - Trackpad_V2.1_class8
P. 94

Program 2: To find the greatest number between two numbers

                      Program2.py

                   File  Edit  Format    Run   Options   Window    Help

                   #Program to compare two numbers using if else statement
                   a = int(input('Enter the first number: '))
                   b = int(input('Enter the second number: '))
                   if(a > b):
                       print('First number is greater than second number')
                   else:
                       print('Second number is greater than first number')




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

                   Enter the first number: 12
                   Enter the second number: 25
                   Second number is greater than first number




                  THE if-elif-else STATEMENT

                  Sometimes,  we need to  evaluate  multiple
                                                                           Start
                  statements  to  get  a  certain  result.  In
                  such scenarios,  we can use the if-elif-else
                  statements  to  evaluate  multiple  scenarios.                        True
                  First, it  checks  and evaluates  the  first          if condition 1         Statement 1
                  condition.  If it  is  true,  it  will execute  the         False
                  respective    statement(s).     However,     if

                  the condition  is false, it moves  to  the elif                       True
                  statement  and evaluates  those  conditions.          if condition 2         Statement 2
                  Finally, if none of the conditions evaluates to
                  true, it executes the else block. The syntax of             False
                  if-elif-else statement is as follows:                                 True
                                                                        if condition 3         Statement 3
                      if (conditional expression):
                              statement(s)
                                                                              False            Statement
                      elif (conditional expression):
                              statement(s)
                      elif (conditional expression):                                                         Stop

                              statement(s)
                      else:
                              statement(s)


                   92   Trackpad (V2.1)-VIII
   89   90   91   92   93   94   95   96   97   98   99