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:
Here as you can see, two numbers are being added. What's happening is an operation known as addition.
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
5 are the operands of this operation.
Let's consider another example.
Below shown is the logical or operation. It returns
True if either of the operands is
Can you name the operator and operands over here?
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:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- The concatenation operator
Arithmetic operators perform mathematical operations on numbers such as addition, subtraction, exponentiation and so on.
|Addition||Adds the numbers |
|Modulo||Returns the remainder when |
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.
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
|Negated equality||Returns |
|Greater than or equal to||Return |
|Lesser than or equal to||Return |
|Greater than||Return |
|Lesser than||Return |
Logical operators evaluate given Boolean values and return
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.
|Logical NOT||Returns |
|Logical OR||Returns |
|Logical AND||Returns |
Bitwise operators evaluate given values and process their corresponding bits to return a given value.
|Bitwise NOT||Inverts each bit in |
|Bitwise OR||Processes corresponding bits in |
|Bitwise AND||Processes corresponding bits in |
|Bitwise XOR||Processes corresponding bits in |
|Bitwise left-shift||Shifts each bit in |
|Bitwise right-shift||Shifts each bit in |
There is one operator that's used to join together given sequences, that's also used in arithmetic operations. It's the
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.
|Concatenation||Concatenates the sequences |