Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 139 - 2612_Digimind
P. 139

    else:
                                 statement(s)

                     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)                                                                  Stop

                                                                                            Control Structures in Python  137

| 134 135 136 137 138 139 140 141 142 143 144 |