Last updated on December 28th, 2025 at 04:30 pm
Operators in Python are special symbols or keywords that are used to perform operations on one or more variables or values. These variables or values are commonly known as operands.
Operators are necessary for performing calculations, manipulating data, controlling program flow, and making comparisons. Keep reading to learn:
- Types of operators in Python:
- Assignment;
- Arithmetic;
- Identity;
- Bitwise;
- Membership; and
- Logical operators.
In the following sections, we examine the various types of operators in Python and their meanings.
If you are new to Python, then you should also read Lesson 1: Mastering Python Programming and Lesson 2: Mastering Python Syntax.
Assignment operators in Python
Assignment operators are used to assign values to variables. Here are some examples of assignment operators and their meaning:
- = (equal to)
- != (not equal to)
- > (greater than)
- < (less than)
- >= (greater than or equal to)
- <= (less than or equal to)
Arithmetic operators in Python
Arithmetic operators are used for mathematical calculations. Here are some examples:
- / (division).
- * (multiplication).
- + (addition).
- – (subtraction).
- ** (exponent: this raises the left operand to the power of the right operand). So 4**2 is the same as 4².
- // (floor division performs division and returns the integer part of the answer). As an example, 15//2 will return 7 as the answer.
- % (modulus: returns the remainder of the division). In this instance, 7%2 will return 1.
Identity operators in Python
Identity operators are used to check if two variables refer to the same object in memory. Here are some examples:
- is ( this checks to see if two variables are the same). This checks to see if two operands are the same (a = b). Note that it does not check if they contain the same values.
- is not (does the opposite of is. It checks to see if two variables are not the same).
Bitwise operators in Pascal
Bitwise operators perform operations on the binary equivalent of integers. In the next few sections, we explore examples of bitwise operators and explain what each does with examples:
& (Bitwise AND)
Let’s use this expression as an example to calculate 8&7. When this operator is used, the numbers eight and seven are first converted to binary:
- 8 = 00001000 while the number 7 is 00000111.
- So
8&7is the same as1000 & 111.The rightmost digit of each number is checked to see if both are 1; if they are, the answer is 1. If either is 0, the answer is 0.- In this instance, the right-most digits are 1 and 0, so the result is zero.
- Next, the second digit to the left of this one is checked using the same reasoning. Again, this is a binary representation of 1 and 0.
- We keep going like this until the calculation is complete. In this instance, the result is 00000000.
| (Bitwise OR)
When the Bitwise OR is used in Python, the decimal numbers are first converted to binary, then each place value is compared, and if either one is equal to 1, the answer is returned as 1.
For 5|3, when converted to binary, we get 00000101 | 00000011. The answer to this is 111, which is then shown in decimal form as 7.
>> (Right shift)
Right shift inserts copies of the left-most bit on the left side, allowing the right-most ones to fall off. For 8 >> 1, we first need to convert 8 to binary. This is 00001000. When we apply the operation 8 >> 1, we get 00000100.
<< (Left shift)
A left shift inserts 0s to the right, causing the leftmost bits to be discarded. Let’s consider the same number, 8. In binary, this is 00001000. When we perform the operation 8 << 2, we obtain 00100000.
~ (Bitwise NOT)
This operator inverts all of the bits. So, for ~5, we first need to convert the number to binary, which is ~00000101. When this number is inverted, it becomes 00000010. We then need to convert this number to its decimal value. When 00000010 is converted to decimal, the value is 2.
^ (Bitwise XOR)
In this instance, let’s look at the example 5^3. The two numbers are first converted to binary, and they are compared. If both numbers that hold the same place value are one, the answer is 0; however, if only one number is 1, the answer is also 1. If both numbers are 0, the answer is 0. So let’s look at the operation 5^3:
- For 5 and 3, where the binary equivalents are 00000101 and 00000011, the first numbers are 1 and 1, so the answer is 0,
- For the second number, the numbers are 0 and 1, so the answer is 1. Therefore, combining the answer from above with this new one, the answer is now 10.
- Now, let’s move to the third digit. These are 1 and 0, so the answer is again 1. When this is combined with the answer above, the result is 110.
- Moving on, we are now looking at the fourth value, which is 0 and 0, evaluating to 0. Adding this to the answer above, we now have 0110.
- Since all the other digits are 0 and 0, we can safely say that the other values will evaluate to 0. As such, the final answer is 00000110.
- When converted to decimal, the number is 6.
Membership operators in Python
Membership operators test whether a value exists in a sequence (like a string, list, or tuple).
- in (checks to see if a value is present)
- not in ( check to see if a value is not present
Logical operators in Python
Logical operators combine conditional statements and return a Boolean result. Here are some examples:
- and (Logical AND)
- or (Logical OR)
- not (Logical Not)
Before You Go
Operators in Python play a crucial role in performing mathematical and logical operations in Python. You need to understand when to use each and how.
If you have any related questions, please leave a comment in the section below. If you do not have a compiler to run your code, this online compiler works perfectly, and you don’t need to download it.
Additionally, be sure to check out lesson 4, where we explore inputs and conditionals, what they are, and how to use them.
Skip to content