Page 116 - 2622_Delhi Police Public School_C-8
P. 116

Program 4: To print a pattern using for loop.

                          *
                          **

                          ***
                          ****

                   Program4.py                                                 Output
                File  Edit  Format   Run   Options   Window    Help         *
                                                                            **
                for i in range (1,5):
                                                                            ***
                    print("*"*i)
                                                                            ****





              Here, * operator repeats the string for a given number of times.

              Program 5: To print multiplication table of number taken as input from user.

                   Program5.py

                File  Edit  Format   Run   Options   Window    Help

                #program to calculate multiplication table of a number
                num= int(input('Enter a number: '))
                for i in range (1, 11):
                    print (num, 'x',i,'=', num * i)




              On running the above program, you will get the following output:

                   Output

                Enter a number: 6
                6 x 1 = 6
                6 x 2 = 12
                6 x 3 = 18
                6 x 4 = 24
                6 x 5 = 30
                6 x 6 = 36
                6 x 7 = 42
                6 x 8 = 48
                6 x 9 = 54
                6 x 10 = 60




              THE WHILE LOOP

              The while loop executes a set of statements repeatedly, until the logical expression evaluates to false.
              When the condition becomes false, the control comes out of the loop.




                        Premium Edition-VIII
                114
   111   112   113   114   115   116   117   118   119   120   121