Page 160 - Computer Science Class 11 With Functions
P. 160

The operators in Python can also be grouped by the type of operation they do, as enumerated below:
        ● Arithmetic operators                     ● Relational Operators
        ● Assignment operators                     ● Logical Operators
        ● Identity Operators                       ● Membership Operators

        7.5.1 Arithmetic Operators

        The arithmetic operators are used to perform arithmetic operations – addition, subtraction, multiplication, division,
        floor division, modulus, and exponentiation. Table 7.1 describes some of the arithmetic operators supported by Python.
                                                Table 7.1: Arithmetic Operators
           Operator          Operation                    Description                       Examples

              +       addition of numbers      Adds two numeric values            >>> -45 + 30
                                                                                      -15
                      concatenation  of  strings,  Concatenates the string on the RHS
                      lists, tuples            of the operator to the string on the  >>> 'Uttar' + 'Pradesh'
                                               LHS of the operator                    'UttarPradesh'

              -       subtraction              Subtracts the operand on the RHS of  >>> 100 - 82
                                               the operator from the operand  on       18
                                               the LHS.                           >>> -222 - 150
                                                                                      -372
              *       multiplication of numbers Multiplies  the  operands  on  either  >>> 45 * 10
                                               side of the operator                   450
                                                                                  >>> -50 * 3
                      concatenate  a string to                                        -150
                      itself multiple times    Replicates the string              >>> "Hello" * 3
                                                                                      'HelloHelloHello'
              /       division                 Divides the  operand on the  LHS of  >>> 410 / 5
                                               the operator by the operand on the       82.0
                                               RHS and yields the quotient as a float   >>> -75 / 6
                                               value                                  -12.5
             //       floor  division  or  integer  Divides the operand on the LHS side  >>> 39 // 4
                      division                 of the operator by the operand on the      9
                                               RHS  and  yields  the  quotient.  When  >>> -75 // 6
                                               the operands include a floating point      -13
                                               number, the result is a floating point  >>> 8 // 2.5
                                               number whose value is the greatest      3.0
                                               integer <= the number resulting from  >>> -8 // 2.5
                                               division.                              -4.0
              %       modulus                  Divides the operand (say m) on the  >>> 75 % 4
                                               LHS of the operator by the operand      3
                                               (say n) on the RHS of the operator,  >>> 12.5 % 4
                                               and  yields  the  remainder  (r),       0.5
                                               according to the equation:         >>> -75 % 4

                                               r = m - (m // n) * n                   1
             **       exponentiation           Computes a                         >>> 5 ** 3
                                                          b
                                                                                      125
                                                                                  >>> -4 ** 4
                                                                                      -256
                                                                                  >>> 4 ** 4
                                                                                      256

         158   Touchpad Computer Science-XI
   155   156   157   158   159   160   161   162   163   164   165