Mark As Completed Discussion

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:

OperatorHow to useFunction
==value1 == value2Checks if value1 is equal to value2
>value1 > value2Checks if value1 is greater than value2
<value1 < value2Checks if value1 is less than value2
>=value1 >= value2Checks if value1 is greater than and equal to value2
<=value1 <= value2Checks if value1 is less than and equal to value2
<>value1 <> value2Checks if value1 is not equal to value2
!=value1 != value2Checks 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.

OperatorsHow to useFunction
isvalue1 is value2Checks if both the objects refer to the same object
is notvalue1 is not value2Checks 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 lists, strings, etc.

OperatorsHow to useFunction
invalue1 in anySequenceIf 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 invalue1 not in anySequenceIf 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.

OperatorsHow to useFunction
=value1 = value2It updates the value of value1 and assigns value2 to it.
+=value1 += value2It adds the value1 and value2 and assigns the new value to value1
-=value1 -= value2It subtracts value2 from value1 and assigns the new value to value1
*=value1 *= value2It multiplies value1 and value2 and assigns the new value to value1
/=value1 /= value2It divides value1 by value2 and assigns the new value to value1
%=value1 %= value2It takes the modulus of value1 and value2 and assigns the new value to value1