This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Operators

Learning Objectives

• Define an operator in terms of Python programming.
• Arrange the Python math operators in terms of their mathematical order of operations.
• Define precedence in terms of Python operators.
• Identify the Python equality, or comparison, operators.
• Explain the difference between mathematical and boolean operators.
• Compute three to five expressions using mathematical and boolean operators.
• Draw a diagram of the various Python operators arranging them into distinct sets, specifically, boolean and mathematical.

Operators work with one or more objects and can perform tasks such as math, comparison, and inspection.

Math

There are standard operators for arithmetic:

 Operator Description + Addition - Subrtaction * Multiplication / Division % Modulus division ** Exponent

Assignment

Additionally, you can modify a named value and assign the output of an operator to the name in one line with inline assignment operators.

```>>> a_number = 1
>>> a_number += 1
>>> a_number
2
>>> a_number *= 8
>>> a_number
16
>>> a_number **= 2
>>> a_number
256
>>> a_number /= 2
>>> a_number
128
>>> a_number %= 3
>>> a_number
2
```

Comparison and Boolean

The comparison operators compare the values of two values, variables, or expressions and return a boolean value (True or False).

Comparison and Boolean Operators
Operator Description Return
< Less than Boolean
<= Less than or equal to Boolean
>

Greater than

Boolean
>= Greater than or equal to Boolean
== Equal Boolean
!= Not equal Boolean
is same Object Boolean
is not different object Boolean
or checks whether either A or B is True Boolean
and returns True if A and B are True

Boolean

Examples of comparison operators follow.

```>>> 1 == 1
True
>>> 2 > 1
True
>>> 3 <= 4
True
>>> 2 + 5 >= 14 / 2
True
>>> (2 + 5 >= 14/2) and (2 <= 4)
True
>>> a = 5
>>> b = 9
>>> a != b
True```