The Ultimate Guide to Python Operators with Examples

Python is one of the most popular programming languages for beginners and professionals alike, as it is simple, easy to learn, and versatile. One of the fundamental concepts in Python is operators.

Operators are symbols or keywords that perform operations on variables and values. These operations can be arithmetic, logical, comparison-based, or something else entirely.

If you are new to Python, understanding the different types of operators is essential. This guide will explain the types of operators in Python with examples so you can follow along easily.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication, division, and more.

Here are the arithmetic operators in Python:

Operator Symbol Example Description
Addition + a + b Adds two numbers
Subtraction - a - b Subtracts the second number from the first
Multiplication * a * b Multiplies two numbers
Division / a / b Divides the first number by the second (returns float)
Floor Division // a // b Divides and returns the integer part of the result
Modulus % a % b Returns the remainder of a division
Exponentiation ** a ** b Raises the first number to the power of the second

Example:

# Arithmetic Operators Example
x = 10
y = 3

print("Addition: ", x + y)        # 13
print("Subtraction: ", x - y)     # 7
print("Multiplication: ", x * y)  # 30
print("Division: ", x / y)        # 3.3333
print("Floor Division: ", x // y) # 3
print("Modulus: ", x % y)         # 1
print("Exponentiation: ", x ** y) # 1000

2. Comparison Operators

Comparison operators are used to compare two values, these operators return either True or False depending on the comparison result.

Operator Symbol Example Description
Equal to == a == b Checks if two values are equal
Not Equal to != a != b Checks if two values are not equal
Greater than > a > b Checks if the first value is greater
Less than < a < b Checks if the first value is smaller
Greater than or equal to >= a >= b Checks if the first value is greater or equal
Less than or equal to <= a <= b Checks if the first value is smaller or equal

Example:

# Comparison Operators Example
x = 10
y = 5

print("Equal to: ", x == y)        # False
print("Not Equal to: ", x != y)    # True
print("Greater than: ", x > y)     # True
print("Less than: ", x < y)        # False
print("Greater or Equal: ", x >= y) # True
print("Less or Equal: ", x <= y)    # False

3. Logical Operators

Logical operators are used to combine conditional statements, these operators return either True or False.

Operator Keyword Example Description
AND and a > 5 and a < 10 Returns True if both conditions are True
OR or a > 5 or a < 3 Returns True if at least one condition is True
NOT not not(a > 5) Reverses the result (True to False or False to True)

Example:

# Logical Operators Example
x = 7
y = 10

print("AND: ", x > 5 and y < 15)   # True
print("OR: ", x > 10 or y < 15)    # True
print("NOT: ", not(x > 5))         # False

4. Assignment Operators

Assignment operators are used to assign values to variables. You can also use them to perform operations and assign the result in one step.

Operator Symbol Example Equivalent to
Assign = a = 5 Assigns value 5 to a
Add and assign += a += 3 a = a + 3
Subtract and assign -= a -= 2 a = a - 2
Multiply and assign *= a *= 4 a = a * 4
Divide and assign /= a /= 2 a = a / 2
Modulus and assign %= a %= 3 a = a % 3
Exponentiate and assign **= a **= 2 a = a ** 2

Example:

# Assignment Operators Example
x = 10
x += 5    # x = x + 5
print("After +=: ", x)  # 15

x -= 3    # x = x - 3
print("After -=: ", x)  # 12

x *= 2    # x = x * 2
print("After *=: ", x)  # 24

x /= 4    # x = x / 4
print("After /=: ", x)  # 6.0

5. Bitwise Operators

Bitwise operators are used to perform operations on binary numbers (bits). These are advanced operators but can be useful in certain situations.

Operator Symbol Example Description
AND & a & b Performs bitwise AND operation
OR ` a | b Performs bitwise OR operation
XOR ^ a ^ b Performs bitwise XOR operation
NOT ~ ~a Performs bitwise NOT operation
Left Shift << a << 2 Shifts bits to the left
Right Shift >> a >> 2 Shifts bits to the right

Example:

# Bitwise Operators Example
a = 6   # Binary: 110
b = 3   # Binary: 011

print("AND: ", a & b)       # 2 (Binary: 010)
print("OR: ", a | b)        # 7 (Binary: 111)
print("XOR: ", a ^ b)       # 5 (Binary: 101)
print("NOT: ", ~a)          # -7 (Binary: ...11111001)
print("Left Shift: ", a << 1) # 12 (Binary: 1100)
print("Right Shift: ", a >> 1) # 3 (Binary: 011)

6. Membership Operators

Membership operators are used to check if a value exists in a sequence, like a list, tuple, or string.

Operator Keyword Example Description
IN in x in y Returns True if x exists in y
NOT IN not in x not in y Returns True if x does not exist in y

Example:

# Membership Operators Example
my_list = [1, 2, 3, 4, 5]
print(3 in my_list)        # True
print(6 not in my_list)    # True

7. Identity Operators

Identity operators are used to compare the memory location of two objects.

Operator Keyword Example Description
IS is x is y Returns True if both objects are the same
IS NOT is not x is not y Returns True if objects are different

Example:

# Identity Operators Example
a = [1, 2, 3]
b = a
c = [1, 2, 3]

print(a is b)       # True (Same object)
print(a is c)       # False (Different objects)
print(a is not c)   # True
Conclusion

Operators in Python are essential tools for performing different types of operations. Whether you are working with numbers, conditions, or objects, Python provides a variety of operators to make your code efficient and clear.

By understanding arithmetic, comparison, logical, assignment, bitwise, membership, and identity operators, you are now ready to write more powerful Python programs. Practice these operators with examples, and you will become confident in no time!

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.