Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 143 - 2632_CodeMind
P. 143

    9                                                                                                                    The range() function is an in-built function of Python. This function generates a list which is a sequence
                                                                                                                                                     Using the range( ) Function

                                                              Loops in Python
                                                                                                                                                type. A sequence is a succession of values bound together by a single name. The range() function is
                                                                                                                                                used in the for loop to iterate over the numbers.

                                                                                                                                                The general syntax of using the range() function is given below.
                                                                                                                                                   range(n): Generates a set of whole numbers starting from 0 to (n–1).

                                                                                                                                                  Example: range(6) is equivalent to [0, 1, 2, 3, 4, 5].
                                                                                                                                                   range(start, stop): Generates a set of whole numbers starting from ‘start’ to ‘stop–1’.
                          Let’s surf                                                                                                              Example: range(4, 9) is equivalent to [3, 4, 5, 6, 7, 8].

                              The for Statement                            The while Statement                                                      range(start,  stop, step_size): By  default,  the value         Just as the heart repeatedly
                                                                                                                                                                                                                       pumps blood, iteration
                              Jump Statements                              Some More Programs                                                      of the step_size  =  1 and numbers are generated  with             in programming involves
                                                                                                                                                   a difference  of  1. However, we can generate  numbers            repeatedly running a set of
                                                                                                                                                   by  specifying  the  value of step_size  according  to  our              instructions.
                      Let’s PLug-in                                                               Century   #Information Literacy                  requirement.
                                                                                                    21 st
                                                                                                   Skills
                                                                                                                                                   Example: range(1, 14, 2) is equivalent to [1, 3, 5, 7, 9,11,13].
                        State whether these statements are true or false.

                        1.  Python does not support conditional statements.                             ____________
                                                                                                                                                                   Imagine, you possess the power of jump statement, allowing you to leap to any
                        2.  We can use if statement inside another if statement.                        ____________
                                                                                                                                                                   part of the universe. Where would you jump to and what quests or missions would
                        3.  We can check multiple conditions in Python.                                 ____________                                               you undertake in each dimension?

                      Sometimes, you need to repeat a task multiple times or you may need to repeat the task either a
                      certain number of times or until a condition is satisfied. In Python, the statements that are used to
                      repeat a set of instructions are called iterative or looping statements. Looping statements are very
                      useful and necessary for developing applications.
                      Python provides two types of looping statements—for and while. The for statement is used to repeat
                      an instruction or a set of instructions a fixed number of times. Whereas, the while statement is used to
                      repeat a set of instructions until a condition evaluates to true. Let’s discuss these statements in brief.

                             THE FOR STATEMENT

                      The for loop is a type of iterative statement that allows the user to repeat a set of statements multiple
                      times until a given condition is falsified. The for loop is commonly used when the number of iterations
                      is known in advance or when iterating over a sequence, such as a list, string, tuple, or a range.

                      The syntax of for loop is as follows:
                      for <counter variable> in range(start, stop,
                      step_size):
                             statements

                      142   Premium Edition-VIII

| 138 139 140 141 142 143 144 145 146 147 148 |