Faster Learning Tutorials

Python Reference: Thirty-four operators in Python

Computer logic revolves around decisions made with expressions. At the center of that expression is the operator.
  1. About - Cover what operators are used for.
  2. Math - Perform arithmetic while following PEMDAS.
  3. Assignment - See how objects are named and incremented.
  4. Boolean - Find True / False answers and create program logic.
  5. Bitwise - Introduce operators performing on bit-level data.
Paul Alan Davis, CFA, November 29, 2018
Updated: November 29, 2018
Tables below help to prioritize which operators to memorize now, and which to return to later. Keep reading.

Outline Back Next

~/ home  / tech  / python  / reference  / python operators

A Guide to Understanding Operators in Python


Python Reference

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.

1. What are Python Operators For?

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.

  1. Math - For expressions involving arithmetic (7 operators).
  2. Assignment - For creating and incrementing variables and objects by assigning values (8 operators).
  3. Boolean - For the logical flow of programs where every expression results in a True or False outcome (13 operators).
  4. Bitwise - For advanced uses like cryptography, embedded languages, theoretical math and graphics where operations are performed on bits, so 1s and 0s (6 operators).

2. Python Math 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.

Python Math Operators in Version 3.5.3
Operator Priority Purpose Example
( ) High Parentheses override order (1 + 2) * 3
** High Exponent 3 ** 2
* High Multiplication 3 * 3
% High Division (Modulo) returns
5 % 2
// High Division (Floor) ignores
5 // 2
/ High Division (Regular) 3 / 3
+ High Addition 3 + 3
- High Subtraction 3 - 3

Please note, technically parentheses are not operators. Also, multiplication and division operators are grouped together, as are addition and subtraction.

3. Python Assignment Operators

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.

Python Assignment Operators in Version 3.5.3
Operator Priority Purpose Example
= High Basic variable assignment x = 5
+= High Count up x += 1
-= High Count down x -= 1
*= Mid Count by multiplication x *= 2
/= Low Count by division x /= 5
//= Low Count by floor division x //= 2
%= Low Count by modulo division x %= 2
**= Mid Count by exponent x **= 2

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.

4. Python Operators with Boolean Values

A series of operators resolve to True or False outcomes and here we organize them in three groups.

  1. Comparison Operators
  2. Logical Operators
  3. Membership Operators
a. Python Comparison Operators

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.

Python Comparison Operators in Version 3.5.3
Operator Priority Purpose Example
== High Equal to 3 == 3
!= High Not equal to 3 != 3
< High Less than 5 < 3
> High Greater than 5 > 3
<= High Less than or equal 3 <= 3
>= High Greater than or equal 3 >= 3
is Low Object equal to x = 'abc'
y = 'abc'
x is y
is not Low Object not equal to x = 'abc'
y = ['abc']
x is y
b. Python Logical Operators

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.

Python Logical Operators in Version 3.5.3
Operator Priority Purpose Examples
and High True if both
are True
1) True and True
2) True and False
3) False and True
4) False and False
or High True if one
is True
1) True or True
2) True or False
3) False or True
4) False or False
not High Opposite of the result
from a single
Boolean value or
compound expression
1) not True
2) not False
3) not 5 == 5
4) not (5 == 5) and (4 == 4)

All of the following are considered False.

  • None
  • Numeric types: 0, 0.0, 0j
  • Empty sequences: "", '', (), []
c. Python Membership Operators

Membership operators test whether the value on the left is in the sequence (list, tuple, set, etc.) on the right.

Python Membership Operators in Version 3.5.3
Operator Priority Purpose Example
in High True if value present 1 in [1, 2, 3]
not in High True if value not present 'a' not in 'apple'

5. Python Bitwise Operators

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.

Python Bitwise Operators in Version 3.5.3
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.

Related Python Content

What's Next?

Subscribe to our growing YouTube Channel, a companion to this free online educational website.

  • To see all Python reference material, click Outline.
  • To learn about keywords in Python, click Back.
  • For a list and description of built-in functions in Python, click Next.

Outline Back Next

~/ home  / tech  / python  / reference  / python operators

python operators
python documentation
python reference
python math operators
python arithmetic operators
python assignment operators
python boolean operators
python memberhip operators
python bitwise operators
python identity operators
python boolean values
python comparison operators
python mathematical operators