Python Operators: Types and Examples
What Are Python Operators?
An operator is a symbol that tells Python to perform a specific mathematical, relational, or logical operation. You use operators to manipulate variables and values to get a result. You can add numbers, compare data, or combine conditional statements with them.
For example, in the expression 10 + 5, the + is the operator and 10 and 5 are the operands.
Want to master Python? This lesson is part of our Free Python course covering variables, operators, logic, and more.
Explore this free Python course →Arithmetic Operators
Arithmetic operators perform common mathematical calculations. You use them with numeric data types like integers and floats.
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 10 + 5 |
15 |
- |
Subtraction | 10 - 5 |
5 |
* |
Multiplication | 10 * 5 |
50 |
/ |
Division | 10 / 3 |
3.33 |
% |
Modulus | 10 % 3 |
1 |
** |
Exponentiation | 10 ** 2 |
100 |
// |
Floor Division | 10 // 3 |
3 |
Floor division (//) divides and rounds the result down to the nearest whole number. The modulus operator (%) returns the remainder of a division.
Assignment Operators
Assignment operators assign values to variables. The most common is the simple assignment operator (=), but Python also gives you compound operators that combine an arithmetic operation with an assignment.
For example, x += 5 is a shorthand way to write x = x + 5.
| Operator | Example | Equivalent To |
|---|---|---|
= |
x = 5 |
x = 5 |
+= |
x += 5 |
x = x + 5 |
-= |
x -= 5 |
x = x - 5 |
*= |
x *= 5 |
x = x * 5 |
Comparison Operators
Comparison operators compare two values and return a Boolean result. The result is either True or False, which you need for conditional logic in if statements and while loops.
| Operator | Name | Example | Result |
|---|---|---|---|
== |
Equal to | 10 == 5 |
False |
!= |
Not equal to | 10 != 5 |
True |
> |
Greater than | 10 > 5 |
True |
< |
Less than | 10 < 5 |
False |
Logical Operators
Logical operators combine conditional statements and return True or False. You use them to create complex decision-making logic.
and: ReturnsTrueif both statements are trueor: ReturnsTrueif at least one of the statements is truenot: Reverses the Boolean value of a statement
Example: Logical Operators in Action
x = 10
y = 5
print(x > 5 and y < 10)
print(x > 15 or y < 10)
print(not(x > 5 and y < 10))
Your output will appear here...
You're making great progress! Ready to apply these operators in real-world scenarios?
Take Python ExercisesIdentity Operators
Identity operators compare the memory locations of two objects. They check if two operands are the exact same object, not just if they have equal values.
is: ReturnsTrueif both variables point to the same objectis not: ReturnsTrueif the variables point to different objects
is vs == (Value vs. Identity)
The == operator compares the values of two objects, while the is operator checks if they're the same object in memory. This is a key difference between checking for equality and checking for identity.
list_a = [1, 2, 3]
list_b = [1, 2, 3]
list_c = list_a
print(list_a == list_b)
print(list_a is list_b)
print(list_a is list_c)
Your output will appear here...
list_a and list_b contain the same elements, they're two separate list objects stored in different memory locations. However, list_c is another name for the same object that list_a refers to.Membership Operators
Membership operators test if a sequence is present in an object, such as a string, list, or tuple. They return True or False.
in: ReturnsTrueif Python finds a value in the sequencenot in: ReturnsTrueif Python doesn't find a value in the sequence
my_list = [10, 20, 30, 40]
my_string = "hello world"
print(20 in my_list)
print("hello" in my_string)
print(50 not in my_list)
Your output will appear here...
Bitwise Operators
Bitwise operators perform operations on integers at the binary level. You usually use them for lower-level programming tasks, such as working with device drivers or network packets. They're less common in general application development.
| Operator | Name | Description |
|---|---|---|
& |
AND | Sets each bit to 1 if both bits are 1 |
| |
OR | Sets each bit to 1 if one of two bits is 1 |
^ |
XOR | Sets each bit to 1 if only one of two bits is 1 |
~ |
NOT | Inverts all the bits |
<< |
Zero fill left shift | Shifts bits to the left, pushing zeros in from the right |
>> |
Signed right shift | Shifts bits to the right, pushing copies of the leftmost bit in from the left |
Lesson Completed
You have learned about all Types of Python Operators. Next, learn Each Operators in Detail.
Full Python Course
Master Python with 11+ hours of content, 50+ exercises, and real-world projects.
Enroll Now