Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 117 - 2625 PortGPt V2.1
P. 117

    2.   Write the output of the following codes:

                             a.    number = 0

                                   if (number > 0):
                                       print('Positive')

                                   elif (number < 0):
                                       print('Negative')

                                   else:

                                       print('Zero')
                             b.    num1 = 8

                                   num2 = 5
                                   if (num1 % 2 == 0 and num2 % 2 == 0):

                                       result = num1 + num2
                                       print("The sum is:", result)

                                   else:

                                       result = num1 * num2
                                       print("The product is:",result)
                             c.    x = 31

                                   if (x > 10):

                                      print("Above ten")
                                      if (x > 20):

                                          print("and also above 20!")

                                      else:
                                          print("but not above 20.")
                             d.    x = 200

                                   y = 33

                                   if (y > x):

                                       print("y is greater than x")
                                   else:

                                       print("y is not greater than x")
                         3.     Find the error in the following code and rewrite the code:

                             a.    x = 10

                                   if (x > 5):
                                   print("x is greater than 5")

                                                                                        Conditional Statements in Python  115

| 112 113 114 115 116 117 118 119 120 121 122 |