/ factorpad.com / tech / python / reference / python-operators.html
An ad-free and cookie-free website.
Beginner
This Python reference offers programmers a quick way to learn Python and also serves as a source for reminders.
While the version documented here is Python 3.5.3, most of this is suitable for other versions of Python 3. Check your version for details.
At the center of an expression like
x = 5
sits the equal symbol which is
an operator. Each of the 34 operators we cover here has a specific
meaning and purpose. Those fluent in Python have memorized most of
these, but it only takes an understanding of about 20 to do amazing
things with Python.
The first step is to categorize them. We group Python operators into four groups, starting with easiest.
True
or False
outcome (13 operators).There are 7 math operators in Python, also referred to as arithmetic operators elsewhere.
The following table lists operators by the order of operation for math,
commonly referred to as PEMDAS. This order can be overridden with
parentheses ( )
, otherwise,
calculations are processed from left to right.
Operator | Priority | Purpose | Example |
---|---|---|---|
( ) |
High | Parentheses override order | (1 + 2) * 39 |
** |
High | Exponent | 3 ** 2 9 |
* |
High | Multiplication | 3 * 3 9 |
% |
High | Division (Modulo) returns remainder |
5 % 2 1 |
// |
High | Division (Floor) ignores remainder |
5 // 2 2 |
/ |
High | Division (Regular) | 3 / 31 |
+ |
High | Addition | 3 + 36 |
- |
High | Subtraction | 3 - 30 |
Please note, technically parentheses are not operators. Also, multiplication and division operators are grouped together, as are addition and subtraction.
One of the first concepts beginners learn in Python is variable
assignment, as in x = 5
.
Python also offers "augmented assignment" operators which provide a shorthand way to increment counts. These include a mathematical operation and an assignment together.
As an example, let's use the most common type used to increment a
count up using addition. The long form would look like
x = x + 1
with the shorthand version
reading x += 1
.
Here, assuming we start with x = 5
the next count would be 6
from the
mathematical operation of addition and an in-place assignment.
For the augmented assignment examples below assume
x = 5
. The answer shows the next count
in the sequence.
Operator | Priority | Purpose | Example |
---|---|---|---|
= |
High | Basic variable assignment | x = 5 |
+= |
High | Count up | x += 16 |
-= |
High | Count down | x -= 14 |
*= |
Mid | Count by multiplication | x *= 210 |
/= |
Low | Count by division | x /= 51.0 |
//= |
Low | Count by floor division | x //= 22 |
%= |
Low | Count by modulo division | x %= 21 |
**= |
Mid | Count by exponent | x **= 225 |
Recall that the division of an integer in Python 3 will result in a float.
Bitwise augmented assignment operators exist but are beyond our scope here.
A series of operators resolve to True
or
False
outcomes and here we organize
them in three groups.
The ==
operator is a test for equality
of values, whereas !=
tests for
non-equality. The remaining four with greater than
>
and less than
<
symbols are similar
to other programming languages.
The is
and
is not
operators compare whether two
objects occupy the same memory location. So objects of different
types do not compare as equal.
Operator | Priority | Purpose | Example |
---|---|---|---|
== |
High | Equal to | 3 == 3True |
!= |
High | Not equal to | 3 != 3False |
< |
High | Less than | 5 < 3False |
> |
High | Greater than | 5 > 3True |
<= |
High | Less than or equal | 3 <= 3True |
>= |
High | Greater than or equal | 3 >= 3True |
is |
Low | Object equal to |
x = 'abc' y = 'abc' x is y True
|
is not |
Low | Object not equal to |
x = 'abc' y = ['abc'] x is y False
|
The and
,
or
operators test compound expressions.
These can be complex expressions or simply the Boolean values
True
and
False
as the examples in the table
below demonstrate.
The not
operator reverses a single
Boolean Value, or a compound expression.
Operator | Priority | Purpose | Examples |
---|---|---|---|
and |
High | True if both expressions are True |
1) True and TrueTrue 2) True and False False 3) False and True False 4) False and False False
|
or |
High | True if one expression is True |
1) True or TrueTrue 2) True or False True 3) False or True True 4) False or False False
|
not |
High | Opposite of the result from a single Boolean value or compound expression |
1) not TrueFalse 2) not False True 3) not 5 == 5 False 4) not (5 == 5) and (4 == 4) False |
All of the following are considered
False
.
None
Membership operators test whether the value on the left is in the sequence (list, tuple, set, etc.) on the right.
Operator | Priority | Purpose | Example |
---|---|---|---|
in |
High | True if value present | 1 in [1, 2, 3]True |
not in |
High | True if value not present | 'a' not in 'apple'False |
The least common type of operator in Python is the Bitwise type which only operates at the bit level, so 1s and 0s. These are commonly used when programming drivers, protocols and graphics at a low-level close to the processor in machine code or assembly languages.
An introduction can be found at the Wikipedia article Bitwise operation.
Because these are advanced features, summaries are provided.
Operator | Priority | Summary |
---|---|---|
& |
Low | Bitwise AND performs a logical AND operation |
| |
Low | Bitwise OR performs a logical inclusive OR operation |
^ |
Low | Bitwise XOR performs a logical exclusive OR operation |
~ |
Low | Bitwise NOT performs a logical negation on each bit |
>> |
Low | Shift bits to the right |
<< |
Low | Shift bits to the left |
Python also offers bitwise augmented assignment operators. See the official documentation for details.
Subscribe to our growing YouTube Channel, a companion to this free online educational website. Follow @factorpad for reminders of new content.
/ factorpad.com / tech / python / reference / python-operators.html
A newly-updated free resource. Connect and refer a friend today.