Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 143 - CodePilot V5.0 C7
P. 143

    Program 9 To print the squares of the first five natural numbers.

                         Program9.py                                                                Output
                      File  Edit  Format   Run    Options   Window    Help                       1

                      for i in range(1,6):                                                       4
                                                                                                 9
                           print(i*i)
                                                                                                 16
                                                                                                 25

                      Program 10 To find the sum of the first 10 natural numbers.

                         Program10.py                                                               Output
                      File  Edit  Format   Run    Options   Window    Help                       55

                      sum=0
                      for i in range(0,11):
                           sum+=i
                      print(sum)

                      Program 11 To print the first five multiples of a number.

                         Program11.py                                                             Output
                      File  Edit  Format   Run    Options   Window    Help                     Enter a number: 7
                      num = int(input("Enter a number: "))                                     7 x 1 = 7
                                                                                               7 x 2 = 14
                      for i in range(1, 6):
                                                                                               7 x 3 = 21
                          print(num, "x", i, "=", num * i)
                                                                                               7 x 4 = 28
                                                                                               7 x 5 = 35

                     WHILE LOOP IN PYTHON
                     A  while loop repeats a set  of instructions  as long as a given                   Start
                     condition is true. It checks the condition before each repetition. If
                     the condition is true, the loop executes; if it’s false, the loop stops   False

                     and the control moves to the statement outside the loop. It is often           While condition
                     used when the number of repetitions is not known in advance.
                                                                                                            True
                     Syntax of while statement:                 SHORT SIGN
                                                                                                      Command
                     while <condition>:
                                                             To indent the code: Tab
                          Statement block to be
                          repeated                                                                      Stop

                                                                                                                      141
                                                                                               Flow of Control in Python

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