Page 127 - Computing E-Book Grade 7
P. 127

The City School  2021-2022



             In this example a is greater than b, so the first condition is not true, also the elif


             condition is not true, so we go to the else condition and print to screen that “a is
             greater than b”. We can also use the else without using elif.

             5.12. Loops in Python


             A For loop is used for repeating over a sequence (that is either a list or a string). This
             is less like the FOR keyword in other programming languages and works more like an

             iterator method as found in other object-orientated programming languages.
             For example, we have a list of students and we want to display the student with the                     Programming the Computer-Python

             highest marks without using the max() function:



             Sample Code


             # This defines a list of student marks
             stdMarks = [70, 80, 92.5, 60.2]

             # This variable keeps track of the max marks

             maxMarks = 0
             # for loop block
             for i in range(0,4):

                 if stdMarks[i] > maxMarks:

                     maxMarks = stdMarks[i]
             # Prints highest student marks
             print(“highest marks are: “, maxMarks)






































                                                                                                                127
   122   123   124   125   126   127   128   129   130   131   132