Operators in Python

Operators in Python are used to perform operations on values and variables. Python supports several types of operators, including:

1. Arithmetic operators: Used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus (remainder).

python
# Arithmetic operators
x = 10
y = 3

print(x + y) # Output: 13
print(x – y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1


2. Comparison operators: Used to compare values and return a Boolean value (`True` or `False`).

python
# Comparison operators
x = 10
y = 3

print(x > y) # Output: True
print(x < y) # Output: False print(x == y) # Output: False print(x >= y) # Output: True
print(x <= y) # Output: False


3. Logical operators: Used to combine Boolean expressions and return a Boolean value.

python
# Logical operators
x = 5
y = 10
z = 15

print(x < y and y < z) # Output: True print(x < y or y > z) # Output: True
print(not(x < y)) # Output: False


4. Assignment operators: Used to assign values to variables.

python
# Assignment operators
x = 10
x += 5 # Equivalent to x = x + 5
x -= 3 # Equivalent to x = x – 3
x *= 2 # Equivalent to x = x * 2
x /= 4 # Equivalent to x = x / 4

print(x) # Output: 6.25


5. Bitwise operators: Used to perform bitwise operations on integers.

python
# Bitwise operators
x = 5 # 0b0101 in binary
y = 3 # 0b0011 in binary

print(x & y) # Output: 1 (0b0001 in binary)
print(x | y) # Output: 7 (0b0111 in binary)
print(x ^ y) # Output: 6 (0b0110 in binary)
print(

x)     # Output: -6 (0b1010 in binary)

These are just some of the operators that Python supports. There are many more operators and variations of operators that can be used in Python depending on the type of data being used and the operation being performed.