Operators

Operators in Python are special symbols or keywords used to perform operations on variables and values.

Arithmetic Operators

Arithmetic operators perform mathematical operations on numeric values.

Pythona = 10
b = 5

# Addition
result_addition = a + b  # Result: 15

# Subtraction
result_subtraction = a - b  # Result: 5

# Multiplication
result_multiplication = a * b  # Result: 50

# Division
result_division = a / b  # Result: 2.0

# Modulus
result_modulus = a % b  # Result: 0

# Exponentiation
result_exponentiation = a ** b  # Result: 100000

Assignment Operators

Assignment operators are used to assign values to variables.

Pythonx = 5

# Simple Assignment
y = x  # y gets the value of x

# Add and Assign
x += 3  # Equivalent to: x = x + 3

# Subtract and Assign
x -= 2  # Equivalent to: x = x - 2

# Multiply and Assign
x *= 4  # Equivalent to: x = x * 4

# Divide and Assign
x /= 2  # Equivalent to: x = x / 2

Comparison Operators

Comparison operators compare two values and return a boolean result.

Pythonx = 5
y = 10

# Equal
result_equal = x == y  # Result: False

# Not Equal
result_not_equal = x != y  # Result: True

# Greater Than
result_greater_than = x > y  # Result: False

# Less Than
result_less_than = x < y  # Result: True

# Greater Than or Equal To
result_greater_equal = x >= y  # Result: False

# Less Than or Equal To
result_less_equal = x <= y  # Result: True

Logical Operators

Logical operators perform logical operations on boolean values.

Pythona = True
b = False

# Logical AND
result_and = a and b  # Result: False

# Logical OR
result_or = a or b  # Result: True

# Logical NOT
result_not_a = not a  # Result: False
result_not_b = not b  # Result: True

Identity Operators

Identity operators compare the memory locations of two objects.

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

# Identity (is)
result_identity = x is y  # Result: False

# Not Identity (is not)
result_not_identity = x is not y  # Result: True

# Identity (is) with same object
result_identity_same_object = x is z  # Result: True

Membership Operators

Membership operators check for the existence of a value in a sequence.

Pythonmy_list = [1, 2, 3, 4, 5]

# Membership (in)
result_membership = 3 in my_list  # Result: True

# Not Membership (not in)
result_not_membership = 6 not in my_list  # Result: True

Operator Precedence

Operator precedence determines the order in which operations are performed in an expression. Some operators have higher precedence than others, and they are evaluated first.

Here is a list of operators in descending order of precedence:

  • ** (Exponentiation)
  • +x, -x, ~x (Unary plus, Unary minus, Bitwise NOT)
  • *, /, //, % (Multiplication, Division, Floor division, Modulus)
  • +, - (Addition, Subtraction)
  • <<, >> (Bitwise shift operators)
  • & (Bitwise AND)
  • ^ (Bitwise XOR)
  • | (Bitwise OR)
  • in, not in, is, is not, <, <=, >, >=, !=, == (Comparisons, Membership tests, Identity tests)
  • not x (Boolean NOT)
  • and (Boolean AND)
  • or (Boolean OR)

It's important to note that parentheses can be used to override the default precedence and force a specific order of evaluation.

Example:

Pythonresult = 5 + 3 * 2  # Result: 11
# The multiplication is performed first due to higher precedence

In the above example, the multiplication operation has higher precedence than addition, so it is evaluated first, resulting in 11.