Page 63 - Information_Practice_Fliipbook_Class11
P. 63

Python ignores text on a line beginning with the hash character and up to the end of the line. The text beginning with
            a hash character is called a comment. Comments are freely used in a program to facilitate the reading of the program.
            In line 2, the variable city is assigned the value 'Delhi'. We can also say that the variable city is mapped to the
            string object  'Delhi'. Similarly, in line 3, the variable interestRate is assigned the value 9.5. Lines 4 and 5 are
            instructions to display the values of variables city and interestRate, respectively. On the execution of Program
            3.1, Python will produce the following output:
            city = Delhi
            interestRate = 9.5
            Program 3.2 The result of applying an arithmetic operation is assigned to the variable amount.

                         1 #Objective: Given the unit price and quantity purchased,

                         2 # compute and display the amount payable
                         3 unitPrice = 50.5
                         4 quantity = 4
                         5 amount = unitPrice * quantity
                         6 print('amount =', amount)
            When an expression appears on the right-hand side of the operator, as in line 5 of program 3.2, Python first evaluates
            the expression, and then the assignment operation is executed. Thus, the variable amount is assigned the value
            202.0 on the execution of the assignment statement at line 5. Finally, the value of the amount is displayed on the
            execution of line 6.
            Assign values to variables prior to use: A variable MUST be assigned a value, before it is used in an expression. Failure
            to do so will result in an error, as shown in Program 3.3 below.

            Program 3.2: Illustrates another important point that we should be careful to use the variable names consistently.
            Switching the variable name itemPrice to price resulted in an error.








                                                      Results in Error as price is
                                                           not declared



















            3.6 Multiple Assignments

            Assigning a value to several variables: Sometimes, we need to assign the same value (say, 0 or 100) to several variables.
            Python allows us to use multiple variables in a single assignment statement by using a comma(,) as a delimiter.





                                                                                    Basics of Python Programming  49
   58   59   60   61   62   63   64   65   66   67   68