Python Operators

Operators in Python are symbols that perform operations on variables and values. They allow you to manipulate data and control the flow of your program. Python offers several types of operators:

1. Arithmetic Operators
2. Comparison Operators
3. Logical Operators
4. Assignment Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators

1. Arithmetic Operators

These operators perform mathematical operations such as addition, subtraction, multiplication, and division.
a = 10
b = 3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponentiation:", a ** b)

Output:
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.3333333333333335
Floor Division: 3
Modulus: 1
Exponentiation: 1000


2. Comparison Operators

Comparison operators compare two values and return a Boolean (`True` or `False`).
x = 5
y = 10

print("Equal:", x == y)
print("Not Equal:", x != y)
print("Greater Than:", x > y)
print("Less Than:", x < y)
print("Greater Than or Equal:", x >= y)
print("Less Than or Equal:", x <= y)

Output:
Equal: False
Not Equal: True
Greater Than: False
Less Than: True
Greater Than or Equal: False
Less Than or Equal: True


3. Logical Operators

Logical operators are used to combine conditional statements and return a Boolean result.
a = True
b = False

print("AND:", a and b)
print("OR:", a or b)
print("NOT a:", not a)

Output:
AND: False
OR: True
NOT a: False


4. Assignment Operators

Assignment operators assign values to variables. You can combine assignment with other operations (e.g., add and assign).
x = 10
print("Initial x:", x)

x += 5   # Equivalent to x = x + 5
print("After x += 5:", x)

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

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

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

x %= 3   # Equivalent to x = x % 3
print("After x %= 3:", x)

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

Output:
Initial x: 10
After x += 5: 15
After x -= 3: 12
After x *= 2: 24
After x /= 4: 6.0
After x %= 3: 0.0
After x **= 2: 0.0


5. Bitwise Operators

Bitwise operators perform operations on binary numbers. These operators treat numbers as a set of bits (1s and 0s).
a = 5   # 101 in binary
b = 3   # 011 in binary

print("Bitwise AND:", a & b)   # 101 & 011 = 001 (1 in decimal)
print("Bitwise OR:", a | b)    # 101 | 011 = 111 (7 in decimal)
print("Bitwise XOR:", a ^ b)   # 101 ^ 011 = 110 (6 in decimal)
print("Bitwise NOT (~a):", ~a) # Inverts all bits (two's complement)
print("Left Shift:", a << 1)   # 101 << 1 = 1010 (10 in decimal)
print("Right Shift:", a >> 1)  # 101 >> 1 = 10 (2 in decimal)

Output:
Bitwise AND: 1
Bitwise OR: 7
Bitwise XOR: 6
Bitwise NOT (~a): -6
Left Shift: 10
Right Shift: 2


6. Membership Operators

Membership operators test if a value exists within a sequence (e.g., strings, lists, tuples).
my_list = [1, 2, 3, 4, 5]

print("Is 3 in list:", 3 in my_list)
print("Is 6 in list:", 6 in my_list)
print("Is 6 not in list:", 6 not in my_list)

Output:
Is 3 in list: True
Is 6 in list: False
Is 6 not in list: True


7. Identity Operators

Identity operators check if two variables refer to the same object (memory location).
x = [1, 2, 3]
y = x
z = [1, 2, 3]

print("Is x the same as y:", x is y)       # True, because y refers to the same object as x
print("Is x the same as z:", x is z)       # False, because z is a different object
print("Is x not the same as z:", x is not z)

Output:
Is x the same as y: True
Is x the same as z: False
Is x not the same as z: True


Summary

Python offers a variety of operators to perform operations on values and variables. By mastering these operators, you can efficiently manage data and control program flow in Python. Practice each type of operator with different data types to become proficient.

Previous: Python DataTypes | Next: Python Numbers

<
>