Page 62 - Information_Practice_Fliipbook_Class11
P. 62

3.5 Be Careful while Naming Variables

        Although Python allows you to use predefined identifiers for any purpose, it is definitely a bad idea to do so because
        you would lose access to Python's predefined identifiers. For example,
         >>> print = 23
         >>> print('Hello')
              Traceback (most recent call last):
                File "<pyshell#12>", line 1, in <module>
                  print('Hello')
              TypeError: 'int' object is not callable
        Note that on executing the statement:

              print = 23
        the identifier print refers to an integer. So, the meaning assigned to the identifier print by Python has been
        overwritten. So, we are unable to print the string 'Hello' because an attempt to execute the statement
         >>> print('Hello')
        yields an error.
        Finally, let us see some more examples of the assignment statements:
        num = 10                                   (the variable num is assigned the value 10)

        city = 'Delhi'                             (the variable city is assigned the string 'Delhi')
        price = 150.50                             (the variable price is assigned the floating-point value 150.50)
        discount = 10/100*price                      (the  variable  discount  is  assigned  the  result  of  evaluating  the
                                                   expression 10/100*price.



                               num                                         10                1000X



                                city                                     "Delhi"             2000X




                               price                                     150.50              3000X


                                                                          15.05
                              discount                                                       4000X
                                                                 (result of 10/100*150.50)

                                               Fig 3.2: Variables and their values

        To experiment with variables and their values, let us write a Python program.

         Program 3.1 Playing with variables and data values

                     1  #Objective: To experiment with variables and their values
                     2  city = 'Delhi'

                     3  interestRate = 9.5
                     4  print('city =', city)
                    5   print('interestRate =', interestRate)

        Program  3.1  has  been  presented  in  the  form  of  two  columns.  While  the  second  column  comprises  the  lines  of
        Python code, the first column shows the line numbers of the Python program. The first column does not form part
        of the program and is included to explain the program. The first line of the program begins with a hash character.

          48   Touchpad Informatics Practices-XI
   57   58   59   60   61   62   63   64   65   66   67