A Beginner's Guide to Python Operators

Understand the different types of operators in Python with clear examples.

Python Operators: Simplifying Operations

In Python, operators are special symbols that perform operations on variables and values. This guide will explore different types of operators you can use in your Python code.


Types of Operators in Python

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

1. Arithmetic Operators

Arithmetic operators allow you to perform mathematical operations on numbers.

OperatorNameExampleDescription
+Addition10 + 5Adds two values
-Subtraction10 - 5Subtracts one value from another
*Multiplication10 * 5Multiplies two values
/Division10 / 5Divides one value by another
%Modulus10 % 3Returns the remainder of division
**Exponentiation2 ** 3Raises a number to the power of another
//Floor Division10 // 3Divides and rounds down to the nearest whole number

Example:

print(10 + 5)  # Output: 15

2. Assignment Operators

Assignment operators are used to assign values to variables.

OperatorExampleEquivalent Expression
=x = 5Assigns 5 to x
+=x += 3x = x + 3
-=x -= 3x = x - 3
*=x *= 3x = x * 3
/=x /= 3x = x / 3
%=x %= 3x = x % 3
**=x **= 3x = x ** 3

Example:

x = 5
x += 3  # x is now 8
print(x)  # Output: 8

3. Comparison Operators

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

| Operator | Name             | Example  | Description                              |
|----------|------------------|----------|------------------------------------------|
| ==       | Equal            | x == y   | Checks if x is equal to y                |
| !=       | Not equal        | x != y   | Checks if x is not equal to y            |
| >        | Greater than     | x > y    | Checks if x is greater than y            |
| <        | Less than        | x < y    | Checks if x is less than y               |
| >=       | Greater or equal | x >= y   | Checks if x is greater than or equal to y|
| <=       | Less or equal    | x <= y   | Checks if x is less than or equal to y   |

Example:

print(10 == 10)  # Output: True

4. Logical Operators

Logical operators combine conditional statements.

OperatorDescriptionExample
andReturns True if both statements are truex < 5 and x < 10
orReturns True if one of the statements is truex < 5 or x < 4
notReverses the resultnot(x < 5 and x < 10)

Example:

print(5 < 10 and 10 < 15)  # Output: True

5. Identity Operators

Identity operators check if two variables point to the same object.

OperatorDescriptionExample
isReturns True if both are the same objectx is y
is notReturns True if both are not the same objectx is not y

Example:

x = [1, 2, 3]
y = x
print(x is y)  # Output: True

6. Membership Operators

Membership operators test for membership in a sequence (like lists, tuples, and strings).

OperatorDescriptionExample
inReturns True if a value is found in a sequencex in y
not inReturns True if a value is not found in a sequencex not in y

Example:

fruits = ["apple", "banana", "cherry"]
print("banana" in fruits)  # Output: True

7. Bitwise Operators

Bitwise operators perform operations on binary numbers.

| Operator | Name       | Description                         | Example  |
|----------|------------|-------------------------------------|----------|
| &        | AND        | Sets each bit to 1 if both bits are 1 | x & y    |
| \|       | OR         | Sets each bit to 1 if one of two bits is 1 | x \| y  |
| ^        | XOR        | Sets each bit to 1 if only one of two bits is 1 | x ^ y  |
| ~        | NOT        | Inverts all the bits                | ~x       |
| <<       | Left shift | Shifts bits to the left             | x << 2   |
| >>       | Right shift| Shifts bits to the right            | x >> 2   |

Example:

print(5 & 3)  # Output: 1 (binary 0101 & 0011 = 0001)

Operator Precedence

Operator precedence determines the order in which operations are evaluated. Higher precedence operators are evaluated first.

Order of Precedence

  1. Parentheses ()
  2. Exponentiation **
  3. Unary operations +x, -x, ~x
  4. Multiplication, Division, Floor Division, Modulus *, /, //, %
  5. Addition, Subtraction +, -
  6. Bitwise Shifts <<, >>
  7. Bitwise AND &
  8. Bitwise XOR ^
  9. Bitwise OR |
  10. Comparison Operators ==, !=, >, <, >=, <=, is, is not, in, not in
  11. Logical NOT not
  12. Logical AND and
  13. Logical OR or

Example:

print((6 + 3) - (6 + 3))  # Output: 0
print(100 + 5 * 3)        # Output: 115 (5 * 3 evaluated first)

If two operators have the same precedence, they are evaluated from left to right.

Example:

print(5 + 4 - 7 + 3)  # Output: 5 (evaluated left to right)