Page 64 - Information_Practice_Fliipbook_Class11
P. 64

Let us learn the different ways to make multiple assignments. For example, suppose we are given a square matrix, and
        we want to find the sum of all elements of the matrix, the sum of the main diagonal elements (trace) of the matrix,
        the sum of the corner elements of the matrix, and the sum of the  antidiagonal elements of the  matrix. To do these
        computations, we would like to initialise these quantities as 0. The following assignment statement will assign the
        value 0 to variables sumMatrix, trace, sumCorners, and sumAntidiag.
        sumMatrix = trace = sumCorners = sumAntidiag = 0
        In general,  Python uses the following syntax for assigning the same value to multiple variables
        <identifier_1> = <identifier_2> = ... = <identifier_N> =  <value>
        The above description extends the notation for Python syntax, described earlier. While the text in the angular brackets
        is replaced by the appropriate Python expressions, the keywords and the operators appear as they are. The ellipses
        indicate multiple occurrences of a syntactic construct. Let us examine the following Python statement in light of the
        above description of the multiple assignments:

        sumMatrix = trace = sumCorners = sumAntidiag = 0
        In the above statement, <identifier_1> is replaced by asumMatrix, the equal to symbol (=) appears in the
        Python statement as it appears in the syntax description, next, the syntax <identifier_2> ... = <identifier
        _N> implies that a variable name may be followed by the equal to symbol any number of times. So, it is fine to follow
        sumMatrix = by trace = sumCorners = sumAntidiag =. Finally, <value> in the syntax description
        is replaced by 0 (a valid value) to form the assignment statement:

        sumMatrix = trace = sumCorners = sumAntidiag = 0
        Assigning multiple values to multiple variables: Sometimes we need to assign multiple values to multiple variables.
        The following syntax enables us to assign multiple values (possibly different, but not necessarily) to multiple variables.
        <identifier_1>,  <identifier_2>,  ...,  <identifier_N>  =    <value1>,  <value2>,  ...,,
        <valueN>
        The above statement will assign different values <value1>, <value2>, ...,, <valueN> to the variables
        <identifier_1>,  <identifier_2>,  ...,  <identifier_N>,  respectively.  Consider  the  following
        example:
         >>> day, month, year = 15, 8, 2022
         >>> print('Independence Day:', day, month, year)
              Independence Day: 15 8 2022

        In the above example, the variable day is assigned the value 15,  the variable month  is assigned the value 8,
        and the variable year is assigned the value 2022. An expression appearing on the right-hand side (RHS) of the
        assignment operator is evaluated first, and thereafter, the result is assigned to the respective variable on the left-hand
        side (LHS). The expressions are evaluated in the order of left to right. This is explained in the example below.
         >>> english, maths, physics, chemistry, computerSc = 80, 90, 85, 95, 90
         >>> totalMarks = english + maths + physics + chemistry +  computerSc
         >>>  print('english:',  english,  'maths:',  maths,  'physics:',  physics,  'chemistry:',
              chemistry, 'computer sc:', computerSc)
                  english: 80 maths: 90 physics: 85 chemistry: 95 computer sc: 90
         >>> print('total marks:', totalMarks)
              total marks: 440

                Give one word answer to what each of the following is called:
                1.  Symbols used to separate various tokens in a statement.
                2.  Constant or a fixed value of a data type.
                3.  An identifier that denotes a data value in memory.
                4.  A function that gives the unique identity of an object.




          50   Touchpad Informatics Practices-XI
   59   60   61   62   63   64   65   66   67   68   69