## Introduction to Python Operators

Constructs in Python programming language that instruct the interpreter to perform a certain function involving two or more variables upon which the construct operates are called as python operators. Though not actually seen as functions, but have their own identity as concepts, because of being different syntactically and semantically from functions. It performs tasks of various nature including arithmetic, bitwise, membership, identity, comparison, assignment, and logical etc. with a certain order of execution existing among them.

### Python Operators

Operators in python are constructs in python which instructs the interpreter to perform a certain function, however, these are traditionally not defined as a function rather they are syntactically and semantically different from functions. Operators are used to performing operations on variables and values according to their use.

Python language supports the following types of operators.

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

#### 1. Arithmetic Operator

Arithmetic operators used to perform mathematical operations

Operator |
Description |
Syntax |
Output |

+ | Addition | a+b | Returns 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 remainder of the division |

// | Floor division | a//b | returns real value and ignores 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

**Program**

`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

Refers to the operators working on bit i.e. they treat the operand as a string of bit 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 | left operands 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. |

#### 3. Membership Operators

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

4.8 (5,742 ratings)

View Course

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

#### 4. Identity Operators

Used to compare the memory locations of the operands, they are quite often used to determine if the operand is of a particular type, 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 |

#### 5. Comparison Operators

Also known as Relational operators, these operators are used in determining the relation between the operand on either side of the operator.

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 condition becomes true. |

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

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

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

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

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

#### 6. Assignment Operators

Refer as the name suggests is used to declare assignments to the operands, 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 |

#### 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 |

Python Operators are a backbone of any operations and functions in the programming context.

### 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 learn more: