Page 401 - Computer_Science_F5
P. 401

print(“x is negative”)
       Computer Science  Data types                  (b) Looping Constructs: for and while
           Python supports several  built-in  data
                                                        else:
           types including numeric types( such as
                                                            print(“x is zero”)
           int, float, complex), sequence types (such
           as  string,  lists,  turples  and  dictionary),
          FOR ONLINE READING ONLY
           Boolean type (such as bool) and other
           type (Such as null )                      Python  offers two  primary  looping
                                                     constructs: the “for” loop and the “while”
                                                     loop. The “for” loop is utilised to iterate
           Control flow and decision making in       over a sequence, which can be a list,
           Python                                    tuple, string, or any other iterable object.
           (a) Conditional Statements: if, elif, else  It allows you to perform a specific set
           In  Python, conditional  statements are   of actions for each item in the sequence
           used to execute different blocks of code   or execute a fixed number of iterations.
           based on specific conditions. The syntax   This loop is particularly  useful when
           for the “if” statement is as follows:     you know  the number of iterations  in
                                                     advance  or when you want to process
           if condition:                             each element in a given sequence. For
               # Block of code to execute if condition   example,
           is True
                                                      animals = [‘lion’, ‘leopard’, ‘buffalo’]
           The “elif” statement, short for “else if,”   for animal in animals:
           enables you to evaluate multiple conditions
           consecutively. It allows you to check for       print(animal)
           additional conditions if the conditions in
           the preceding “if” statement(s) are not  The  “while” loop  is employed  to
           met.                                      execute  a block  of code  repeatedly  as
                                                     long  as  a  specified  condition  remains
           On the other hand, the “else” statement is   true. It allows you to create a loop that
           used to execute a block of code when the   continues until the condition evaluates
           conditions of the “if” or “elif” statements   to  false.  This type  of  loop  is  useful
           are evaluated as False. It provides an    when the number of iterations  is not
           alternative code path to be executed when   known in advance, and the loop should
           the previous conditions are not satisfied.   continue until a certain condition is met
           Here is an example that demonstrates the   or becomes false. For example,
           usage of these statements:
                                                       count = 0
               x = 10                                  while count < 7:

               if x > 0:
                   print(“x is positive”)                  print(count)
               elif x < 0:                                 count += 1


                                                 392
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   392                                                     23/07/2024   12:34
   396   397   398   399   400   401   402   403   404   405   406