Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 124 - 2615_KEC
P. 124

    Program 25: To create a list of four subjects: “Math”, “Science”, “English”, and “Computer”. Using

                      a for loop, print each subject from the list on a new line.

                          Program25.py
                       File  Edit  Format    Run   Options   Window    Help

                       subjects = ["Math", "Science", "English", "Computer",]
                       for subject in subjects:
                           print(subject)

                          Output

                       Math
                       Science
                       English
                       Computer

                      Program 26: Write a program to print the squares of numbers from 1 to 5.

                           Program26.py                                                     Output
                                                                                          1
                        File  Edit  Format   Run   Options   Window    Help
                                                                                          4
                        for i in range(1, 6):                                             9
                                                                                          16
                            print(i*i)
                                                                                          25

                      Program 27: To test whether a given number is even or odd.

                          Program27.py
                       File  Edit  Format    Run   Options   Window    Help

                       #Program to check whether the number is even or odd
                       number = int(input("Enter an integer: "))
                       if number % 2 == 0:
                           print("The number is even.")
                       else:
                           print("The number is odd.")

                          Output

                       Enter an integer: 24
                       The number is even.

                      122   Premium Edition-VII

| 119 120 121 122 123 124 125 126 127 128 129 |