Operators in Python
Operators in Python
There are three logical operators supported by Python. These operators (and, or, not) are to be
written in lower case only. Every value is logically either True or False and by default, all values are
True except None, False, 0 (zero), empty collections "", (), [], {}, and few other special values. So if
we say num1 = 10, num2 = -20, then both num1 and num2 are logically True.
When an expression contains different kinds of operators, precedence determines which operator
should be applied first. Higher precedence operator is evaluated before the lower precedence
operator.
Unary operators need only one operand, and they have a higher precedence than the binary
operators.
Order of
Operators Description
Precedence
1 ** Exponentiation (raised to the power)
2 ~,+, - Complement, unary plus and unary minus
3 *,/,%,// Multiply, divide, modulo and floor division
4 +,- Addition and subtraction
5 <=,<,>,>= Relational operators
6 ==,!= Equality operators
7 =,%=,/=,//=,-=,+=,*=,**= Assignment operators
8 is, is not Identity operators
9 in, not in Membership operators
10 not, and, or Logical operators
NOTE :
1) Parenthesis can be used to override the precedence of operators. The expression within
() is evaluated first.
2) For operators with equal precedence, the expression is evaluated from left to right.