Operators in Python | Types of Operators in Python with Example

In this post, we will learn about what is operator and different types of operators in Python.

What is an Operator?

An operator is a special symbol which are used to perform an operation on variable or values.

Types of Operators in Python

Python Arithmetic Operators

Arithmetic operators in python are used to perform mathematical calculations like addition, subtraction, multiplication, etc.

Symbol Operation Example
+ Addition x + y
Subtraction x – y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponent x ** y
// Floor Division x // y

Python Comparison Operators

The comparison operator in python is used to compare two operands.

Symbol Operation Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than equal to x >= y
<= Less than equal to x <= y

Python Logical Operators

The logical operator in python is used to perform logical operations on two values or variables. It is used in a conditional statement that is either true or false.

Symbol Operation Example
and Returns true if both the conditions are true x < 8 and x < 10
or Returns true if any one condition is true x < 6 or x < 10
not Returns true if the result is false and vice versa not(x < 6 or x < 10)

Python Identity Operators

Python identity operator is used to compare two objects not only if they are equal but also to make sure that both the objects are equal with the same memory location.

Symbol Operation Example
is Returns true only if both the variables are the same object x is y
is not Returns true only if both the variables are not the same object x is not y

Python Membership Operator

The membership operator in python is used to validate the membership of value.

Symbol Operation Example
in Returns true if the sequence with the specified value is present in the object x in y
not in Returns true if the sequence with the specified value is not present in the object x in not y

Python Bitwise Operators

Bitwise operator in Python is used to perform an action on bit-level programming.

Symbol Operation Description
& AND Set each bit to 1 if both bits are 1
| OR Set each bit to 1 if one of the two bits is 1
^ XOR Set each bit to 1 if only one of the two bits is 1
~ NOT Inverts all bits
<< Left Shift
>> Right Shift

Python Assignment Operators

The assignment operator in python is used to assign values to the variable.

Symbol Operation Example
= x = 10 x = 10
+= x = x + 10
-= x = x – 10 x -= 10
*= x = x * 10 x *= 10
/= x = x / 10 x /= 10
%= x = x % 10 x %= 10
//= x = x // 10 x //= 10
**= x = x ** 10 x **= 10
&= x = x & 10 x &= 10
|= x = x | 10 x |= 10
^= x = x ^ 10 x ^= 10
>>= x = x >>= 10 x >>= 10
<<= x = x <<= 10 x <<= 10

Leave a Reply