Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 78 - 2607_Traclpad_Pro_5.1_flipbook_C8
P. 78

    On running the above program, we get the following output:

                         Output

                      1
                      2
                      3
                      4
                      6
                      7
                      8
                      9

                     Program 12:  To display the first 5 powers of 2.

                         Program12.py
                      File  Edit  Format   Run    Options   Window    Help

                      # Print the first 5 powers of 2
                      for i in range(5):
                          print(2 ** i)

                     On running the above program, we get the following output:

                         Output
                      1
                      2
                      4
                      8
                      16

                     Program 13:  To count the number of digits in a number.

                         Program13.py
                      File  Edit  Format   Run    Options   Window    Help

                      # Count the number of digits in a number
                      num = 123456
                      count = 0
                      while num > 0:
                          num //= 10
                          count += 1
                      print("Numbers of digits: ", count)

                                                                                                   Iterations in Python  77

| 73 74 75 76 77 78 79 80 81 82 83 |