## Introduction to Python Operators

Python operators are special symbols or reserved keywords used to execute operations on one or more operands(values or variables). Operators are essential to programming, allowing developers to manipulate data and control program flow. Python has many operators that perform various operations such as arithmetic, comparison, logical, assignment, identity, membership, and bitwise operations.

Operators can operate on different types of data, such as numbers, strings, lists, tuples, and dictionaries, depending on the type of operator and the data type of the operands. For example, the addition operator (+) can be used to add two numbers, concatenate two strings, or merge two lists.

Python operators have a specific order of precedence, which determines the order in which operators are evaluated in an expression. The order of precedence ensures that expressions are evaluated correctly, following the standard mathematical rules. In cases where multiple operators have the same precedence, the order of evaluation follows the associativity of the operator.For example, some operators, such as addition and multiplication, are left-associative, meaning they are evaluated from left to right. Others, such as exponentiation, are right-associative, meaning they are evaluated from right to left.

Understanding Python operators is critical for developing Python programs. By using operators, developers can write code that performs complex operations quickly and efficiently, allowing them to create more powerful and effective programs.

### Types of Python Operators

Python Operators are a fundamental part of programming that allows developers to manipulate data and control program flow. Python has several types of operators that can be used to perform different operations. Python’s most common types of operators include arithmetic operators, assignment operators, comparison operators, logical operators, identity operators, membership operators, and bitwise operators. Understanding the different types of Python operators and how to use them effectively is critical for developing Python programs that can perform complex operations on data.

Python language supports the following types of operators.

#### 1. Arithmetic Operator

Arithmetic operators are used to perform mathematical operations.

Operator |
Description |
Syntax |
Output |

+ | Addition | a+b | Returns the sum of the operands |

– | Subtraction | a-b | Returns Difference of the operands |

/ | Division | a/b | Returns Quotient of the operands |

* | Multiplication | a*b | Returns product of the operands |

** | Exponentiation | a**b | returns exponent of a raised to the power b |

% | Modulus | a%b | returns the remainder of the division |

// | Floor division | a//b | returns a real value and ignores the decimal part |

Let us consider an example program for carrying out the arithmetic operations explained above.

Let us consider two integers Xa=2 and Xb=3

**Code:**

```
Xa = int(input('Enter First number: '))
Xb = int(input('Enter Second number: '))
add = Xa + Xb
diff = Xa - Xb
mul = Xa * Xb
div = Xa / Xb
floor_div = Xa // Xb
power = Xa ** Xb
modulus = Xa % Xb
print('Sum of the numbers is',Xa ,'and' ,Xb ,'is :',add)
print('Difference of the numbers is ',Xa ,'and' ,Xb ,'is :',diff)
print('Product of the numbers is ' ,Xa ,'and' ,Xb ,'is :',mul)
print('Division of the numbers is ',Xa ,'and' ,Xb ,'is :',div)
print('Floor Division of the numbers is ',Xa ,'and' ,Xb ,'is :',floor_div)
print('Exponent of the numbers is ',Xa ,'and' ,Xb ,'is :',power)
print('Modulus of the numbers is ',Xa ,'and' ,Xb ,'is :',modulus)
```

**Output:**

#### 2. Bitwise Operators

In python bitwise operator is used to perform operations onto the binary representation for the integer values. The bitwise operator works on bits and performs the operations bit by bit. Refers to the operators working on a bit, i.e. they treat the operand as a string of bits; for example, in bitwise operations, 5 will be considered as 0101.

The box below provides the bitwise operators in python

Operator |
Description |
Syntax |
Output |

& | Binary AND | a&b | copies a bit to the result if it exists in both operands |

| | Binary OR | a|b | copies a bit if it exists in either operand. |

^ | Binary XOR | a^b | copies the bit if it is set in one operand but not both. |

~ | Binary One’s Complement | a~b | Unary operation of flipping bits |

<< | Binary Left Shift | a<<b | The left operand value is moved left by the number of bits specified by the right operand. |

>> | Binary Right Shift | a>>b | left operands value is moved right by the number of bits specified by the right operand. |

The below example shows the bitwise operator as follows. In the below example, we have defined all the bitwise operators as follows.

**Code:**

```
first_operand = 10
second_operand = 15
res = first_operand & second_operand
print ("first_operand & second_operand : ", res)
res = first_operand | second_operand
print ("first_operand | second_operand : ", res)
res = first_operand ^ second_operand
print ("first_operand ^ second_operand : ", res)
res = ~first_operand;
print ("~first_operand : ", res)
res = first_operand << 5;
print ("first_operand << 5 : ", res)
res = first_operand >> 5;
print ("first_operand >> 5 : ", res)
```

**Output:**

#### 3. Membership Operators

Refers to the operators used in the validation of membership of operand test in a sequence, such as strings, lists, or tuples. There are two types of membership operators in python

Operator |
Syntax |
Output |

in | if (a in x): | Evaluates to true if it finds a variable in the specified sequence and false otherwise. |

not in | If ( b not in x ): | Evaluates to true if it does not finds a variable in the specified sequence and false otherwise. |

The below example shows the membership operator as follows.

**Code:**

```
first_operand = 29
second_operand = 23
list = [11, 15, 19, 23, 27]
if (first_operand not in list):
print("first_operand is NOT present in given list")
else:
print("first_operand is present in given list")
if (second_operand in list):
print("second_operand is present in given list")
else:
print("second_operand is NOT present in given list")
```

**Output:**

#### 4. Identity Operators

In python identity operator is used to compare the memory locations of two objects and used to return the Boolean value that depends on whether they refer to the same object. There are two types of identity operators in python.

Operator |
Syntax |
Output |

is | x is y | returns True if the type of the value in y points to the same type in the x. |

is not | x is not y | returns True if the type of the value in y points to a different type than the value in the x |

The below example shows the identity operator as follows.

**Code:**

```
first_operand = 10
second_operand = 20
third_operand = first_operand
print(first_operand is not second_operand)
print(first_operand is third_operand)
```

**Output:**

#### 5. Comparison Operators

A comparison operator is used to compare the values of two operands and after comparing the value it will return a true or false Boolean value. it is also known as Relational operators.

Operator |
Syntax |
Output |

== | (a == b) | If the values of a and b are equal, then the condition becomes true. |

!= | (a != b) | If values of a and b are not equal, then the condition becomes true. |

<> | (a <> b) | If values of a and b are not equal, then the condition becomes true. |

> | (a > b) | If the value of a is greater than the value of b, then the condition becomes true. |

< | (a < b) | If the value of a is less than the value of b, then the condition becomes true. |

>= | (a >= b) | If the value of a is greater than or equal to the value of b, then the condition becomes true. |

<= | (a <= b) | If the value of b is less than or equal to the value of b, then the condition becomes true. |

The below example shows the comparison operator as follows.

**Code:**

```
first_operand = 15
second_operand = 25
print("first_operand == second_operand : ", first_operand == second_operand)
print("first_operand != second_operand : ", first_operand != second_operand)
print("first_operand > second_operand : ", first_operand > second_operand)
print("first_operand < second_operand : ", first_operand < second_operand)
print("first_operand >= second_operand : ", first_operand >= second_operand)
print("first_operand <= second_operand : ", first_operand <= second_operand)
```

**Output:**

#### 6. Assignment Operators

Assignment operators in python are used to assign the values to the variables. Following are the types of assignment operators in python.

Operator |
Description |
Syntax |
Output |

= | Equal to | c = a + b | assigns a value of a + b into c |

+= | Add AND | c += a | is equivalent to c = c + a |

-= | Subtract AND | c -= a | is equivalent to c = c – a |

*= | Multiply AND | c *= a | is equivalent to c = c * a |

/= | Divide AND | c /= a | is equivalent to c = c / ac /= a is equivalent to c = c / a |

%= | Modulus AND | c %= a | is equivalent to c = c % a |

**= | Exponent AND | c **= a | is equivalent to c = c ** a |

//= | Floor Division | c //= a | is equivalent to c = c // a |

The below example shows the assignment operator as follows.

**Code:**

```
first_operand = 10
first_operand += 5
print ("first_operand += 5 : ", first_operand)
first_operand -= 5
print ("first_operand -= 5 : ", first_operand)
first_operand *= 5
print ("first_operand *= 5 : ", first_operand)
first_operand /= 5
print ("first_operand /= 5 : ",first_operand)
first_operand %= 3
print ("first_operand %= 3 : ", first_operand)
first_operand **= 2
print ("first_operand **= 2 : ", first_operand)
first_operand //= 3
print ("first_operand //= 3 : ", first_operand)
```

**Output:**

#### 7. Logical Operators

These operators are used to perform similar operations as that of logical gates; there are 3 types of logical operators in python.

Operator |
Description |
Syntax |
Output |

and | Logical AND | a and b | a condition is true if both a and b are true |

or | Logical OR | a or b | a condition is true if either a and b are true |

not | Logical NOT | not a | Complement the operand |

The below example shows the logical operator as follows.

**Code:**

```
first_operand = True
second_operand = False
print(first_operand and second_operand)
print(first_operand or second_operand)
print(not first_operand)
```

**Output:**

### Python Operator Precedence

Operator precedence in python determines the order in which operators are evaluated in an expression. The below table shows operator precedence as follows.

Operator |
Description |

** | Exponentiation |

‘~ + -’ | Bitwise NOT, Unary Plus and Minus |

<‘* / % //’ | Multiplication, division, modulus, and floor division |

‘+ -’ | Addition and subtraction |

‘>> <<’ | Bitwise shift right and left |

& | Bitwise AND |

^ | Bitwise XOR |

and | Logical and |

not | Logical not |

or | Logical or |

‘== != > >= < <=’ | Comparison Operators |

= %= /= //= -= += *= **= | Assignment Operators |

Is, is not | Identity Operators |

in not in | Membership Operators |

not or and | Logical Operators |

**Conclusion**

In conclusion, It is important to have a good understanding of Python operators and their precedence to write efficient and error-free code. With its intuitive syntax and a vast library of operators, Python remains a popular choice for developers across various domains,from web development to data science and machine learning. With its ease of use and flexibility, Python is an excellent language for both beginners and advanced programmers alike.

### Recommended Articles

This has been a guide to Python Operators. Here we discuss the various Python Operators like Logical, Comparison, Arithmetic, etc. You may also look at the following articles to gain vast amount of information.

- Python 3 Commands
- Is Python a scripting language?
- Frameworks of Python
- Assignment Operators in Python

40 Online Courses | 13 Hands-on Projects | 215+ Hours | Verifiable Certificate of Completion

4.8

View Course

Related Courses