Skip to main content

Python Operators

Python Operators are used to perform operations on values and variables. Several groups of operators exist in Python.

What are operators in Python?

Operators are special symbols that indicate that some kind of calculation is to be performed.

The values on which an operator acts are called operands.

For example

What operators exist in Python?

Python divides operators into the following groups:

note

They are discussed individually, with examples, on the dedicated pages.

Arithmetic operators

Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, etc.

OperatorDescriptionExample
+Add two operands or unary plusx + y + 1
-Subtract right operand from the left or unary minusx - y - 1
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus: remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division: division that results into whole number adjusted to the left in the number linex // y
**Exponent: left operand raised to the power of rightx**y (x to the power y)

Assignment operators

Assignment operators are used to assign values to variables

OperatorDescriptionExampleEquivalent to
=Assign value of right side of expression to left side operandx = 2x = 2
+=Add right-side operand with left side operand and then assign to left operandx += 2x = x + 2
-=Subtract right operand from left operand and then assign to left operandx -= 2x = x - 2
*=Multiply right operand with left operand and then assign to left operandx *= 2x = x * 2
/=Divide left operand with right operand and then assign to left operandx /= 2x = x / 2
%=Takes modulus using left and right operands and assign the result to left operandx %= 2x = x % 2
//=Divide left operand with right operand and then assign the value(floor) to left operandx //= 2x = x // 2
**=Calculate exponent(raise power) value using operands and assign value to left operandx **= 2x = x ** 2
&=Performs Bitwise AND on operands and assign value to left operandx &= 2x = x & 2
&124;=Performs Bitwise OR on operands and assign value to left operandx &124;= 2x = x &124; 2
^=Performs Bitwise XOR on operands and assign value to left operandx ^= 2x = x ^ 2
>>=Performs Bitwise right shift on operands and assign value to left operandx >>= 2x = x >> 2
<<=Performs Bitwise left shift on operands and assign value to left operandx <<= 2x = x << 2

Comparison operators

Comparison operators are used to compare values.

note

They return either True or False according to the condition.

OperatorMeaningExample
>Greater than: True if left operand is greater than the rightx > y
<Less than: True if left operand is less than the rightx < y
==Equal to: True if both operands are equalx == y
!=Not equal to: True if operands are not equalx != y
>=Greater than or equal to: True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to: True if left operand is less than or equal to the rightx <= y

Logical operators

Logical operators are the and, or, not operators.

They are used to combine conditional statements.

OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x

Identity operators

Identity operators are used to check if two values are located on the same part of the memory.

OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)x is True
is notTrue if the operands are not identical (do not refer to the same object)x is not True

Membership operators

Membership operators are used to test whether a value or variable is in a sequence.

OperatorMeaningExample
inTrue if value/variable is found in the sequence2 in x
not inTrue if value/variable is not found in the sequence2 not in x

Bitwise operators

Bitwise operators act on bits and perform the bit-by-bit operations.

These are used to operate on binary numbers.

OperatorMeaningExample
&Bitwise ANDx & y = 0 (0000 0000)
&124;Bitwise ORx &124; y = 14 (0000 1110)
~Bitwise NOT~x = -11 (1111 0101)
^Bitwise XORx ^ y = 14 (0000 1110)
>>Bitwise right shiftx >> 2 = 2 (0000 0010)
<<Bitwise left shiftx << 2 = 40 (0010 1000)