Python Operators


Operators can be applied to variables and values. In Python, operators are keywords or special characters. The value on which these operators work are called operands.

Operators you already know are the ones you learned in math class like addition (+) and substraction(-). But there are many other operators.

Related course: Complete Python Programming Course & Exercises

Types of Python Operators

Python has these types of operators:

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

Arithmetic Operators

These operators can be applied to numbers. They are the typical ones you are already familiar with: addition (+), substraction (-), multiplication (*) etc.

  • + : addition operator
  • - : subtraction operator
  • * : multiplication operator
  • / : division operator
  • ** : exponential operator
  • // : floor division operator

The program below demonstrates the use of arithmetic operators:

>>> x = 3
>>> y = 7
>>> 
>>> sum = x + y
>>> print("x plus y is", sum)
x plus y is 10
>>> 
>>> sub = x - y
>>> print("x minus y is", sub)
x minus y is -4
>>> 
>>> mult = x * y
>>> print("x times y is", mult)
x times y is 21
>>> 
>>> divs = x / y
>>> print("division result ", divs)
division result  0.42857142857142855
>>> 
>>> modulus = x % y
>>> print("modulus is", modulus)
modulus is 3
>>> 
>>> exp = x ** 2
>>> print("exponent is", exp)
exponent is 9
>>> 
>>> floor_division = x // y
>>> print("division is", floor_division)
division is 0

python arithmetic operators

You can use the addition operator on strings too:

>>> print("hello " + "world")
hello world

Comparison operators

You can use comparison operators if you want to compare data. The output is a boolean value: True or False.

The list of comparison operators in Python is:

  • == : returns True if both the values are equal.
  • != : returns True if both the operands are not equal.
  • > : returns True if the left operand is greater than the right operand.
  • < : returns True if the left operand is less than the right operand.
  • >= : returns True if the left value is greater than or equal to the right value.
  • <= : returns True if the left value is less than or equal to the right value.

Take a look at this example:

>>> x = 3
>>> y = 6
>>> x > y
False
>>> x < y
True
>>> x == y
False
>>> x != y
True

python comparison operators

Bitwise operators

Bits are how computers are working low-level. A bit can be 0 (False) or 1 (True).

The bit operator allows us to manipulate a single "bit", i.e. a binary bit, of an integer-type value of a basic data type.

The bit operator performs Boolean algebraic operations on the bits corresponding to the two parameters, and produces a result.

Python has 6 bitwise operators:

  • & : Bitwise AND operator
  • | : Bitwise OR operator
  • ^ : Bitwise XOR operator
  • ~ : Binary ones’ complement operator
  • << : Binary left shift operator
  • >> : Binary right shift operator

An example of using bitwise operators:

a = 60           # 60 = 0011 1100
b = 13           # 13 = 0000 1101

print(a & b)     # 0000 1100 = 12
print(a | b)     # 0011 1101 = 61
print(a ^ b)     # 0011 0001 = 49
print(~ a)       # 1100 0011 = -61
print(a << 2)    # 1111 0000 = 240
print(a >> 2)    # 0000 1111 = 15

The bit operator comes from the C language's low-level operations, when we often need to manipulate the hardware directly. With Python, it's likely you barely use bit operators.

Python Logical Operators

The common logical operators in computer science (and Python) are:

  • AND: both input A and B must be True, for True as output
  • OR: either input A or B must be True, for True as output
  • NOT: logical NOT operator

You can do this:

A = False
B = True

print(A and B)
print(A or B)
print(A and A)
print(B and B)

python logical operators

Assignment operators

To change a variables value, you can use an assignment operator. The variable to be changed is written on the left of the assignment operator.

  • = : simple assignment operator
  • += : adds the two operands and then assign the value to the right operand
  • -= : subtracts the right operand from the left and then assign the value to the left operand
  • *= : multiplies both the operands and then assign to the left
  • /= : divides left operand from the right operand and then assign the value to the left operand
  • %= : modulus of left and right operands and then assigned to the left operand
  • **= : exponential of left to the right operands and then assignment to the left operand
  • //= : floor division of the left and right operands and then the value is assigned to the left operand

Example:

a = 8 # assignment

a += 1 # equals a = a + 1
a -= 1 # equals a = a - 1
a *= 1 # equals a = a * 1
a /= 1 # equals a = a / 1
a %= 1 # equals a = a % 1

a = 10
a /= 2
print(a)

python assignment operator

Membership Operators

You can use membership operators to check the existence of a value in a sequence.

  • in searches for value in sequence
  • not checks if value is not in sequence

python membership operators

Identity operators

An identity operator verifies that two variables point to the same location in memory.

  • is returns True if its the same memory location
  • is not returns True if they are stored in a different memory location

Like this example:

x = 1
y = 2

print(x is y)
print(x is not y)

Keep in mind that it doesn't compare value, but memory location.

python identity operator

python operator module

The operator module is a built-in operator-function interface in python that defines some functions for arithmetic and comparison of built-in operations.

The operator module is implemented in c, so execution is faster than python code.

An example of using the operator module:

>>> import operator
>>> 
>>> a = 5
>>> b = 6
>>> 
>>> operator.lt(a,b)
True
>>> operator.le(a,b)
True
>>> operator.eq(a,b)
False
>>> operator.eq(a,a)
True
>>> operator.ne(a,b)
True
>>> operator.gt(a,b)
False
>>> operator.ge(a,b)
False

These functions can be used to replace the previous operators,

Let's start with a brief description of what these functions mean.

  • lt(a, b) is equivalent to a < b
  • le(a,b) is equivalent to a <= b
  • eq(a,b) is equal to a == b
  • ne(a,b) is equivalent to a ! = b
  • gt(a,b) equals a > b
  • ge(a, b) is equivalent to a>= b

python operator module