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: Returns True if both statements are true
  • or: Returns True if at least one of the statements is true
  • not: Reverses the Boolean value of a statement

Example: Logical Operators in Action

x = 10 y = 5 # Example for 'and' print(x > 5 and y < 10) # Example for 'or' print(x > 15 or y < 10) # Example for 'not' print(not(x > 5 and y < 10))
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 Exercises

Identity 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: Returns True if both variables point to the same object
  • is not: Returns True if 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 # Compares the values, which are the same print(list_a == list_b) # Compares the memory location, which is different print(list_a is list_b) # list_c points to the same object as list_a print(list_a is list_c)
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...
Analysis: While 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: Returns True if Python finds a value in the sequence
  • not in: Returns True if Python doesn't find a value in the sequence
my_list = [10, 20, 30, 40] my_string = "hello world" # Example for 'in' print(20 in my_list) print("hello" in my_string) # Example for 'not in' print(50 not in my_list)
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
📝

Next Lesson

Continue with the next lesson on Python Control Flow.

Next Lesson ->

Scroll to Top