Python Operators

Operators enable you to do things such as calculate numbers, join strings together, assign values to a variable, compare one value to another, and more.

In computer programming, an operator is a symbol with a special meaning, which is used to carry out a particular operation.

Operators behave similar to functions, in that they take an input and produce an output, but they differ syntactally to functions. For example, in 1 + 1, the plus sign (+) is an operator that adds the number on its left with the number on its right.

Python includes operators in the following categories:

These are explained below. For a more detailed list of Python operators, see Python 3 Operators.

Arithmetic Operators

Python includes the following arithmetic operators:

Arithmetic operators take numerical values (either literals or variables) as their operands and return a single numerical value.

For example, the plus-sign (+) adds the number on its right with the number on its left. Like this:

Result
700

Here's the result of applying each of the arithmetic operators to the same operands:

Result
520
480
10000
25.0
25
0
112589990684262400000000000000000000000000000000000000000000000000

Comparison Operators

Python includes the following comparison operators:

Also known as relational operators, comparison operators allow you to compare two objects. The operation returns a boolean value of either True or False. The objects being compared don't need to have the same type.

Here's an example of using the == operator. This returns True if both operands are exactly equal, otherwise it returns False.

Result
True
False

Comparison operations in Python have the same priority, which is lower than that of any arithmetic, shifting or bitwise operation.

Logical Operators

Python includes the following logical operators:

Logical operators return either True or False depending on the value of the operands. They're used when testing for a value. For example the and logical operator can be used to test that both operands have a certain value:

Result
True
False

Assignment Operators

The assignment operators in Python are:

Assignment operators are used to assign values to variables. The basic assignment operator is the equal-sign (=), which assigns the value of its right operand/s with its left operand.

Example:

Result
3

The other assignment operators are generally shorthand for various arithmentic operations. For example, the += operator can be used to shorten a a = a + b assignment.

The following assignments are equivalent:

Result
30
30

Bitwise Operators

The following bitwise operations can be performed on integers:

For example, to return a bitwise or of 500 and 200:

Result
508

Ternary (Conditional) Operator

Most programming languages have a ternary operator, which allows you to define a conditional expression. In Python, you can define a conditional expression like this:

Basically, what this means is, the condition (C) is evaluated first. If it returns True, the result is x, otherwise it's y.

Example:

Result
Low

If this seems confusing, don't worry, we cover if statements next.

Also, check out this list of Python 3 Operators for a more detailed description of the operators available in Python.