Page 141 - CodePilot V5.0 C6
P. 141

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

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

                      //      Floor division    Divides the left operand by the right and        x // y         1
                                                discards the fraction.


                  Program 3    To demonstrate the use of all the arithmetic operations.


                     Program3.py
                  File  Edit  Format   Run    Options  Window     Help
                                                                                    Write a Python program to
                  x = 5                                                              calculate simple interest:
                  y = 3
                                                                                    Use the formula (P * R * T) /
                  result_add = x + y # Addition                                   100, where P is the principal, R
                  print("Addition:", result_add)                                    is the rate and T is the time.
                  result_sub = x - y # Subtraction
                  print("Subtraction:", result_sub)

                  result_mul = x * y # Multiplication
                  print("Multiplication:", result_mul)

                  result_div = x / y # Division
                  print("Division:", result_div)                                    Output
                  result_mod = x % y # Modulus                                   Addition: 8
                  print("Modulus:", result_mod)                                  Subtraction: 2

                  result_exp = x ** y # Exponentiation                           Multiplication: 15
                  print("Exponentiation:", result_exp)                           Division: 1.6666666666666667
                                                                                 Modulus: 2
                  result_floor = x // y # Floor Division
                                                                                 Exponentiation: 125
                  print("Floor Division:", result_floor)
                                                                                 Floor Division: 1


                 RELATIONAL OPERATORS

                 Relational operators compare values on both sides and return True or False.

                 The following table describes the relational operators:
                                                                                          Example
                  Operator         Name                     Description                                   Output
                                                                                         (x = 5, y = 3)
                      ==       Equal to        Returns True if both  operands are           x == y          False
                                               equal.
                      !=       Not equal to    Returns True if both operands are not        x != y          True
                                               equal.
                       >       Greater than    Returns True if the left operand  is          x > y          True
                                               greater than the right.




                                                                                                                  139
                                                                                              Python–Start to Code
   136   137   138   139   140   141   142   143   144   145   146