# Operators In Python

Operators in Python are symbols that perform operations on variables and values. This tutorial covers various types of operators and how to use them in Python.

Example

``````
# Using + Operator for addition
sum = 1 + 2
print(sum)
``````

## Types Of Operators

Here is the list of different type of operators we will learn in this tutorial:

As you can see there are around 7 types of operators so let’s see each one of them one by one.

## 1. Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations such as addition, subtraction, multiplication, division etc.

Example

``````
x = 10
y = 3

result_addition = x + y

# Subtraction
result_subtraction = x - y
print("Subtraction:", result_subtraction)

# Multiplication
result_multiplication = x * y
print("Multiplication:", result_multiplication)

# Division
result_division = x / y
print("Division:", result_division)

# Modulus (remainder)
result_modulus = x % y
print("Modulus (remainder):", result_modulus)

# Exponentiation
result_exponentiation = x ** y
print("Exponentiation:", result_exponentiation)

# Floor Division
result_floor_division = x // y
print("Floor Divsion:", result_floor_division)
``````

## 2. Comparison Operators

Comparison operators are used to compare values and return a Boolean result

Example

``````
x = 10
y = 5

# Equal to
is_equal = x == y
print("Equal to:", is_equal)

# Not equal to
is_not_equal = x != y
print("Not equal to:", is_not_equal)

# Greater than
is_greater_than = x > y
print("Greater than:", is_greater_than)

# Less than
is_less_than = x < y
print("Less than:", is_less_than)

# Greater than or equal to
is_greater_equal = x >= y
print("Greater than or equal to:", is_greater_equal)

# Less than or equal to
is_less_equal = x <= y
print("Less than or equal to:", is_less_equal)
``````

## 3. Logical Operators

A logical operators are used to connect two or more expressions.

Example

``````
x = True
y = False

# AND
result_and = x and y
print("Logical AND:", result_and)

# OR
result_or = x or y
print("Logical OR:", result_or)

# NOT
result_not = not x
print("Logical NOT:", result_not)
``````

## 4. Assignment Operators

Assignment operators are used to assign values.

Example

``````
x = 5

# += (Addition assignment)
x += 3

# -= (Subtraction assignment)
x -= 2
print("Subtraction assignment:", x)

# *= (Multiplication assignment)
x *= 4
print("Multiplication assignment:", x)

# /= (Division assignment)
x /= 2
print("Division assignment:", x)
``````

## 5. Identity Operators

Identity operators are used to compare the memory locations of two objects..

Example

``````
x = [1, 2, 3]
y = [1, 2, 3]

# is (True if the variables reference the same object)
is_same_object = x is y
print("is (same object):", is_same_object)

# is not (True if the variables reference different objects)
is_different_object = x is not y
print("is not (different object):", is_different_object)

``````

## 6. Membership Operators

Membership operators are used to check if a value is a member of a sequence (e.g., list, tuple).

Example

``````
numbers = [1, 2, 3, 4, 5]

# in (True if the value is in the sequence)
is_in_sequence = 3 in numbers
print("in (in sequence):", is_in_sequence)

# not in (True if the value is not in the sequence)
is_not_in_sequence = 6 not in numbers
print("not in (not in sequence):", is_not_in_sequence)
``````

## 7. Bitwise Operators

Bitwise operators in Python manipulate individual bits of binary numbers.

Example

``````
# Bitwise AND
x = 5  # 0b0101 in binary
y = 3  # 0b0011 in binary
result_and = x & y
print("Bitwise AND:", result_and)

# Bitwise OR
result_or = x | y
print("Bitwise OR:", result_or)

# Bitwise XOR
result_xor = x ^ y
print("Bitwise XOR:", result_xor)

# Bitwise NOT
result_not_x = ~x
print("Bitwise NOT (x):", result_not_x)

# Bitwise Left Shift
result_left_shift = x << 1
print("Bitwise Left Shift:", result_left_shift)

# Bitwise Right Shift
result_right_shift = x >> 1
print("Bitwise Right Shift:", result_right_shift)
``````

So these were all the operators in python, don't worry you will not require to memorise all of them there are few which will be used most of the time.