Introduction

Back in the Python Basics chapter, we saw how to add together two numbers using the + symbol, how to subtract them using the - symbol, and so on.

These symbols, known as operators, along with the function they perform on given values, known as operands, is what we will study in this chapter. We'll explore a wide variety of operators in Python, see what are they used for, and finally consider examples for each set of operators.

What are operations?

Understanding what exactly is an operation is very easy if we take the example below:

The addition operation
The addition operation

Here as you can see, two numbers are being added. What's happening is an operation known as addition.

An operation can be thought of as a process or a function that's performed on given values.

Every operation has a corresponding operator that operates on given operands. To understand this we first need to understand what exactly is meant by the terms 'operator' and 'operands'.

Taking the example above, 3 + 5 represents the addition operation. Here + is the operator of the addition operation, whereas 3 and 5 are the operands of this operation.

An operator is a symbol, or keyword, that represents a given operation.
An operand is a value on which a given operation takes place.

Let's consider another example.

Below shown is the logical or operation. It returns True if either of the operands is True, and False otherwise.

Can you name the operator and operands over here?

True and False are the operands on which the logical OR operation takes place, while the keyword or is the operator that represents the logical OR operation.

Operators in Python

In Python, there are a handful of operators as we shall see in the sections that follow, each serving a specific job. We've divided the operators based on the type of operation they perform.

This gives us the following categories of operators:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Bitwise operators
  6. The concatenation operator

Arithmetic operators

Arithmetic operators perform mathematical operations on numbers such as addition, subtraction, exponentiation and so on.

OperationOperatorSyntaxPurpose
Addition+x + yAdds the numbers x and y
Subtraction-x - ySubtracts y from x
Multiplication*x * yMultiplies x with y
Division/x / yDivides x by y
Exponentiation**x ** yRaises x to the power of y
Modulo%x % yReturns the remainder when x is divided by y

Assignment operators

Assignment operators assign a value to an identifier in Python. The best example is of the = equals sign.

Compound assignment operators perform an operation on the value and then assign it to the given identifer. They are frequently used to shorten reassignment statements.

OperationOperatorSyntaxPurpose
Assignment=identifier = valueAssigns value to identifier
Addition-assignment+=x += yEvaluate x + y, then assign to x
Subtraction-assignment-=x -= yEvaluate x - y, then assign to x
Multiplication-assignment*=x *= yEvaluate x * y, then assign to x
Division-assignment/=x /= yEvaluate x / y, then assign to x
Exponentiation-assignment**=x **= yEvaluate x ** y, then assign to x
Modulo-assignment%=x %= yEvaluate x % y, then assign to x

Comparison operators

Comparison operator are used throughout Python to compare two given values together.

The most basic one of comparison operators is the equality operator, denoted by == (double equals sign). It compares two values and returns True if they both are equal to one another; and False otherwise.

OperationOperatorSyntaxPurpose
Equality==x == yReturns True if x is equal to y, and False otherwise.
Negated equality!=x != yReturns True if x is not equal to y, and False otherwise.
Greater than or equal to>=x >= yReturn True if x is greater than or equal to y, and False otherwise.
Lesser than or equal to<=x <= yReturn True if x is lesser than or equal to y, and False otherwise.
Greater than>x > yReturn True if x is greater than y, and False otherwise.
Lesser than<x < yReturn True if x is lesser than y, and False otherwise.

Logical operators

Logical operators evaluate given Boolean values and return True or False based on their truthy/falsy values.

Logical operators are frequently used in conditional statements to combine the results of two or more Boolean values.

OperationOperatorSyntaxPurpose
Logical NOTnotnot xReturns True if x is False, and otherwise False
Logical ORorx or yReturns True if x, or y or both are True, and otherwise False
Logical ANDandx and yReturns True if x and y are both True, and otherwise False

Bitwise operators

Bitwise operators evaluate given values and process their corresponding bits to return a given value.

OperationOperatorSyntaxPurpose
Bitwise NOT~~xInverts each bit in x and returns the result.
Bitwise OR|x | yProcesses corresponding bits in x and y using the OR operation, and returns the result.
Bitwise AND&x & yProcesses corresponding bits in x and y using the AND operation, and returns the result.
Bitwise XOR^x ^ yProcesses corresponding bits in x and y using the XOR operation, and returns the result.
Bitwise left-shift<<x << yShifts each bit in x to the left by y places.
Bitwise right-shift>>x >> yShifts each bit in x to the right by y places, adding zeroes to the right.

Concatenation operator

There is one operator that's used to join together given sequences, that's also used in arithmetic operations. It's the + symbol.

When used on strings, for instance, + serves to join together the strings into one single string. Similarly, when used on lists, + adds together the given lists and returns back the result.

In programming terminology, this is referred to as concatenation. Concatenation is to add together two sequences to produce a single sequence.

OperationOperatorSyntaxPurpose
Concatenation+a + bConcatenates the sequences a and b together.