Page 399 - Computer_Science_F5
P. 399

Computer Science  consisting of four spaces, to define code  Variables
           blocks within functions, loops, and
                                                     (a) Variable declaration
           conditional  statements.  By adhering
                                                     Python does not require a specific
           to consistent indentation  practices, the
                                                     command for declaring a variable. In
           structure  and  hierarchy  of  the  code
          FOR ONLINE READING ONLY
           become clear and easily understandable.
                                                     when you assign a value to it for the
           For example,                              Python, a variable is automatically created
                                                     first time. There is no need to explicitly
              if x > 100:                            declare the variable beforehand. Once
                  print(“x is greater than 100”)     a value is assigned to a variable, it is
              else:                                  stored in memory and can be referenced
                  print(“x is not greater than 100”)  by its assigned name throughout the
                                                     program. This dynamic nature of variable
           In this example,  the  two prints has     creation simplifies the coding process
           gone inside to the right by four space    and allows for more flexibility in Python
           leaving if and else at the left. If you skip   programming. For example,
           indentation,  Python will  give  you an         x = 256
           error message, For example:                     y = “Mwanamweru”

              if x > 100:                                  print(x)
              print(“x is greater than 100”)               print(y)
              else:
              print(“x is not greater than 100”)     (b) Variable naming
                                                     In  Python, variables  can have  both
           This program will give you an error. It   short and descriptive names. However,
           is important to use the same number of    there are certain rules and guidelines to
           spaces in the same block of code so that   follow. These include:
           Python will not give you an error         (i)  A variable name must begin with a
                                                         letter or an underscore character.
           (b) Comments                              (ii)  It cannot start with a number.
           In Python, comments are denoted by the #
           symbol and serve the purpose of explaining   (iii) It can only consist of alphanumeric
           code or adding notes. Comments are not        characters (letters and numbers) and
           interpreted by the Python interpreter and     underscores.
           are solely meant for human readers. They   (iv)  Variable names are case-sensitive,
           provide additional context, improve code      meaning that variables with different
           readability, and can serve as reminders or    capitalization are considered distinct
           explanations for future reference.            (such as age, Age, and AGE are
           For example,                                  treated as separate variables).
                                                     (v)  Python keywords cannot be used
            #This is a comment for this code
                                                         as variable names, as they have

                                                 390
                                                                for Advanced Secondary Schools



     Computer Science Form 5.indd   390                                                     23/07/2024   12:34
   394   395   396   397   398   399   400   401   402   403   404