Operators in Python
Understand the various operators in Python and how to use them in expressions and calculations.
Introduction to Operators
Operators are characters or symbols that are used to perform various operations in Python.
For example, the '+' operator in Mathematics is used to add two values together and the same can be done in Python.
4 + 6There are several kinds of operators in Python and we will discuss about them in detail in this lesson:
- Arithmetic Operators (+, -, /, *, //, %)🥷Ninja Jump
- Boolean Operators (and, or, not)🥷Ninja Jump
- Bitwise Operators (>>, <<, &, |)🥷Ninja Jump
- Assignment Operators (=, +=, -=, *=, /=)🥷Ninja Jump
Let's go over each of them one by one.
Arithmetic Operators
Arithmetic operators are used to perform basic arithmetic operations in Python.
- Addition (+): Used for adding two numbers.
10 + 68We can also perform operations on variables and get the same result.
x = 10
y = 68
x + y- Subtraction (-): Used for subtracting two numbers.
10 - 10- Multiplication (*): Used for multiplying two numbers.
10 * 10- Division (/): Used for dividing one number by another.
10 / 2- Modulus (%): Used for finding the remainder after division.
10 % 1011 % 10- Floor division (//): Used for rounding the result of division to the nearest whole number.
19 // 1020 // 10Operator Precedence
When multiple operators are used in a single expression, Python follows a specific order of operations known as operator precedence to determine which operation to perform first.
The order of precedence for arithmetic operators in Python is as follows (from highest to lowest):
- Parentheses ()
- Exponentiation **
- Multiplication *, Division /, Floor Division //, Modulus %
- Addition +, Subtraction -
Operators with the same precedence level are evaluated from left to right.
Example: In the expression 3 + 5 * 2, multiplication has a higher precedence than addition, so the multiplication is performed first, resulting in 3 + 10, which equals 13.
Now that you have started to get a sense of how arithmetic operators work, let's see if you can answer this:
To change the order of operations, you can use parentheses. For instance, in the expression (3 + 5) * 2, the addition inside the parentheses is performed first, resulting in 8 * 2, which equals 16.
Boolean Operators
Boolean operators are used to check conditions between two variables and return either 'True' or 'False'. There are two kinds of boolean operators:
- a. Comparison Operators
- b. Logical Operators
a. Comparison Operators
Comparison operators are used to compare two values in Python. Let us go over some commonly used comparison operators.
- equal to (==)
4 == 24 == 4- not equal to (!=)
4 != 24 != 4- greater than (>)
4 > 22 > 4- less than (<)
2 < 44 < 2- greater than or equal to (>=)
4 >= 22 >= 44 => 4- less than or equal to (<=)
2 <= 44 <= 24 <= 4b. Logical Operators
Logical operators are used to chain multiple boolean operations together. The three most used Logical operators are:
- AND logical operator
- OR logical operator
- NOT logical operator
'AND' logical operator
It is used to find an overlapping relationship between two boolean values. The 'AND' operator results in True if both the evaluated conditions are True, otherwise it results in False.
X | Y | X and Y |
|---|---|---|
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
Let's look at an example to understand this better:
x = 4 < 2 # False
y = 4 > 2 # True
# False and True results in False
print(x and y)This is the same as writing:
# False and True results in False
4 < 2 and 4 > 2'OR' Operator
It is used to find a non-overlapping relationship between two boolean values. The 'OR' operator results in True if any of the conditions are True, otherwise it results in False.
X | Y | X or Y |
|---|---|---|
| False | False | False |
| False | True | True |
| True | False | True |
| True | True | True |
Let's look at an example:
x = 4 < 2 # False
y = 4 > 2 # True
# False or True results in True
print(x or y)'NOT' Operator It is used to find the negation of a boolean value. The 'NOT' operation results in True if the boolean value is False and vice-versa.
X | Not X |
|---|---|
| True | False |
| False | True |
x = 4 < 2 # False
print(not x)Bitwise Operators
Bitwise operators work directly on the binary representation of numbers.
Instead of comparing whole numbers, they compare individual bits (0s and 1s) at each position.
These operators are not commonly used in everyday programming, so it’s normal if they feel unintuitive at first. The goal here is to understand how the calculation happens, not to memorize results.
Let’s start by defining two numbers.
x = 13
bin(x)y = 10
bin(y)So in binary form:
- x = 13 → 1101
- y = 10 → 1010
Each position represents a power of 2.
Decimal | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| x = 13 | 1 | 1 | 0 | 1 |
| y = 10 | 1 | 0 | 1 | 0 |
Bitwise AND (&)
The AND (&) operator returns 1 only if both bits are 1.
Rule:
- 1 & 1 → 1
- Anything else → 0
Bit Position | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| x (1101) | 1 | 1 | 0 | 1 |
| y (1010) | 1 | 0 | 1 | 0 |
| x & y | 1 | 0 | 0 | 0 |
The result is 1000 in binary, which equals 8 in decimal.
x & yBitwise OR (|)
The OR (|) operator returns 1 if either bit is 1.
Rule:
- 0 | 0 → 0
- Anything else → 1
Bit Position | 8 | 4 | 2 | 1 | |
|---|---|---|---|---|---|
| x (1101) | 1 | 1 | 0 | 1 | |
| y (1010) | 1 | 0 | 1 | 0 | |
| x | y | 1 | 1 | 1 | 1 |
The result is 1111, which equals 15 in decimal.
x | yBitwise XOR (^)
The XOR (^) operator returns 1 when the bits are different.
Rule:
- 1 ^ 0 → 1
- 0 ^ 1 → 1
- Same bits → 0
Bit Position | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| x (1101) | 1 | 1 | 0 | 1 |
| y (1010) | 1 | 0 | 1 | 0 |
| x ^ y | 0 | 1 | 1 | 1 |
The result is 0111, which equals 7 in decimal.
x ^ yBitwise NOT (~)
The NOT (~) operator flips every bit:
- 1 becomes 0
- 0 becomes 1
Python uses two’s complement to represent negative numbers, which is why the result may look unfamiliar.
Bit Position | 8 | 4 | 2 | 1 |
|---|---|---|---|---|
| x (1101) | 1 | 1 | 0 | 1 |
| ~x | 0 | 0 | 1 | 0 |
The flipped bits represent -14 in Python’s signed integer system.
~xLeft Shift (<<)
The left shift (<<) operator moves bits to the left and always fills with zeros on the right.
Each left shift multiplies the number by 2.
Operation | Binary | Decimal |
|---|---|---|
| x | 1101 | 13 |
| x << 1 | 11010 | 26 |
# Implies x = 1101 in binary
x = bin(13)
# Bits shifts to left by 1 and fills with 0 to right: 11010
x << 1
# Decimal value of 11010 is the resultRight Shift (>>)
The right shift (>>) operator moves bits to the right and rightmost bit is dropped.
Each right shift divides the number by 2 and discards the remainder.
Operation | Binary | Decimal |
|---|---|---|
| y | 1101 | 13 |
| y >> 1 | 0110 | 6 |
# Implies y = 1101 in binary
y = bin(13)
# Bit shifts to right by 1, the rightmost bit (1) is dropped: 0110
y >> 1
# Decimal value of 0110 is the resultAssignment Operators
Assignment operators are used to perform assignment operation on a variable while performing arithmetic operations.
Example: To compute an operation given as x = x + 1 we can simply write: x += 1
x = 2
x += 1
print(x)x = 2
x -= 1
print(x)x = 2
x *= 10
print(x)x = 2
x /= 10
print(x)x = 2
x %= 10
print(x)x = 2
x //= 2
print(x)