Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 146 - 2632_CodeMind
P. 146

    Program 4: To print multiplication table of an input number.
                                                                                                 Output

                         Program4.py                                                          Enter a number: 8
                                                                                              8 × 1 = 8
                      File  Edit  Format    Run   Options   Window    Help                    8 × 2 = 16
                                                                                              8 × 3 = 24
                      #Program to print multiplication table of a number                      8 × 4 = 32
                                                                                              8 × 5 = 40
                      num = int (input('Enter a number: '))                                   8 × 6 = 48
                                                                                              8 × 7 = 56
                      for i in range (1,11):                                                  8 × 8 = 64
                      #printing the table                                                     8 × 9 = 72
                          print(num, '×', i, '=', num*i)                                      8 × 10 = 80

                            THE WHILE STATEMENT

                     The while statement executes a set of statements repeatedly, till the logical expression evaluates
                     to true. When the condition becomes false, the control comes out of the loop. The syntax of while
                     statement is given below:

                     while (test expression):
                         Statements
                         Increment/Decrement

                      Program 5: To demonstrate the use of a while loop.

                         Program5.py
                      File  Edit  Format   Run    Options   Window    Help

                      #Program to demonstrate the use of a while loop

                      # Initialize a variable                                                Output
                      count = 1
                                                                                           Count is: 1
                      #test expression
                      while count < 5:                                                     Count is: 2
                          print("Count is:", count)                                        Count is: 3
                          count += 1                                                       Count is: 4

                          The While Loop Using Else Statement
                     Python enables the use of else statement with the while loop. The else block is executed when the
                     condition given in the while statement becomes false.

                                                                                                      Loops in Python  145

| 141 142 143 144 145 146 147 148 149 150 151 |