Are there operators in Python other than arithmetic and logical operators?
Yes, there are operators in Python other than arithmetic and logical ones. The others are comparison
, identity
, membership
, and assignment
operators. Each type of operators contains multiple operators. Let's have a look at each one by one.
Comparison Operators
These operators are used for the comparison between two values. These are following:
Operator | How to use | Function |
---|---|---|
== | value1 == value2 | Checks if value1 is equal to value2 |
> | value1 > value2 | Checks if value1 is greater than value2 |
< | value1 < value2 | Checks if value1 is less than value2 |
>= | value1 >= value2 | Checks if value1 is greater than and equal to value2 |
<= | value1 <= value2 | Checks if value1 is less than and equal to value2 |
<> | value1 <> value2 | Checks if value1 is not equal to value2 |
!= | value1 != value2 | Checks if value1 is not equal to value2 |
Identity Operators
These operators are used to compare the identity of the variables or type of the variables.
Operators | How to use | Function |
---|---|---|
is | value1 is value2 | Checks if both the objects refer to the same object |
is not | value1 is not value2 | Checks if both the objects do not refer to the same object |
Membership Operators
These operators are used to check the membership of a variable in a sequence like list
s, string
s, etc.
Operators | How to use | Function |
---|---|---|
in | value1 in anySequence | If used with a condition, it checks if a value is present in a sequence. If used with a for loop, it creates value1 variable and assign sequence values to it one by one. |
not in | value1 not in anySequence | If used with a condition, it checks if a value is not present in a sequence. |
Assignment Operators
These operators are used to assign a value to a variable. The main assignment operator is =. It can be used with the arithmetic operators.
Operators | How to use | Function |
---|---|---|
= | value1 = value2 | It updates the value of value1 and assigns value2 to it. |
+= | value1 += value2 | It adds the value1 and value2 and assigns the new value to value1 |
-= | value1 -= value2 | It subtracts value2 from value1 and assigns the new value to value1 |
*= | value1 *= value2 | It multiplies value1 and value2 and assigns the new value to value1 |
/= | value1 /= value2 | It divides value1 by value2 and assigns the new value to value1 |
%= | value1 %= value2 | It takes the modulus of value1 and value2 and assigns the new value to value1 |