Python Operators

Certainly! Python, like many other programming languages, has a variety of operators that allow you to perform operations on data. Here's an explanation of some of the most commonly used Python operators along with examples:

1. Arithmetic Operators:

+ (Addition): Adds two operands.

a = 5
b = 2
sum_result = a + b  # sum_result is 7
product_result = a * b  # product_result is 10
Python

- (Subtraction): Subtracts the right operand from the left operand.

a = 5
b = 2
difference = a - b  # difference is 3
Python

* (Multiplication): Multiplies two operands.

a = 5
b = 2
product = a * b  # product is 10
Python

/ (Division): Divides the left operand by the right operand.

a = 10
b = 2
result = a / b  # result is 5.0
Python

% (Modulus): Computes the remainder of the division of the left operand by the right operand.

a = 17
b = 5
remainder = a % b  # remainder is 2
Python

** (Exponentiation): Raises the left operand to the power of the right operand.

a = 2
b = 3
result = a ** b  # result is 8
Python

2. Relational Operators:

== (Equal to): Tests if two operands are equal.

x = 5
y = 3
is_equal = x == y  # is_equal is False
Python

!= (Not equal to): Tests if two operands are not equal.

x = 5
y = 3
is_not_equal = x != y  # is_not_equal is True
Python

< (Less than): Checks if the left operand is less than the right operand.

a = 3
b = 5
is_less = a < b  # is_less is True
Python

> (Greater than): Checks if the left operand is greater than the right operand.

a = 8
b = 5
is_greater = a > b  # is_greater is True
Python

<= (Less than or equal to): Checks if the left operand is less than or equal to the right operand.

a = 5
b = 5
is_less_or_equal = a <= b  # is_less_or_equal is True
Python

>= (Greater than or equal to): Checks if the left operand is greater than or equal to the right operand.

a = 6
b = 5
is_greater_or_equal = a >= b  # is_greater_or_equal is True
Python

3. Logical Operators:

and (Logical AND): Returns True if both operands are True.

p = True
q = False
logical_and = p and q  # logical_and is False
Python

or (Logical OR): Returns True if at least one of the operands is True.

p = True
q = False
logical_or = p or q  # logical_or is True
Python

not (Logical NOT): Inverts the value of the operand; if it's True, it becomes False, and vice versa.

p = True
logical_not = not p  # logical_not is False
Python

4. Assignment Operators:

= (Assignment): Assigns the value on the right to the variable on the left.

x = 5
y = 10
x = y  # 'x' is now 10
Python

+= (Add and assign): Adds the right operand to the left operand and stores the result in the left operand.

a = 5
b = 3
a += b  # 'a' is now 8
Python

-= (Subtract and assign): Subtracts the right operand from the left operand and stores the result in the left operand.

x = 10
y = 4
x -= y  # 'x' is now 6
Python

These are some of the fundamental operators in Python. Python also provides other operators for tasks like bitwise operations, membership testing, identity testing, and more.