Python Operators

Operators are used to perform operations on values and variables.

The Python operators are classified into seven different categories:

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

Arithmetic Operators

Arithmetic operators are used to perform simple mathematical operations on numeric values (except complex).

Python Arithmetic operators
OperatorMeaningExample
+Additionx + y
Subtractionx – y
*Multiplicationx * y
/Divisionx / y
%Modulusx % y
**Exponentiationx ** y
//Floor divisionx // y

Here are some examples:

Examples:

Suppose:

x = 6
y = 2

Arithmetic operations:

# addition
print(x + y)	# 8
# subtraction
print(x - y)	# 4
# multiplication
print(x * y)	# 12
# division
print(x / y)	# 3
# modulus
print(x % y)	# 0
# exponentiation
print(x ** y)	# 36
# floor division
print(x // y)	# 3

For additional numeric operations see the math module.

Assignment Operators

Assignment operators are used to assign new values to variables.

Python Assignment operators
OperatorMeaningExampleEquivatent to
=Assignmentx = 3x = 3
+=Addition assignmentx += 3x = x + 3
-=Subtraction assignmentx -= 3x = x – 3
*=Multiplication assignmentx *= 3x = x * 3
/=Division assignmentx /= 3x = x / 3
%=Modulus assignmentx %= 3x = x % 3
//=Floor division assignmentx //= 3x = x // 3
**=Exponentiation assignmentx **= 3x = x ** 3
&=Bitwise AND assignmentx &= 3x = x & 3
|=Bitwise OR assignmentx |= 3x = x | 3
^=Bitwise XOR assignmentx ^= 3x = x ^ 3
>>=Bitwise right shift assignmentx >>= 3x = x >> 3
<<=Bitwise left shift assignmentx <<= 3x = x << 3

Comparison Operators

Comparison operators are used to compare two values.

Python Comparison operators
OperatorMeaningExample
==Equal tox == y
!=Not equal tox != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equal tox <= y

Here are some examples:

Examples:

Suppose:

x = 6
y = 2

Comparison operations:

# equal to
print(x == y)	# False
# not equal to
print(x != y)	# True
# greater than
print(x > y)	# True
# less than
print(x < y)	# False
# greater than or equal to
print(x >= y)	# True
# less than or equal to
print(x <= y)	# False

Logical Operators

Logical operators are used to join two or more conditions.

Python Logical operators
OperatorDescriptionExample
andReturns True if both statements are truex > 0 and y < 0
orReturns True if one of the statements is truex > 0 or y < 0
notReverse the result, returns False if the result is truenot(x > 0 and y < 0)

Here are some examples:

Examples:

Suppose:

x = 2
y = -2

Logical operations:

# and
print(x > 0 and y < 0)			# True
# or
print(x > 0 or y < 0)			# True
# not
print(not(x > 0 and y < 0))		# False

Identity Operators

Identity operators are used to check if two objects point to the same object, with the same memory location.

Python Identity operators
OperatorDescriptionExample
isReturns true if both variables are the same objectx is y
is notReturns true if both variables are not the same objectx is not y

Here are some examples:

Examples:

Suppose:

x = [1, 2, 3]
y = [1, 2, 3]

Identity operations:

# is
print(x is y)		# False
# is not
print(x is not y)	# True

Membership Operators

Membership operators are used to check if a specific item is present in a sequence (such as a stringtuplelist, or range) or a collection (such as a dictionaryset, or frozen set).

Python Membership operators
OperatorDescriptionExample
inReturns True if a value is present in the sequencex in y
not inReturns True if a value is not present in the sequencex not in y

Here are some examples:

Examples:

Suppose:

L = ['red', 'green', 'blue']

Membership operations:

# in
print('red' in L)			# True
# not in
print('yellow' not in L)	# True

Bitwise Operators

Binary operators are used to perform bit-level operations on (binary) numbers.

Python Bitwise operators
OperatorMeaningExample
&ANDx & y
|ORx | y
^XORx ^ y
~NOT~x
<<Left shiftx << 2
>>Right shiftx >> 2

Here are some examples:

Examples:

Suppose:

x = 0b1100
y = 0b1010

Bitwise operations:

# and
print(bin(x & y))		# 0b1000
# or
print(bin(x | y))		# 0b1110
# xor
print(bin(x ^ y))		# 0b0110
# not
print(bin(~x))			# -0b1101
# shift 2 bits left
print(bin(x << 2))		# 0b1100
# shift 2 bits right
print(bin(x >> 2))		# 0b0011

Operator Precedence (Order of Operations)

In Python, every operator is assigned a precedence.

Operator Precedence determines which operations are performed before which other operations.

Operators of highest precedence are performed first.

Any operators of equal precedence are performed in left-to-right order.

Operator Precedence in Python
PrecedenceOperatorDescription
lowest precedenceorBoolean OR
andBoolean AND
notBoolean NOT
==, ! =, <, <=, >, >=, is, is notcomparisons, identity
|bitwise OR
^bitwise XOR
&bitwise AND
<<, >>bit shis
+, –addition, subtraction
*, /, //, %multiplication, division, floor division, modulo
+x, -x, ~xunary positive, unary negation, bitwise negation
highest precedence**exponentiation