2.3 Arithmetic

Python operation Arithmetic operator Python expression
Addition + f + 7
Subtraction p - c
Multiplication * b * m
Exponentiation ** x ** y
True division / x / y
Floor division // x // y
Remainder (modulo) % r % s

Multiplication (*)

  • Python uses the asterisk (*) multiplication operator:
In [1]:
7 * 4
Out[1]:
28

Exponentiation (**)

  • The exponentiation (**) operator raises one value to the power of another.
In [2]:
2 ** 10
Out[2]:
1024
  • To calculate the square root, use the exponent 1/2 or 0.5.
In [3]:
9 ** (1 / 2)
Out[3]:
3.0

True Division (/) vs. Floor Division (//) (1 of 3)

  • True division (/) divides a numerator by a denominator and yields a floating-point number.
In [4]:
7 / 4
Out[4]:
1.75

True Division (/) vs. Floor Division (//) (2 of 3)

  • Floor division (//) divides a numerator by a denominator, yielding the highest integer that’s not greater than the result.
  • Truncates (discards) the fractional part.
In [5]:
7 // 4
Out[5]:
1
In [6]:
3 // 5
Out[6]:
0
In [7]:
14 // 7
Out[7]:
2

True Division (/) vs. Floor Division (//) (3 of 3)

In [8]:
-13 / 4
Out[8]:
-3.25
In [9]:
-13 // 4
Out[9]:
-4

Exceptions and Tracebacks (1 of 3)

  • Dividing by zero with / or // is not allowed and results in an exception.
In [10]:
123 / 0
------------------------------------------------------------------------
ZeroDivisionError                      Traceback (most recent call last)
<ipython-input-10-76e1a9ab9410> in <module>
----> 1 123 / 0

ZeroDivisionError: division by zero

Exceptions and Tracebacks (2 of 3)

  • Exceptions produce tracebacks.
  • The line that begins with ----> shows the code that caused the exception.
  • The error message at the bottom of the traceback shows the exception that occurred, followed by a colon (:) and an error message with more information about the exception..

Exceptions and Tracebacks (3 of 3)

  • An exception occurs if you try to use a variable that you have not yet created.
In [11]:
z + 7
------------------------------------------------------------------------
NameError                              Traceback (most recent call last)
<ipython-input-11-2ca5f6c7aca2> in <module>
----> 1 z + 7

NameError: name 'z' is not defined

Remainder Operator

  • Remainder operator (%) yields the remainder after the left operand is divided by the right operand.
In [12]:
17 % 5
Out[12]:
2
In [13]:
7.5 % 3.5
Out[13]:
0.5

Straight-Line Form

  • Algebraic expressions must be typed in straight-line form using Python’s operators.

Grouping Expressions with Parentheses

  • Parentheses group Python expressions, as in algebraic expressions.
In [14]:
10 * (5 + 3)
Out[14]:
80
In [15]:
10 * 5 + 3
Out[15]:
53

Operator Precedence Rules (1 of 2)

  • Generally the same as those in algebra:
    1. Expressions in parentheses evaluate first, so parentheses may force the order of evaluation to occur in any sequence you desire. Parentheses have the highest level of precedence. In expressions with nested parentheses, such as (a / (b - c)), the expression in the innermost parentheses (that is, b - c) evaluates first.
    2. Exponentiation operations evaluate next. If an expression contains several exponentiation operations, Python applies them from right to left.

Operator Precedence Rules (1 of 2)

  1. Multiplication, division and modulus operations evaluate next. If an expression contains several multiplication, true-division, floor-division and modulus operations, Python applies them from left to right. Multiplication, division and modulus are “on the same level of precedence.”
  2. Addition and subtraction operations evaluate last. If an expression contains several addition and subtraction operations, Python applies them from left to right. Addition and subtraction also have the same level of precedence.

Operator Grouping

  • When we say that Python applies certain operators from left to right, we are referring to the operators’ grouping.
  • All Python operators of the same precedence group left-to-right except for the exponentiation operator (**), which groups right-to-left.

Redundant Parentheses

  • Can use redundant parentheses to group subexpressions to make an expression clearer.

Operand Types

  • If both operands are integers, the result is an integer—except for the true-division (/) operator, which always yields a floating-point number.
  • If both operands are floating-point numbers, the result is a floating-point number.
  • Mixed-type expressions produce floating-point results.

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 2 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.