Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 82 - 2615_KEC
P. 82

    Program 2: To use the random.choice(sequence) function.

                          Program2.py
                       File  Edit  Format    Run   Options   Window    Help

                       import random
                       colors = ["Red", "Blue", "Green", "Yellow"]                            Output
                       print(random.choice(colors))
                                                                                           Green

                                SELECTION STATEMENTS

                      Some problems cannot be solved by performing a set of ordered steps as seen in sequential
                      execution. When programmers are required to execute a particular set of statements depending
                      on a particular  test  condition,  a selection  or decision  making  statement  is required.  Python
                      provides the following selection statements:
                      (i)  if statement     (ii)  if-else statement     (iii)  if-elif-else statement

                      THE if STATEMENT
                      The if statement is a decision-making statement that is used to control the flow of execution of

                      statements.
                      It contains a conditional expression , which is used to compare              Start
                      data. A decision is made based on the result of the comparison.
                      If the result of the expression is true, the statements within the                          False
                                                                                                if condition
                      if block are executed and if the result of the expression is false,
                      the statements within the if block are not executed. There is no                True
                      limitation on the number of statements that can appear within        Statements in if block

                      the if block. Hence, you may add as many statements in the if              execute
                      block as needed. The syntax of if statement is as follows:
                      if (conditional expression):                                                 Stop

                          statement(s)
                                                                                         Start
                      THE if-else STATEMENT

                      The if-else statement is used to evaluate whether a
                      given statement is true or false. If the given condition is      if condition
                      true, then statements within the if block are executed.                                     False
                      If the given condition is false, then statements within               True
                      the else block are executed. The syntax of the if-else        Statements in if     Statements in else
                                                                                                           block execute
                      statement is as follows:                                       block execute
                          if (conditional expression):
                                  statement(s)                                           Stop

                       80   Premium Edition-VIII

| 77 78 79 80 81 82 83 84 85 86 87 |