Operators are special symbols that perform operations on values and variables. Think of them as verbs that act on nouns (values and variables).
Categories of Operators:
Python has several categories of operators:
Arithmetic Operators:
These perform mathematical operations.+ (Addition): Adds two values.- (Subtraction): Subtracts the second value from the first.* (Multiplication): Multiplies two values./ (Division): Divides the first value by the second (returns a float).// (Floor Division): Divides and returns the integer part of the quotient.% (Modulo): Returns the remainder of the division.** (Exponentiation): Raises the first value to the power of the second.
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x // y) # Output: 3
print(x % y) # Output: 1
print(x ** y) # Output: 1000
Comparison Operators:
These compare two values and return a boolean (True or False).== (Equal): Checks if two values are equal.!= (Not Equal): Checks if two values are not equal.> (Greater Than): Checks if the first value is greater than the second.< (Less Than): Checks if the first value is less than the second.>= (Greater Than or Equal): Checks if the first value is greater than or equal to the second.<= (Less Than or Equal): Checks if the first value is less than or equal to the second.
a = 5
b = 10
print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: False
print(a < b) # Output: True
print(a >= 5) # Output: True
print(b <= 10) # Output: True
Logical Operators:
These combine or modify boolean values.and: Returns True if both operands are True.or: Returns True if at least one operand is True.not: Returns the opposite of the operand’s boolean value.
p = True
q = False
print(p and q) # Output: False
print(p or q) # Output: True
print(not p) # Output: False
Assignment Operators:
These assign values to variables.= (Assignment): Assigns the value on the right to the variable on the left.+= (Add and Assign): Adds the right operand to the left operand and assigns the result to the left operand.-= (Subtract and Assign): Subtracts the right operand from the left operand and assigns the result to the left operand.*= (Multiply and Assign): Multiplies the left operand by the right operand and assigns the result to the left operand./=, //=, %=, **= (Similar for other arithmetic operators).
num = 5
num += 3 # Equivalent to num = num + 3
print(num) # Output: 8
Membership Operators:
These test if a value is a member of a sequence (like a string, list, or tuple).in: Returns True if the value is found in the sequence.not in: Returns True if the value is not found in the sequence.
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
print(6 not in my_list) # Output: True
Identity Operators:
These compare the memory locations of two objects.is: Returns True if both variables refer to the same object.is not: Returns True if both variables do not refer to the same object.
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True
print(x is z) # Output: False (different memory locations)
Operator Precedence:
Just like in mathematics, operators have a precedence. For example, multiplication and division are performed before addition and subtraction. Use parentheses () to control the order of evaluation.

Leave a comment