Arithmetic Operators

Arithmetic operators in Python are used to perform basic mathematical operations, such as addition, subtraction, multiplication, division, and more. The primary arithmetic operators include +, -, , /, //, %, and *.

a = 10
b = 3

# Addition
print(a + b)  # Output: 13

# Subtraction
print(a - b)  # Output: 7

# Multiplication
print(a * b)  # Output: 30

# Division
print(a / b)  # Output: 3.3333333333333335

# Floor Division
print(a // b)  # Output: 3

# Modulus
print(a % b)  # Output: 1

# Exponentiation
print(a ** b)  # Output: 1000

Comparison Operators

Comparison operators in Python are used to compare two values and return a Boolean result (True or False). The primary comparison operators include ==, !=, <, >, <=, and >=.

a = 10
b = 3

# Equal to
print(a == b)  # Output: False

# Not equal to
print(a != b)  # Output: True

# Less than
print(a < b)  # Output: False

# Greater than
print(a > b)  # Output: True

# Less than or equal to
print(a <= b)  # Output: False

# Greater than or equal to
print(a >= b)  # Output: True

Logical Operators

Logical operators in Python are used to combine conditional statements and return Boolean results (True or False). The primary logical operators are and, or, and not.

a = True
b = False

# Logical AND
print(a and b)  # Output: False

# Logical OR
print(a or b)  # Output: True

# Logical NOT
print(not a)  # Output: False

Bitwise Operators

Bitwise operators in Python are used to perform bit-level operations on integer values. The primary bitwise operators include &, |, ^, ~, <<, and >>.

a = 10  # Binary: 1010
b = 3   # Binary: 0011

# Bitwise AND
print(a & b)  # Output: 2  (Binary: 0010)

# Bitwise OR
print(a | b)  # Output: 11 (Binary: 1011)

# Bitwise XOR
print(a ^ b)  # Output: 9  (Binary: 1001)

# Bitwise NOT
print(~a)     # Output: -11 (Binary: ...11110101, two's complement representation)

# Bitwise left shift
print(a << 1)  # Output: 20 (Binary: 10100)

# Bitwise right shift
print(a >> 1)  # Output: 5  (Binary: 0101)

Assignment Operators

Assignment operators in Python are used to assign values to variables and can also perform operations before the assignment. The primary assignment operators include =, +=, -=, =, /=, %=, //=, *=, &=, |=, ^=, <<=, and >>=.

a = 10
b = 3

# Simple assignment
a = b        # a is now 3

# Add and assign
a += b       # a is now 6 (3 + 3)

# Subtract and assign
a -= b       # a is now 3 (6 - 3)

# Multiply and assign
a *= b       # a is now 9 (3 * 3)

# Divide and assign
a /= b       # a is now 3.0 (9 / 3)

# Modulus and assign
a %= b       # a is now 0.0 (3.0 % 3)

# Floor division and assign
a = 10       # reset a to 10
a //= b      # a is now 3 (10 // 3)

# Exponentiation and assign
a **= b      # a is now 27 (3 ** 3)

# Bitwise AND and assign
a &= b       # a is now 3 (27 & 3)

# Bitwise OR and assign
a |= b       # a is now 3 (3 | 3)

# Bitwise XOR and assign
a ^= b       # a is now 0 (3 ^ 3)

# Bitwise left shift and assign
a = 10       # reset a to 10
a <<= b      # a is now 80 (10 << 3)

# Bitwise right shift and assign
a >>= b      # a is now 10 (80 >> 3)