Skip to content

Operators


Python Operators: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise

Section titled “Python Operators: Arithmetic, Assignment, Comparison, Logical, Identity, Membership, Bitwise”

Operators are special symbols that perform some operation on operands and returns the result. For example, 5 + 6 is an expression where + is an operator that performs arithmetic add operation on numeric left operand 5 and the right side operand 6 and returns a sum of two operands as a result. 5 + 6``+``5``6 Python includes the operator module that includes underlying methods for each operator. For example, the + operator calls the operator.add(a,b) method. operator+``operator.add(a,b)

import operator
n=5+5
print(n)
n=operator.add(5, 10)
print(n)
n=operator.__add__(5, 20)
print(n)

`import operator

n=5+5
print(n)

n=operator.add(5, 10) print(n)

n=operator.add(5, 20) print(n)[Try it](/codeeditor?cid=python-3z7uz4reb) Above, expression 5 + 6 is equivalent to the expression operator.add(5, 6) and operator.__add__(5, 6). Many function names are those used for special methods, without the double underscores (dunder methods). For backward compatibility, many of these have functions with the double underscores kept. 5 + 6operator.add(5, 6)operator.add(5, 6)` Python includes the following categories of operators:

Arithmetic operators perform the common mathematical operation on the numeric operands.

The arithmetic operators return the type of result depends on the type of operands, as below.

  1. If either operand is a complex number, the result is converted to complex;
  2. If either operand is a floating point number, the result is converted to floating point;
  3. If both operands are integers, then the result is an integer and no conversion is needed.

The following table lists all the arithmetic operators in Python:

x,y= 5,6 print(x + y) #output: 11 import operator operator.add(5,6) #output: 11
x,y =5,6 print(x - y) #output: -1 import operator operator.sub(10, 5) #output: 5
x,y =5,6 print(x * y) #output: 30 import operator operator.mul(5,6) #output: 30
x = 2; y = 3 print(x ** y) #output: 8 import operator operator.pow(2, 3) #output: 8
x = 6; y = 3 print(x / y) #output: 2 import operator operator.truediv(6, 3) #output: 2

math.floor(a/b)

x = 6; y = 5 print(x // y) #output: 1 import operator operator.floordiv(6,5) #output: 1

a/b

x = 11; y = 3 print(x % y) #output: 12 import operator operator.mod(11, 3) #output: 2

The assignment operators are used to assign values to variables. The following table lists all the arithmetic operators in Python:

x = 5; x 5
x = 5 print(x += 5) #output: 10 import operator x = operator.iadd(5, 5) #output: 10
x = 5 print(x -= 2) #output: 3 import operator x = operator.isub(5,2)
x = 2 print(x *= 3) #output: 6 import operator x = operator.imul(2, 3)
x = 6 print(x /= 3) #output: 2 import operator x = operator.itruediv(6, 3)
x = 6 print(x //= 5) #output: 1 import operator operator.ifloordiv(6,5)
x = 11 print(x %= 3) #output: 2 import operator operator.imod(11, 3) #output: 2
x = 11 print(x &= 3) #output: 1 import operator operator.iand(11, 3) #output: 1
x = 3 print(x |= 4) #output: 7 import operator operator.mod(3, 4) #output: 7
x = 5 print(x ^= 2) #output: 7 import operator operator.ixor(5, 2) #output: 7
x = 5 print(x >>= 2) #output: 1 import operator operator.irshift(5, 2) #output: 1
x = 5 print(x <<= 2) #output: 20 import operator operator.ilshift(5, 2) #output: 20

The comparison operators compare two operands and return a boolean either True or False. The following table lists comparison operators in Python.

x,y =5,6 print(x > y) #output: False import operator operator.gt(5,6) #output: False
x,y =5,6 print(x < y) #output: True import operator operator.add(5,6) #output: True
x,y =5,6 print(x == y) #output: False import operator operator.eq(5,6) #output: False
x,y =5,6 print(x != y) #output: True import operator operator.ne(5,6) #output: True
x,y =5,6 print(x >= y) #output: False import operator operator.ge(5,6) #output: False
x,y =5,6 print(x <= y) #output: True import operator operator.le(5,6) #output: True

The logical operators are used to combine two boolean expressions. The logical operations are generally applicable to all objects, and support truth tests, identity tests, and boolean operations.

x,y =5,6 print(x > 1 and y <10) #output: True
x,y =5,6 print(x > 6 or y <10) #output: True
x = 5 print(not x > 1) #output: False

The identity operators check whether the two objects have the same id value e.i. both the objects point to the same memory location.

x,y =5,6 print(x is y) #output: False import operator operator.is_(x,y) #output: False
x,y =5,6 print(x is not y) #output: True import operator operator.is_not(x, y) #output: True

The membership test operators in and not in test whether the sequence has a given item or not. For the string and bytes types, x in y is True if and only if x is a substring of y. in``not in``x in y``x``y

nums = [1,2,3,4,5] print(1 in nums) #output: True print(10 in nums) #output: False print('str' in 'string') #output: True import operator operator.contains(nums, 2) #output: True
nums = [1,2,3,4,5] print(1 not in nums) #output: False print(10 not in nums) #output: True print('str' not in 'string') #output: False import operator not operator.contains(nums, 2) #output: False

Bitwise operators perform operations on binary operands.

x=5; y=10 z=x & y print(z) #output: 0 import operator operator.and_(x, y)
x=5; y=10 z=x | y print(z) #output: 15 import operator operator.or_(x, y)
x=5; y=10 z=x ^ y print(z) #output: 15 import operator operator.xor(x, y)
x=5 print(~x) #output: -6 import operator operator.invert(x)
x=5 print(x<<2) #output: 20 import operator operator.lshift(x,2)
x=5 print(x>>2) #output: 1 import operator operator.rshift(x,2)