In this article i am goint to tell you about Operators in Python, Types of Operators in Python, etc. which can be very helpful for you, so read the article till last.

**Contents**hide

## Introduction

Operators in Python play a crucial role in performing various operations on data or variables, such as mathematical calculations, comparisons, and logical operations. They enable programmers to manipulate data and control the flow of their programs efficiently. In this article, we’ll discuss about operators in Python with examples, Types of Operators in Python, understanding what they are and also the seven types of operators with examples.

## What is Operator in Python ?

In Python, an operator is a symbol that performs operations on one or more operands. Operands are the values or variables on which the operator operates. Python supports a wide range of operators that allow you to perform arithmetic, comparison, logical, assignment, membership, identity, and bitwise operations.

now you should have a question, that What is operand in programming? so don’t worry, I will definitely explain this below.

## What is Operand in Python ?

In programming, particularly in Python, operands and operators are fundamental concepts that play specific roles in performing operations and calculations. Understanding the difference between operands and operators is crucial for writing effective and accurate code.

**Operands:**

**Operands** are the values or variables that are used in an operation. They are the data that operators act upon to produce a result. In simple terms, operands are the “inputs” for operations. These operands can be numeric values, variables, expressions, or any other values that can be manipulated.

For example, in the expression `3 + 5`

, the operands are `3`

and `5`

. Similarly, in the expression `x * y`

, the operands are the variables `x`

and `y`

.

**Types of Operators in Python**

Let’s take a closer look at each of the seven types of operators in Python, along with examples that showcase their usage:

- Arithmetic Operators
- Logical Operators
- Comparison Operators
- Bitwise Operators
- Membership Operators
- Assignment Operators
- Identity Operators

**Arithmetic Operators in Python**

Arithmetic operators are used to perform basic mathematical operations. They include addition (+), subtraction (-), multiplication (*), division (/), modulus (%), and exponentiation (**).

```
a = 10
b = 3
// Addition
print(a + b) # Output: 13
// Division
print(a / b) # Output: 3.333...
// Modulus
print(a % b) # Output: 1
// Multiplication
print(a*b) # Output: 30
// Substraction
print(a-b) #Output: 7
// Exponentiation
print(a**b) #Output: 1000
```

**1. Addition (+)**

The addition operator (`+`

) is used to add two numbers together.

```
a = 5
b = 3
result = a + b
print(result) # Output: 8
```

**2. Subtraction (-)**

The subtraction operator (`-`

) is used to subtract the second number from the first number.

```
x = 10
y = 7
result = x - y
print(result) # Output: 3
```

**3. Multiplication (*)**

The multiplication operator (`*`

) is used to multiply two numbers.

```
p = 4
q = 6
result = p * q
print(result) # Output: 24
```

**4. Division (/)**

The division operator (`/`

) is used to divide the first number by the second number. It returns a floating-point result, even if the division is exact.

```
m = 15
n = 3
result = m / n
print(result) # Output: 5.0
```

**5. Modulus (%)**

The modulus operator (`%`

) is used to find the remainder when the first number is divided by the second number.

```
numerator = 17
denominator = 4
remainder = numerator % denominator
print(remainder) # Output: 1
```

**6. Exponentiation (**)**

The exponentiation operator (`**`

) is used to raise the first number to the power of the second number.

```
base = 2
exponent = 3
result = base ** exponent
print(result) # Output: 8
```

**Logical Operators in Python**

Logical operators are used to combine conditional statements. They include AND (and), OR (or), and NOT (not).

Logical operators in Python are essential tools for making decisions based on the truth or falsehood of conditions. They allow you to combine or manipulate boolean values (True or False) to create more complex conditional expressions. Python supports three main logical operators: `and`

, `or`

, and `not`

.

```
p = True
q = False
print(p and q) # Output: False
print(not p) # Output: False
```

**1. and Operator:**

The `and`

operator returns `True`

if both of the operands are `True`

, and `False`

otherwise.

```
x = True
y = False
result = x and y
print(result) # Output: False
```

**2. or Operator:**

The `or`

operator returns `True`

if at least one of the operands is `True`

, and `False`

if both operands are `False`

.

```
p = True
q = False
result = p or q
print(result) # Output: True
```

**3. not Operator:**

The `not`

operator returns the opposite of the boolean value of the operand. It converts `True`

to `False`

and `False`

to `True`

.

```
is_open = False
is_closed = not is_open
print(is_closed) # Output: True
```

**Example with if Statement:**

```
temperature = 28
humidity = 80
if temperature > 25 and humidity < 90:
print("Weather is comfortable")
else:
print("Weather is not comfortable")
```

**Comparison Operators in Python**

Comparison operators are used to compare two values. They include equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=).

Comparison operators in Python are used to compare two values or expressions and determine their relationship in terms of equality, inequality, and order. These operators return boolean values (`True`

or `False`

) based on the comparison’s result. Python supports several comparison operators that allow you to perform different types of comparisons.

```
x = 5
y = 8
print(x > y) # Output: False
print(x == y) # Output: False
```

**1. Equal To ( ==)**

The equal to operator (`==`

) checks whether two values are equal.

```
x = 5
y = 5
result = x == y
print(result) # Output: True
```

**2. Not Equal To ( !=)**

The not equal to operator (`!=`

) checks whether two values are not equal.

```
a = 10
b = 7
result = a != b
print(result) # Output: True
```

**3. Greater Than ( >)**

The greater than operator (`>`

) checks if the left operand is greater than the right operand.

```
m = 15
n = 10
result = m > n
print(result) # Output: True
```

**4. Less Than ( <)**

The less than operator (`<`

) checks if the left operand is less than the right operand.

```
p = 3
q = 6
result = p < q
print(result) # Output: True
```

**5. Greater Than or Equal To ( >=)**

The greater than or equal to operator (`>=`

) checks if the left operand is greater than or equal to the right operand.

```
alpha = 25
beta = 20
result = alpha >= beta
print(result) # Output: True
```

**6. Less Than or Equal To ( <=)**

The less than or equal to operator (`<=`

) checks if the left operand is less than or equal to the right operand.

```
i = 7
j = 7
result = i <= j
print(result) # Output: True
```

**Example with if Statement:**

```
user_age = 18
legal_age = 21
if user_age >= legal_age:
print("You are allowed to enter the club.")
else:
print("Sorry, you must be at least 21 years old.")
```

**Bitwise Operators in Python**

Bitwise operators perform operations on individual bits of integers. They include AND (&), OR (|), XOR (^), left shift (<<), and right shift (>>).

Bitwise operators in Python are used to perform operations on individual bits of binary numbers. They are especially useful when working with low-level data manipulation, encoding, and decoding. Python supports several bitwise operators that allow you to perform various operations on binary representations of integers.

```
num1 = 5 # 0101 in binary
num2 = 3 # 0011 in binary
result = num1 & num2 # Bitwise AND
```

**1. Bitwise AND (&)**

The bitwise AND operator (`&`

) performs a bitwise AND operation between the corresponding bits of two integers. It results in a new integer with bits set to 1 only if both operands have the corresponding bits set to 1.

```
a = 12 # Binary: 1100
b = 6 # Binary: 0110
result = a & b
print(result) # Output: 4 (Binary: 0100)
```

**2. Bitwise OR ( |)**

The bitwise OR operator (`|`

) performs a bitwise OR operation between the corresponding bits of two integers. It results in a new integer with bits set to 1 if either or both operands have the corresponding bits set to 1.

```
x = 5 # Binary: 0101
y = 3 # Binary: 0011
result = x | y
print(result) # Output: 7 (Binary: 0111)
```

The bitwise XOR operator (`^`

) performs a bitwise exclusive OR operation between the corresponding bits of two integers. It results in a new integer with bits set to 1 if the corresponding bits of the operands are different.

```
p = 10 # Binary: 1010
q = 6 # Binary: 0110
result = p ^ q
print(result) # Output: 12 (Binary: 1100)
```

**4. Bitwise NOT ( ~)**

The bitwise NOT operator (`~`

) performs a bitwise negation operation, flipping all the bits of the integer.

```
num = 5 # Binary: 0101
result = ~num
print(result) # Output: -6 (Binary: 1010, Two's complement representation)
```

**5. Left Shift ( <<)**

The left shift operator (`<<`

) shifts the bits of an integer to the left by a specified number of positions.

```
m = 8 # Binary: 1000
shifted = m << 2
print(shifted) # Output: 32 (Binary: 100000)
```

**6. Right Shift ( >>)**

The right shift operator (`>>`

) shifts the bits of an integer to the right by a specified number of positions.

```
n = 16 # Binary: 10000
shifted = n >> 2
print(shifted) # Output: 4 (Binary: 100)
```

**Membership Operators in Python**

Membership operators are used to test if a value is a member of a sequence. They include in and not in.

```
my_list = [1, 2, 3, 4, 5]
print(3 in my_list) # Output: True
```

**1. in Operator**

The `in`

operator returns `True`

if a value is found in the sequence and `False`

if it is not found.

```
fruits = ['apple', 'banana', 'orange']
result = 'banana' in fruits
print(result) # Output: True
```

**2. not in Operator**

The `not in`

operator returns `True`

if a value is not found in the sequence and `False`

if it is found.

```
numbers = [1, 3, 5, 7, 9]
result = 4 not in numbers
print(result) # Output: True
```

**Example with Conditional Statement:**

```
colors = ['red', 'blue', 'green', 'yellow']
color_to_check = 'purple'
if color_to_check in colors:
print(f"{color_to_check} is a valid color.")
else:
print(f"{color_to_check} is not a valid color.")
```

**Assignment Operators in Python**

Assignment operators are used to assign values to variables. They include =, +=, -=, *=, /=, %=, and more.

```
num = 10
num += 5 # Equivalent to num = num + 5
```

**1. Assignment ( =) Operator**

The basic assignment operator (`=`

) is used to assign a value to a variable.

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

**2. Addition Assignment ( +=) Operator**

The addition assignment (`+=`

) operator adds the right operand to the left operand and assigns the result to the left operand.

```
total = 5
quantity = 3
total += quantity # Equivalent to: total = total + quantity
```

**3. Subtraction Assignment ( -=) Operator**

The subtraction assignment (`-=`

) operator subtracts the right operand from the left operand and assigns the result to the left operand.

```
balance = 1000
withdrawal = 200
balance -= withdrawal # Equivalent to: balance = balance - withdrawal
```

**4. Multiplication Assignment ( *=) Operator**

The multiplication assignment (`*=`

) operator multiplies the left operand by the right operand and assigns the result to the left operand.

```
count = 3
factor = 2
count *= factor # Equivalent to: count = count * factor
```

**5. Division Assignment ( /=) Operator**

The division assignment (`/=`

) operator divides the left operand by the right operand and assigns the result to the left operand.

```
total_price = 100
items = 5
total_price /= items # Equivalent to: total_price = total_price / items
```

**6. Modulus Assignment ( %=) Operator**

The modulus assignment (`%=`

) operator calculates the remainder of the division between the left operand and the right operand, then assigns the result to the left operand.

```
numerator = 17
denominator = 4
numerator %= denominator # Equivalent to: numerator = numerator % denominator
```

**7. Exponentiation Assignment ( **=) Operator**

The exponentiation assignment (`**=`

) operator raises the left operand to the power of the right operand and assigns the result to the left operand.

```
base = 2
exponent = 3
base **= exponent # Equivalent to: base = base ** exponent
```

**Identity Operators in Python**

Identity operators are used to compare the memory locations of two objects. They include is and is not.

```
a = [1, 2, 3]
b = a
print(a is b) # Output: True
```

**1. is Operator**

The `is`

operator returns `True`

if both variables point to the same memory location, indicating that they are the same object. Otherwise, it returns `False`

.

```
x = [1, 2, 3]
y = x
result = x is y
print(result) # Output: True
```

**2. is not Operator**

The `is not`

operator returns `True`

if both variables point to different memory locations, indicating that they are not the same object. Otherwise, it returns `False`

.

```
p = "hello"
q = "world"
result = p is not q
print(result) # Output: True
```

**Example with Conditional Statement:**

```
list1 = [1, 2, 3]
list2 = list1
if list1 is list2:
print("Both lists refer to the same object.")
else:
print("The lists are distinct objects.")
```

Example with Function Parameters

```
def modify_list(lst):
lst.append(4)
numbers = [1, 2, 3]
modify_list(numbers)
if numbers is modified_numbers:
print("The list was modified in place.")
else:
print("A new list was created.")
```

## Conclusion

Hope will have read the full article and the article “Operators in Python with Example” will have been very helpful for you, keep exploring….