Python Operators

Python Operators


Python Operators

Arithmetic Operators


Name Operator Example
Addition + a + b
Subtraction - a - b
Multiplication * a * b
Division / a / b
Exponential ** a ** b
Modulus % a % b
Floor Division // a // b


Assignment Operators


Name Evaluated As
= a = b
+= a += b    or    a = a + b
-= a -= b    or    a = a - b
*= a *= b    or    a = a * b
**= a **= b    or    a = a ** b
/= a /= b    or    a = a / b
//= a //= b    or    a = a // b
%= a %= b    or    a = a % b
&= a &= b    or    a = a & b
|= a |= b    or    a = a | b
^= a ^= b    or    a = a ^ b
>>= a >>= b    or    a = a >> b
<<= a <<= b    or    a = a << b


Bitwise Operators


Name Operator Example
Bitwise AND & a & b
Bitwise OR | a | b
Bitwise NOT ~ ~a
Bitwise XOR ^ a ^ b
Bitwise right shift >> a >> b
Bitwise left shift << a << b


Comparison Operators


Name Operator Example
Equal == a == b
Not Equal != a != b
Less Than < a < b
Greater Than > a > b
Less Than or Equal to <= a <= b
Greater Than or Equal to >= a >= b


Identity Operators


Name Example Evaluated As
is a is b Returns True if a and b are same
is not a is not b Returns True if a and b are not same


Logical Operators


Name Operator Example
AND and a and b
OR or a or b
NOT not not(a and b)


Membership Operators


Name Example Evaluated As
in a in b Returns True if a is present in given sequence or collection
not in a not in b Returns True if a is not present in given sequence or collection


Operator Precedence in Python


Name Operator
Parenthesis ()
Exponential **
Complement, unary plus, unary minus ~, +, -
Multiply, divide, modulus, floor division *, /, %, //
Addition, subtraction +, -
Left shift and right shift operators <<, >>
Bitwise AND &
Bitwise OR and XOR ^, |
Comparison operators <, >, >=, <=
Equality operators ==, !=
Assignment operators =, %=, /=, //=, -=, +=, *=, **=
Identity operators is, is not
Membership operators in, not in
Logical operators and, or, not