Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 90 - 2629_Devagiri_C-8
P. 90

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

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

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

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

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

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

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

                          Program4.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

                       88
                            Premium Edition-VIII

| 85 86 87 88 89 90 91 92 93 94 95 |