What are the 7 operators in Python?

Python has several operators that can be categorized into different groups based on their functionality. Here are the seven main categories of operators in Python:

  • Arithmetic Operators: These operators are used for basic arithmetic operations.
    • Addition: +
    • Subtraction: -
    • Multiplication: *
    • Division: /
    • Modulus: %
    • Exponentiation: ** (e.g., 2**3 equals 2 raised to the power of 3, which is 8)
    • Floor Division: // (e.g., 5 // 2 equals 2 as it returns the quotient of the division rounded down)
a = 10
b = 3

addition_result = a + b
subtraction_result = a - b
multiplication_result = a * b
division_result = a / b
modulus_result = a % b
exponentiation_result = a ** b
floor_division_result = a // b

print(addition_result)        # Output: 13
print(subtraction_result)     # Output: 7
print(multiplication_result)  # Output: 30
print(division_result)        # Output: 3.3333333333333335
print(modulus_result)         # Output: 1
print(exponentiation_result)  # Output: 1000
print(floor_division_result)  # Output: 3
Code language: Python (python)
  • Assignment Operators: These operators are used to assign values to variables.
    • Assignment: =
    • Add and Assign: +=
    • Subtract and Assign: -=
    • Multiply and Assign: *=
    • Divide and Assign: /=
    • Modulus and Assign: %=
    • Exponentiation and Assign: **=
    • Floor Division and Assign: //=
x = 5
x += 3  # Equivalent to x = x + 3
print(x)  # Output: 8

y = 10
y *= 2  # Equivalent to y = y * 2
print(y)  # Output: 20
Code language: Python (python)
  • Comparison Operators: These operators are used to compare two values.
    • Equal to: ==
    • Not equal to: !=
    • Greater than: >
    • Less than: <
    • Greater than or equal to: >=
    • Less than or equal to: <=
p = 5
q = 10

print(p == q)   # Output: False
print(p != q)   # Output: True
print(p > q)    # Output: False
print(p < q)    # Output: True
print(p >= q)   # Output: False
print(p <= q)   # Output: True
Code language: Python (python)
  • Logical Operators: These operators are used to combine or modify logical statements.
    • Logical AND: and
    • Logical OR: or
    • Logical NOT: not
raining = True
temperature = 25

if raining and temperature < 30:
    print("It's raining and not too hot. Bring an umbrella.")
else:
    print("The weather is fine.")
    
if not raining or temperature > 30:
    print("It's either not raining or hot. Enjoy the day.")
else:
    print("The weather might not be ideal.")
Code language: Python (python)
  • Bitwise Operators: These operators perform operations on individual bits of integer values.
    • Bitwise AND: &
    • Bitwise OR: |
    • Bitwise XOR: ^
    • Bitwise NOT: ~
    • Left Shift: <<
    • Right Shift: >>
x = 5  # Binary: 0101
y = 3  # Binary: 0011

bitwise_and_result = x & y  # Binary: 0001 -> Decimal: 1
bitwise_or_result = x | y   # Binary: 0111 -> Decimal: 7
bitwise_xor_result = x ^ y  # Binary: 0110 -> Decimal: 6
bitwise_not_result = ~x     # Binary: 1010 (assuming 4 bits) -> Decimal: -6
left_shift_result = x << 1  # Binary: 1010 -> Decimal: 10
right_shift_result = x >> 1 # Binary: 0010 -> Decimal: 2

print(bitwise_and_result)   # Output: 1
print(bitwise_or_result)    # Output: 7
print(bitwise_xor_result)   # Output: 6
print(bitwise_not_result)   # Output: -6
print(left_shift_result)    # Output: 10
print(right_shift_result)   # Output: 2
Code language: Python (python)
  • Membership Operators: These operators check for membership within a sequence or collection.
    • in: Returns True if a value is present in a sequence.
    • not in: Returns True if a value is not present in a sequence.
list_example = [1, 2, 3, 4, 5]

print(3 in list_example)    # Output: True
print(6 not in list_example)  # Output: True
Code language: Python (python)
  • Identity Operators: These operators check if two variables refer to the same object in memory.
    • is: Returns True if both variables point to the same object.
    • is not: Returns True if both variables do not point to the same object.
x = [1, 2, 3]
y = x
z = [1, 2, 3]

print(x is y)  # Output: True (both x and y refer to the same object)
print(x is z)  # Output: False (x and z are different objects with the same values)
print(x is not z)  # Output: True (x and z are different objects)
Code language: Python (python)

These are the main categories of operators in Python, and understanding how to use them is fundamental for writing efficient and expressive code.

What is %= in Python?

the %= is a shorthand assignment operator used for the modulus operation. It combines the modulus operator % with the assignment operator =. The modulus operation calculates the remainder when one number is divided by another.

The syntax for the %= operator is as follows:

x %= yCode language: Python (python)

This means take the value of x, perform the modulus operation on x and y, and then assign the result back to x.

For example, consider the following code:

x = 10
y = 3

x %= y  # Equivalent to x = x % y

print(x)  # Output: 1Code language: Python (python)

In this example, x starts with the value of 10, and after applying the %= operator with y (which is 3), the value of x becomes 1, which is the remainder when 10 is divided by 3.

Read More;

    by
  • Yaryna Ostapchuk

    I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

Leave a Comment