Python Operators

Python Operators

Operators in Python are special symbols or keywords used to perform operations on variables and values. They are essential for manipulating data, performing calculations, and making decisions in your code.


Categories of Python Operators

Python provides several types of operators:

  • Arithmetic operators: Perform mathematical operations.
  • Comparison operators: Compare two values.
  • Assignment operators: Assign values to variables.
  • Logical operators: Combine multiple conditions.
  • Bitwise operators: Work on bits.
  • Membership operators: Test if a value exists in a sequence.
  • Identity operators: Check whether two variables refer to the same object.

Arithmetic Operators

These operators perform basic mathematical operations.

a = 10
b = 3

print(a + b)  # Addition
print(a - b)  # Subtraction
print(a * b)  # Multiplication
print(a / b)  # Division (returns a float)
print(a // b) # Floor division (integer result)
print(a % b)  # Modulo (remainder)
print(a ** b) # Exponentiation

Output:

13
7
30
3.3333333333333335
3
1
1000

Comparison Operators

These operators compare two values and return a boolean (True or False).

x = 5
y = 8

print(x == y)  # Equal to
print(x != y)  # Not equal to
print(x > y)   # Greater than
print(x < y)   # Less than
print(x >= y)  # Greater than or equal to
print(x <= y)  # Less than or equal to

Output:

False
True
False
True
False
True

Assignment Operators

These operators are used to assign values to variables and update them.

x = 5
x += 3  # x = x + 3
print(x)

x *= 2  # x = x * 2
print(x)

x -= 4  # x = x - 4
print(x)

x /= 2  # x = x / 2
print(x)

Output:

8
16
12
6.0

Other assignment operators include //=, %=, **=, and &=, |=, ^=, >>=, <<=.


Logical Operators

Used to combine multiple conditions.

a = True
b = False

print(a and b)  # True if both are True
print(a or b)   # True if at least one is True
print(not a)    # Negates the value

Output:

False
True
False

Logical operators are commonly used in if statements and loops.


Membership Operators

Check whether a value is in a sequence like a list, tuple, or string.

fruits = ["apple", "banana", "cherry"]

print("apple" in fruits)
print("orange" not in fruits)

Output:

True
True

Identity Operators

Test whether two variables point to the same object in memory.

x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)   # True: same object
print(x is z)   # False: same content, different object
print(x == z)   # True: same content

Output:

True
False
True

Bitwise Operators

Operate on binary representations of integers.

a = 5      # 0b0101
b = 3      # 0b0011

print(a & b)  # AND
print(a | b)  # OR
print(a ^ b)  # XOR
print(~a)     # NOT
print(a << 1) # Left shift
print(a >> 1) # Right shift

Output:

1
7
6
-6
10
2

Bitwise operators are mostly used in low-level programming, such as graphics or device control.


Operator Precedence

When multiple operators appear in an expression, Python follows a specific order of precedence:

  • Parentheses ()
  • Exponentiation **
  • Unary plus, minus +x, -x
  • Multiplication, division, modulo *, /, //, %
  • Addition, subtraction +, -
  • Comparison ==, !=, >, <, >=, <=
  • Logical NOT not
  • Logical AND and
  • Logical OR or

You can always use parentheses to make your expressions clearer.


Summary

  • Arithmetic operators perform math.
  • Comparison operators check relationships between values.
  • Assignment operators update variable values.
  • Logical operators help with conditional logic.
  • Membership and identity operators work with sequences and objects.
  • Bitwise operators work at the binary level.
  • Precedence determines the order in which operations are evaluated.

Understanding Python operators is foundational to writing efficient, readable, and correct code. Practice using them in small code snippets to get comfortable with their behavior.

Continue Learning

Python Variables

Popular

### Understanding Variables and Literals in Python Variables and literals are the foundation of any

Python Type Conversion

For You

### Python Type Conversion In Python, **type conversion** is the process of converting the data typ

Python Input-Output

For You

### Displaying Output with print() Use the `print()` function to show text or values on the screen.

Personalized Recommendations

Log in to get more relevant recommendations based on your reading history.