In Python, an operator is a special symbol that performs an operation on one or more values, called operands. For example, in the expression 10 + 5, the + is the operator, and 10 and 5 are the operands. Operators are the fundamental building blocks for performing calculations, making comparisons, and controlling the logic of your programs.
1. Arithmetic Operators
These are used to perform standard mathematical operations on numbers.
- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division): Always results in a float.
- // (Floor Division): Divides and rounds down to the nearest whole number.
- % (Modulus): Returns the remainder of a division.
- ** (Exponentiation): Raises a number to the power of another.
2. Comparison (Relational) Operators
These operators compare two values and return a Boolean (True or False) result. They are the foundation of conditional statements.
- == (Equal to)
- != (Not equal to)
- > (Greater than)
- < (Less than)
- >= (Greater than or equal to)
- <= (Less than or equal to)
3. Logical Operators
These operators are used to combine conditional statements and return a Boolean result.
- and: Returns True if both operands are true.
- or: Returns True if at least one of the operands is true.
- not: Inverts the truth value; returns False if the operand is true, and True if it is false.
4. Assignment Operators
These operators are used to assign values to variables.
- = (Simple Assignment): Assigns the value on the right to the variable on the left.
- +=, -=, *=, /=, etc. (Compound Assignment): These combine an arithmetic operation with an assignment. For example, x += 5 is a shorthand for x = x + 5.
5. Identity Operators
These operators compare the memory locations of two objects, not just their values.
- is: Returns True if both variables point to the exact same object in memory.
- is not: Returns True if both variables point to different objects in memory.
6. Membership Operators
These operators test if a sequence (like a string, list, or tuple) contains a specific value.
- in: Returns True if a value is found in the sequence.
- not in: Returns True if a value is not found in the sequence.
7. Bitwise Operators
These operators are used to perform operations on integers at the binary level. They are used in more advanced, low-level programming.
- & (AND)
- | (OR)
- ^ (XOR)
- ~ (NOT)
- << (Left Shift)
- >> (Right Shift)
# --- 1. Arithmetic Operators ---
# Used for mathematical calculations.
a = 10
b = 3
print("--- 1. Arithmetic Operators ---")
print(f"{a} + {b} = {a + b}")
print(f"{a} - {b} = {a - b}")
print(f"{a} * {b} = {a * b}")
print(f"{a} / {b} = {a / b}") # Standard division
print(f"{a} // {b} = {a // b}") # Floor division
print(f"{a} % {b} = {a % b}") # Modulus (remainder)
print(f"{a} ** {b} = {a ** b}") # Exponentiation
# --- 2. Comparison Operators ---
# Used to compare two values, resulting in True or False.
print("\n--- 2. Comparison Operators ---")
print(f"{a} == {b}: {a == b}")
print(f"{a} != {b}: {a != b}")
print(f"{a} > {b}: {a > b}")
print(f"{a} <= {b}: {a <= b}")
# --- 3. Logical Operators ---
# Used to combine boolean expressions.
x = True
y = False
print("\n--- 3. Logical Operators ---")
print(f"{x} and {y}: {x and y}")
print(f"{x} or {y}: {x or y}")
print(f"not {x}: {not x}")
# --- 4. Assignment Operators ---
# Used to assign values to variables.
c = 15
print("\n--- 4. Assignment Operators ---")
print(f"Initial value of c: {c}")
c += 5 # Equivalent to c = c + 5
print(f"After c += 5: {c}")
c *= 2 # Equivalent to c = c * 2
print(f"After c *= 2: {c}")
# --- 5. Identity Operators ---
# Used to compare the memory location of two objects.
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print("\n--- 5. Identity Operators ---")
print(f"list1 == list2: {list1 == list2}") # True, because their values are the same
print(f"list1 is list2: {list1 is list2}") # False, because they are different objects in memory
print(f"list1 is list3: {list1 is list3}") # True, because list3 refers to the same object as list1
# --- 6. Membership Operators ---
# Used to check if a value is present in a sequence.
my_list = ["apple", "banana", "cherry"]
print("\n--- 6. Membership Operators ---")
print(f"'banana' in my_list: {'banana' in my_list}")
print(f"'mango' not in my_list: {'mango' not in my_list}")
# --- 7. Bitwise Operators ---
# Used to perform operations on integers at the binary level.
# 5 in binary is 0101
# 3 in binary is 0011
num1 = 5
num2 = 3
print("\n--- 7. Bitwise Operators ---")
print(f"{num1} & {num2} (AND): {num1 & num2}") # 0001 -> 1
print(f"{num1} | {num2} (OR): {num1 | num2}") # 0111 -> 7
print(f"{num1} ^ {num2} (XOR): {num1 ^ num2}") # 0110 -> 6
print(f"~{num1} (NOT): {~num1}") # Inverts all bits
print(f"{num1} << 1 (Left Shift): {num1 << 1}") # 1010 -> 10
print(f"{num1} >> 1 (Right Shift): {num1 >> 1}")# 0010 -> 2