Operators in Python
Operators in Python are used to perform operations on variables and values. They are fundamental to programming and are categorized into several types based on their functionality.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second, returns a float.
- Floor Division (//): Divides the first operand by the second, returns an integer.
- Modulus (%): Returns the remainder of division.
- Exponentiation (**): Raises the first operand to the power of the second.
# Arithmetic Operators
a = 10
b = 3
print(a + b) # Addition: 13
print(a - b) # Subtraction: 7
print(a * b) # Multiplication: 30
print(a / b) # Division: 3.3333...
print(a // b) # Floor Division: 3
print(a % b) # Modulus: 1
print(a ** b) # Exponentiation: 1000
Comparison Operators
Comparison operators are used to compare two values and return a boolean result (True or False).
- Equal to (==): Checks if two values are equal.
- Not equal to (!=): 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 to (>=): Checks if the first value is greater than or equal to the second.
- Less than or equal to (<=): Checks if the first value is less than or equal to the second.
# Comparison Operators
x = 5
y = 10
print(x == y) # Equal to: False
print(x != y) # Not equal to: True
print(x > y) # Greater than: False
print(x < y) # Less than: True
print(x >= y) # Greater than or equal to: False
print(x <= y) # Less than or equal to: True
Logical Operators
Logical operators are used to combine conditional statements and return a boolean result.
- And (and): Returns True if both operands are true.
- Or (or): Returns True if at least one of the operands is true.
- Not (not): Returns True if the operand is false.
# Logical Operators
a = True
b = False
print(a and b) # And: False
print(a or b) # Or: True
print(not a) # Not: False
Assignment Operators
Assignment operators are used to assign values to variables.
- Equal to (=): Assigns a value to a variable.
- Plus equal to (+=): Adds and assigns a value to a variable.
- Minus equal to (-=): Subtracts and assigns a value to a variable.
- Multiply equal to (*=): Multiplies and assigns a value to a variable.
- Divide equal to (/=): Divides and assigns a value to a variable.
- Floor divide equal to (//=): Floor divides and assigns a value to a variable.
- Modulus equal to (%=): Modulus and assigns a value to a variable.
- Exponentiate equal to (**=): Exponentiates and assigns a value to a variable.
# Assignment Operators
num = 10
num += 5 # Plus equal to: num = 15
num -= 3 # Minus equal to: num = 12
num *= 2 # Multiply equal to: num = 24
num /= 4 # Divide equal to: num = 6.0
num //= 2 # Floor divide equal to: num = 3.0
num %= 2 # Modulus equal to: num = 1.0
num **= 3 # Exponentiate equal to: num = 1.0
Bitwise Operators
Bitwise operators are used to perform bit-level operations.
- And (&): Performs a bitwise AND operation.
- Or (|): Performs a bitwise OR operation.
- XOR (^): Performs a bitwise XOR operation.
- Complement (~): Inverts all the bits of the operand.
- Left shift (<<): Shifts bits to the left.
- Right shift (>>): Shifts bits to the right.
# Bitwise Operators
a = 5 # 0101 in binary
b = 3 # 0011 in binary
print(a & b) # Bitwise AND: 1 (0001 in binary)
print(a | b) # Bitwise OR: 7 (0111 in binary)
print(a ^ b) # Bitwise XOR: 6 (0110 in binary)
print(~a) # Bitwise Complement: -6 (invert bits)
print(a << 1) # Left shift: 10 (1010 in binary)
print(a >> 1) # Right shift: 2 (0010 in binary)
Membership Operators
Membership operators are used to test if a value or variable is found in a sequence (e.g., list, tuple, string).
- In: Returns True if a value is found in the sequence.
- Not in: Returns True if a value is not found in the sequence.
# Membership Operators
sequence = [1, 2, 3, 4, 5]
print(3 in sequence) # True
print(10 not in sequence) # True
Identity Operators
Identity operators are used to compare the memory locations of two objects.
- Is: Returns True if both variables point to the same object.
- Is not: Returns True if both variables point to different objects.
# Identity Operators
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (same object)
print(a is not c) # True (different objects)