Page 101 - KEC Khaitan C8.5 Flipbook
P. 101

random.choice(sequence): Picks a random item from a list or a sequence. For example,
                 Program 2: To use the random.choice(sequence) function.

                     Program.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)



                                       Subject: If statement execution criteria
                                       A statement or statements indented within an if statement is executed only if
                                       the condition is true, otherwise the control skips the if statement and moves on
                                       to the statements outside the if block.











                                                                                        Control Structures in Python  99
   96   97   98   99   100   101   102   103   104   105   106