October 15, 2024

Python Operators

Python operators are special symbols that perform operations on variables and values. Python supports various types of operators, each serving different purposes. Here’s a comprehensive overview of Python operators:

1. Arithmetic Operators

Arithmetic operators are used to perform mathematical operations on numerical values.

Operator Name Description Example
+ Addition Adds two operands. x + y
- Subtraction Subtracts the second operand from the first. x - y
* Multiplication Multiplies two operands. x * y
/ Division Divides the first operand by the second. x / y
% Modulus Returns the remainder of the division of the operands. x % y
** Exponentiation Raises the first operand to the power of the second. x ** y
// Floor Division Divides and returns the largest integer less than or equal to the quotient. x // y

2. Comparison (Relational) Operators

Comparison operators compare two values and return a Boolean result (True or False).

Operator Name Description Example
== Equal Returns True if both operands are equal. x == y
!= Not Equal Returns True if operands are not equal. x != y
> Greater Than Returns True if the left operand is greater than the right. x > y
< Less Than Returns True if the left operand is less than the right. x < y
>= Greater Than or Equal Returns True if the left operand is greater than or equal to the right. x >= y
<= Less Than or Equal Returns True if the left operand is less than or equal to the right. x <= y

3. Logical Operators

Logical operators are used to combine conditional statements and return a Boolean value.

Operator Name Description Example
and AND Returns True if both operands are True. x > 5 and x < 10
or OR Returns True if at least one of the operands is True. x > 5 or x < 3
not NOT Reverses the logical state of its operand. not(x > 5 and x < 10)

4. Bitwise Operators

Bitwise operators perform bit-level operations on integers.

Operator Name Description Example
& AND Performs a bitwise AND operation. x & y
` ` OR Performs a bitwise OR operation.
^ XOR Performs a bitwise XOR operation. x ^ y
~ NOT Inverts all the bits of the operand. ~x
<< Left Shift Shifts the bits of the left operand to the left by the number of positions specified by the right operand. x << 2
>> Right Shift Shifts the bits of the left operand to the right by the number of positions specified by the right operand. x >> 2

5. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Name Description Example
= Assignment Assigns the right-hand operand to the left-hand operand. x = 5
+= Add and Assign Adds and assigns the result to the left-hand operand. x += 5
-= Subtract and Assign Subtracts and assigns the result to the left-hand operand. x -= 5
*= Multiply and Assign Multiplies and assigns the result to the left-hand operand. x *= 5
/= Divide and Assign Divides and assigns the result to the left-hand operand. x /= 5
%= Modulus and Assign Takes modulus and assigns the result to the left-hand operand. x %= 5
**= Exponentiate and Assign Raises to the power and assigns the result to the left-hand operand. x **= 3
//= Floor Divide and Assign Performs floor division and assigns the result to the left-hand operand. x //= 3
&= Bitwise AND and Assign Performs bitwise AND and assigns the result to the left-hand operand. x &= 3
` =` Bitwise OR and Assign Performs bitwise OR and assigns the result to the left-hand operand.
^= Bitwise XOR and Assign Performs bitwise XOR and assigns the result to the left-hand operand. x ^= 3
<<= Left Shift and Assign Performs left shift and assigns the result to the left-hand operand. x <<= 3
>>= Right Shift and Assign Performs right shift and assigns the result to the left-hand operand. x >>= 3

6. Identity Operators

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

Operator Name Description Example
is Is Returns True if both operands refer to the same object in memory. x is y
is not Is Not Returns True if both operands do not refer to the same object in memory. x is not y

7. Membership Operators

Membership operators are used to test if a value is found within a sequence (like a string, list, or tuple).

Operator Name Description Example
in In Returns True if the specified value is found in the sequence. "a" in "apple"
not in Not In Returns True if the specified value is not found in the sequence. "b" not in "apple"

8. Miscellaneous Operators

There are a few operators that don’t fall into the above categories but are still essential.

    • Ternary (Conditional) Operator:
      • Used for conditional expressions (short-hand for if-else).
      • Example:

      x = 10
      result = "Positive" if x > 0 else "Negative"

      Operator Precedence

      Operators in Python have a specific order of precedence, which determines the sequence in which operations are performed in an expression. For example, multiplication and division have higher precedence than addition and subtraction.

      Operator Description
      ** Exponentiation
      +, - Unary plus and minus
      *, /, //, % Multiplication, Division, Floor Division, Modulus
      +, - Addition, Subtraction
      <<, >> Bitwise shift operators
      & Bitwise AND
      ^ Bitwise XOR
      ` `
      ==, !=, >, <, >=, <=, is, is not, in, not in Comparison, Identity, Membership
      not Logical NOT
      and Logical AND
      or Logical OR

      Examples of Python Operators in Use

# Arithmetic Operators
a = 10
b = 5
print(a + b) # Addition: 15
print(a - b) # Subtraction: 5
print(a * b) # Multiplication: 50
print(a / b) # Division: 2.0
print(a % b) # Modulus: 0
print(a ** b) # Exponentiation: 100000
# Comparison Operators
print(a == b) # Equal: False
print(a != b) # Not equal: True
print(a > b) # Greater than: True
print(a < b) # Less than: False # Logical Operators print(a > 0 and b > 0) # Logical AND: True
print(a > 0 or b < 0) # Logical OR: True print(not (a > b)) # Logical NOT: False
# Assignment Operators
a += 5 # Add and assign: a = 15
b *= 2 # Multiply and assign: b = 10
# Identity Operators
print(a is b) # Identity: False
print(a is not b) # Identity: True
# Membership Operators
my_list = [1, 2, 3]
print(2 in my_list) # Membership: True
print(4 not in my_list) # Membership: True
# Bitwise Operators
x = 4 # (binary: 0100)
y = 5 # (binary: 0101)
print(x & y) # Bitwise AND: 4 (binary: 0100)
print(x | y) # Bitwise OR: 5 (binary: 0101)
print(x ^ y) # Bitwise XOR: 1 (binary: 0001)