# Python Operator Usage

Operators in Python are used to perform operations on variables and values. There are different types of operators in Python, such as arithmetic operators, assignment operators, comparison operators, logical operators, bitwise operators, membership operators, and identity operators. In this guide, we will explore the usage of these operators in Python.

## Arithmetic Operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, etc. The following are the arithmetic operators in Python:

– Addition (+): Adds two operands.

– Subtraction (-): Subtracts the second operand from the first.

– Multiplication (*): Multiplies two operands.

– Division (/): Divides the first operand by the second.

– Modulus (%): Returns the remainder of the division.

– Exponentiation (**): Raises the first operand to the power of the second.

Example:

“`python

a = 10

b = 20

print(a + b) # Output: 30

print(a – b) # Output: -10

print(a * b) # Output: 200

print(a / b) # Output: 0.5

print(a % b) # Output: 10

print(a ** 2) # Output: 100

“`

## Assignment Operators

Assignment operators are used to assign values to variables. The following are the assignment operators in Python:

– Equal (=): Assigns the value of the right operand to the left operand.

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

– Subtract and assign (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

– Multiply and assign (*=): Multiplies the right operand with the left operand and assigns the result to the left operand.

– Divide and assign (/=): Divides the left operand by the right operand and assigns the result to the left operand.

– Modulus and assign (%=): Computes the modulus of the left operand with the right operand and assigns the result to the left operand.

– Exponentiate and assign (**=): Raises the left operand to the power of the right operand and assigns the result to the left operand.

Example:

“`python

x = 10

x += 5 # Equivalent to x = x + 5

print(x) # Output: 15

y = 20

y -= 10 # Equivalent to y = y – 10

print(y) # Output: 10

z = 5

z *= 2 # Equivalent to z = z * 2

print(z) # Output: 10

“`

## Comparison Operators

Comparison operators are used to compare two values. They return either True or False based on the comparison result. The following are the comparison operators in Python:

– Equal to (==): Returns True if the operands are equal.

– Not equal to (!=): Returns True if the operands are not equal.

– Greater than (>): Returns True if the left operand is greater than the right operand.

– Less than (<): Returns True if the left operand is less than the right operand.
- Greater than or equal to (>=): Returns True if the left operand is greater than or equal to the right operand.

– Less than or equal to (<=): Returns True if the left operand is less than or equal to the right operand.
Example:
```python
a = 10
b = 20
print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: False

print(a < b) # Output: True
print(a >= b) # Output: False

print(a <= b) # Output: True
```
## Logical Operators
Logical operators are used to combine conditional statements. The following are the logical operators in Python:
- And (and): Returns True if both operands are True.
- Or (or): Returns True if at least one of the operands is True.
- Not (not): Returns True if the operand is False.
Example:
```python
x = 5
print(x > 0 and x < 10) # Output: True
print(x > 0 or x < 2) # Output: True
print(not(x > 0 and x < 10)) # Output: False
```
## Bitwise Operators
Bitwise operators are used to perform bitwise operations on integers. The following are the bitwise operators in Python:
- Bitwise AND (&): Sets each bit to 1 if both bits are 1.
- Bitwise OR (|): Sets each bit to 1 if one of the bits is 1.
- Bitwise XOR (^): Sets each bit to 1 if only one of the bits is 1.
- Bitwise NOT (~): Inverts all the bits.
- Left shift (<<): Shifts the bits to the left by the specified number of positions.
- Right shift (>>): Shifts the bits to the right by the specified number of positions.

Example:

“`python

a = 60 # 60 = 0011 1100

b = 13 # 13 = 0000 1101

print(a & b) # Output: 12 (12 = 0000 1100)

print(a | b) # Output: 61 (61 = 0011 1101)

print(a ^ b) # Output: 49 (49 = 0011 0001)

print(~a) # Output: -61

print(a << 2) # Output: 240 (240 = 1111 0000)
print(a >> 2) # Output: 15 (15 = 0000 1111)

“`

## Membership Operators

Membership operators are used to test if a sequence is present in an object. The following are the membership operators in Python:

– In: Returns True if a sequence is present in the object.

– Not in: Returns True if a sequence is not present in the object.

Example:

“`python

x = [“apple”, “banana”, “cherry”]

print(“banana” in x) # Output: True

print(“orange” not in x) # Output: True

“`

## Identity Operators

Identity operators are used to compare the memory locations of two objects. The following are the identity operators in Python:

– 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.

Example:

“`python

a = [1, 2, 3]

b = a

print(a is b) # Output: True

print(a is not b) # Output: False

“`

These are the various operators available in Python and their usage. Understanding how to use these operators effectively can help you write more concise and efficient code. Practice using these operators in different scenarios to become more comfortable with them.