Python Tutorial Python Advanced Python References Python Libraries

Python - Operators



Operators are used to perform operation on two operands. Operators in Python can be categorized as follows:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Identity operators
  • Membership operators
  • Bitwise operators

Python Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

OperatorNameDescription
+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
**Exponent / PowerReturns first operand raised to the power of second operand
%ModuloReturns remainder of division operation
//Floor divisionReturns quotient of division operation

Example

Python Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

Operator Expression Equivalent toExample
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
**=a **= ba = a ** b
%=a %= ba = a % b
//=a //= ba = a // b
&=a &= ba = a & bMore Info
|=a |= ba = a | bMore Info
^=a ^= ba = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
<<=a <<= ba = a << bMore Info

Python Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

Operator Description
==Equal
!= Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to

Example

Python Logical operators

Logical operators are used to combine two or more conditions.

Operator Description
and Returns true when all conditions are true
orReturns true when any of the conditions is true
notReturns opposite boolean result

More Info

Python Identity operators

Identity operators are used to compare memory location of two objects.

Operator Description
isReturns true when both variables are the same objecta is b
is notReturns true when both variables are not the same objecta is not b

More Info

Python Membership operators

Membership operators are used to check the membership of element(s) in a sequence like lists, tuple etc.

Operator Description Example
in Returns true if a given element is present in the objecta in b
not inReturns true if a given element is not present in the objecta not in b

More Info

Python Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
& ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0More Info
~ NOTReverse all the bitsMore Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operandMore Info
<<Left shiftThe left operand value is moved left by the number of bits present in the right operandMore Info

Python Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The following table lists the precedence and associativity of Python operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

Operators in the same group have associativity from left to right, except exponentiation, which has associativity from right to left.

PrecedenceOperatorDescription
18(expressions...)Binding or parenthesized expression
[expressions...]List display
{key: value...}Dictionary display
{expressions...}Set display
17x[index]Subscription
x[index:index]Slicing
x(arguments...)Call
x.attributeAttribute reference
16await xAwait expression
15**Exponentiation
14+x  -xUnary plus, Unary negation
~Bitwise NOT
13*  /  //  %Multiplication, Division, Floor division, Remainder
@Matrix multiplication
12+  -Addition, Subtraction
11<<  >> Bitwise left shift, right shift
10&Bitwise AND
9^Bitwise XOR
8|Bitwise OR
7<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equal
isIdentity operators
is not
inMembership operators
not in
6not xBoolean NOT
5andBoolean AND
4orBoolean OR
3if-elseConditional expression
2lambdaLambda expression
1=Direct assignment
+=  -=  *=  /=  //=  %=  **=Compound assignment by addition, subtraction, multiplication, division, floor division, remainder, and exponentiation
<<=  >>=Compound assignment by Bitwise left shift, right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR