Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 123 - 2612_Digimind
P. 123

    In the above program, Python converts the string "100" into an integer using int(), allowing it
                     to be used in mathematical operations. The type() function confirms that the variable is now of
                     type <class 'int'>.

                               OPERATORS

                     Operators are symbols that perform mathematical, logical or relational operations on values
                     called operands. For example, in 15 + 7, 15 and 7 are operands and + is the operator. Python has
                     different categories of operators.

                     ARITHMETIC OPERATORS
                     Arithmetic  operators perform arithmetic  operations  between  two operands. The  arithmetic

                     operators are defined in the following table:

                                                                                        Example
                       Operator          Name                 Description              (x = 5, y = 3)       Output

                                                       Adds values on either side of
                            +         Addition                                              x + y               8
                                                       the operator.

                                                       Subtracts the right operand
                            -         Subtraction                                           x - y               2
                                                       from the left operand.

                                                       Multiplies values on either
                            *         Multiplication                                        x * y              15
                                                       side of the operator.

                                                       Divides the left operand by
                            /         Division                                              x / y            1.6667
                                                       the right and returns a float.

                                                       Divides the left operand by
                            %         Modulus          the right and returns the           x % y                2
                                                       remainder.

                                                       Raises the left operand to the
                            **        Exponentiation                                       x ** y              125
                                                       power of the right operand.

                                                       Divides the left operand by

                            //        Floor division   the right and discards the          x // y               1
                                                       fraction.

                                                                                        Tokens and Data Types in Python  121

| 118 119 120 121 122 123 124 125 126 127 128 |