Page 107 - PortGPT_V2.1_C7_Flipbook
P. 107

Program 2: To demonstrate the if statement.

                     Program2.py
                  File  Edit  Format   Run   Options  Window    Help

                  #Program to display the message using if statement
                  days = int(input('How many days are there in a leap year? '))


                  if(days == 366):
                      print('Your answer is correct, Good Job')


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

                     Output

                  How many days are there in a leap year? 366
                  Your answer is correct, Good Job


                  How many days are there in a leap year? 365




                        THE IF…ELSE STATEMENT
                                                                                   Start

                 The  if...else  statement  checks  for a condition.  If  the
                 condition evaluates to True, the indented block following
                 the if statement is executed; otherwise, the indented block     if condition
                 after the else statement is executed. The syntax of the                                    False
                 if…else statement is given below:                                     True
                                                                                                   Statements in else
                                                                              Statements in if
                 Syntax:                                                                             block execute
                                                                               block execute
                 if (Test Expression):
                     Indented block
                                                                                    Stop
                 else:
                     Indented block
                 Program 3: To compare two numbers and print the result

                     Program3.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')




                                                                                    Conditional Statements in Python  105
   102   103   104   105   106   107   108   109   110   111   112