Table of Contents

## What Are Python Operators?

Python operators are special syntactical sugar to run basic operations without calling their respective methods. For example, you can use the `+`

operator in `a + b`

instead of the more clunky `.add()`

method in `a.add(b)`

. Each operator has a unique symbol that is placed between the two arguments called *operands*.

A simple example is given next where the + operator is applied to operands 40 and 2 to generate the sum 42:

>>> 40 + 2 42

## Comparison Operators

Comparison operators are applied to comparable objects and they return a Boolean value (`True`

or `False`

).

Operator | Name | Description | Example |
---|---|---|---|

> | Greater Than | Returns `True` if the left operand is greater than the right operand | `3 > 2 == True` |

< | Less Than | Returns `True` if the left operand is smaller than the right operand | `3 < 2 == False` |

== | Equal To | Returns `True` if the left operand is the same as the right operand | `(3 == 2) == False` |

!= | Not Equal To | Returns `True` if the left operand is not the same as the right operand | `(3 != 2) == True` |

>= | Greater Than or Equal To | Returns `True` if the left operand is greater than or equal to the right operand | `(3 >= 3) == True` |

<= | Less Than or Equal To | Returns `True` if the left operand is less than or equal to the right operand | `(3 <= 2) == False` |

## Arithmetic Operators

Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers.

Operator | Name | Description | Example |
---|---|---|---|

+ | Addition | Calculating the sum of the two operands | `3 + 4 == 7` |

– | Subtraction | Subtracting the second operand from the first operand | `4 - 3 == 1` |

* | Multiplication | Multiplying the first with the second operand | `3 * 4 == 12` |

/ | Division | Dividing the first by the second operand | `3 / 4 == 0.75 ` |

% | Modulo | Calculating the remainder when dividing the first by the second operand | `7 % 4 == 3` |

// | Integer Division, Floor Division | Dividing the first operand by the second operand and rounding the result down to the next integer | `8 // 3 == 2` |

** | Exponent | Raising the first operand to the power of the second operand | `2 ** 3 == 8` |

## Logical Operators

Logical operators are shortcuts to perform Boolean comparisons, but can be used on integers and other objects as well.

Operator | Description | Example |
---|---|---|

`and` | Returns `True` if both operands are `True` , and `False` otherwise. | `(True and True) == True` |

`or` | Returns `True` if at least one of the two operands is `True` , and `False` otherwise. | `(False or True) == True` |

`not` | Returns `True` if the single operand is `False` , and `False` otherwise. | `(not True) == False` |

## Bitwise Operators

Bitwise operators perform operations on the binary (bit) representation of integers.

**Background**: Each integer is first written as a binary number that is a sequence of digits 0 or 1. For example:

- 0 is written as “0”
- 1 is written as “1”
- 2 is written as “10”
- 3 is written as “11”
- 4 is written as “100”
- 5 is written as “101”

The bitwise operators are then applied on those binary representations. The resulting binary number is then converted back to the integer representation (decimal number).

In the following table, we exemplify all bit-wise operators with variables `x`

and `y`

defined as follows:

>>> x = 4 # 100 >>> y = 5 # 101

As a comment, we also provide the binary representation `100`

for the decimal integer `4`

, and `101`

for the decimal integer `5`

.

Operator | Name | Description | Example |
---|---|---|---|

& | Bitwise AND | Performs the AND operation on a bit-by-bit basis | `x & y` |

| | Bitwise OR | Performs the OR operation on a bit-by-bit basis | `x | y` |

~ | Bitwise NOT | Performs the Bitwise NOT – i.e., inverts each bit so that 0 becomes 1 and 1 becomes 0. Same as `-x-1` . | `~x` |

^ | Bitwise XOR | Performs the “exclusive or” operation on a bit-by-bit basis | `x ^ y` |

>> | Bitwise right shift | `x >> z` shifts `x` by `z` positions to the right. | `x >> 2` |

<< | Bitwise left shift | Shifts each bit one position to the left. | `x << 2` |

## In-Place Assignment Operators

In-place assignment operators (also called *compound* assignment operators) perform an operation in-place on a variable provided as first operand. They overwrite the value of the first operand variable with the result of the operation when performing the operator without assignment. For example, `x += 3`

is the same as `x = x + 3`

of first calculating the result of `x +3`

and then assigning it to the variable x.

Operator | Name | Short Example | Equivalent Long Example |
---|---|---|---|

`=` | In-place Assignment | x = 3 | x = 3 |

`+=` | In-place Addition | x += 3 | x = x + 3 |

`-=` | In-place Subtraction | x -= 3 | x = x – 3 |

`*=` | In-place Multiplication | x *= 3 | x = x * 3 |

`/=` | In-place Division | x /= 3 | x = x / 3 |

`%=` | In-place Modulo | x %= 3 | x = x % 3 |

`//=` | In-place Integer Division | x //= 3 | x = x // 3 |

`**=` | In-place Power | x **= 3 | x = x ** 3 |

`&=` | In-place Logical And | x &= 3 | x = x & 3 |

`|=` | In-place Logical Or | x |= 3 | x = x | 3 |

`^=` | In-place Logical XOR | x ^= 3 | x = x ^ 3 |

`>>=` | In-place Bitwise Shift Right | x >>= 3 | x = x >> 3 |

<<= | In-place Bitwise Shift Left | x <<= 5 | x = x << 5 |

## Identity Operators

Python has two identity operators: `is`

and `is not`

. They are used to check if two values or variables reside at the same memory location, i.e., refer to the same object in memory. However, if two variables are equal, this doesn’t imply that they are identical. For example, two lists `[1, 2, 3]`

and `[1, 2, 3]`

may be different objects (not identical) but they’re equal in value.

Operator | Meaning | Example |
---|---|---|

`is` | Returns `True` if both operands refer to the same object in memory and `False` otherwise. | `x is True` |

`is not` | Returns `False` if both operands refer to the same object in memory and `True` otherwise. | `x is not True` |

## Membership Operators

Python has two membership operators `in`

and `not in`

that test whether a value or variable exists in a sequence such as string, list, tuple, set, and dictionary.

Operator | Meaning | Example |
---|---|---|

`in` | True if value/variable is found in the sequence | 5 in x |

`not in` | True if value/variable is not found in the sequence | 5 not in x |

## Python Operators Precedence

If you have multiple operators in a single expression, it is important to know which operator comes first. For example, consider the expression 2 * 3 + 4. Does Python calculate (2 * 3) + 4 or 2 * (3 + 4)? Depending on the operator precedence, the result would be 10 or 14.

The following table describes the operator precedence relationship. The topmost operator has the highest priority.

Name | Operators |
---|---|

Exponentiation | `**` |

Bitwise Not | `~` |

Multiplication, Division, Modulo, Integer Division | `* / % //` |

Addition, Substraction | `+ -` |

Bitwise Shift Right and Left | `>> <<` |

Bitwise AND | `&` |

Bitwise XOR and OR | `^ |` |

Comparison | `<= < > >=` |

Equality | `<> == !=` |

In-Place Assignments | `= %= /= //= -= += *= **=` |

Identity | `is is not` |

Membership | `in not in` |

Logical | `not or and` |

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.