Operators in Python


Python Operators

In Python, operators are used to perform operations on given values. The operations could be arithmetic, logical, etc. Based on the operations, the input values can be numeric, string, boolean, etc.

For example, Arithmetic Addition operator takes two numeric values as operands, performs addition operation, and returns their sum.

In this tutorial, we will go through all of the operators available in Python, with examples, and references to the individual tutorials for each of the operators.

Arithmetic Operators

In the following program, we take two numbers: a, and b; and perform arithmetic operations on these numbers.

Python Program

a = 5
b = 2

print('a + b  :', (a + b))  #Arithmetic Addition
print('a - b  :', (a - b))  #Arithmetic Subtraction
print('a * b  :', (a * b))  #Arithmetic Multiplication
print('a / b  :', (a / b))  #Arithmetic Division
print('a % b  :', (a % b))  #Arithmetic Modular Division
print('a // b :', (a // b)) #Arithmetic Floor Division
print('a ** b :', (a ** b)) #Arithmetic Exponentiation

Output

a + b  : 7
a - b  : 3
a * b  : 10
a / b  : 2.5
a % b  : 1
a // b : 2
a ** b : 25

The following tutorials cover Arithmetic Operators in detail.

  • AdditionPython Tutorial for Arithmetic Addition operator, with syntax and examples.
  • SubtractionPython Tutorial for Arithmetic Subtraction operator, with syntax and examples.
  • MultiplicationPython Tutorial for Arithmetic Multiplication operator, with syntax and examples.
  • DivisionPython Tutorial for Arithmetic Division operator, with syntax and examples.
  • Modular DivisionPython Tutorial for Arithmetic Modular Division operator, with syntax and examples.
  • ExponentiationPython Tutorial for Arithmetic Exponentiation operator, with syntax and examples.
  • Floor DivisionPython Tutorial for Arithmetic Floor-Division operator, with syntax and examples.

Comparison Operators

In the following program, we take two numbers: a, and b; and perform comparison operations on these numbers.

Python Program

a = 5
b = 2

print('a == b :', (a == b))  #Comparison Equal to
print('a != b :', (a != b))  #Comparison Not Equal to
print('a > b  :', (a > b))   #Comparison Greater than
print('a < b  :', (a < b))   #Comparison Less than
print('a >= b :', (a >= b))  #Comparison Greater than or Equal to
print('a <= b :', (a <= b))  #Comparison Less than or Equal to

Output

a == b : False
a != b : True
a > b  : True
a < b  : False
a >= b : True
a <= b : False

The following tutorials cover Comparison/Relational Operators in detail.

  • EqualPython Tutorial for Comparison Equal operator, with syntax and examples.
  • Not equalPython Tutorial for Comparison Not-Equal operator, with syntax and examples.
  • Greater thanPython Tutorial for Comparison Greater-than operator, with syntax and examples.
  • Less thanPython Tutorial for Comparison Less-than operator, with syntax and examples.
  • Greater than or equal toPython Tutorial for Comparison Greater-than or Equal-to operator, with syntax and examples.
  • Less than or equal toPython Tutorial for Comparison Less-than or Equal-to operator, with syntax and examples.

Logical Operators

In the following program, we take boolean values in a, and b; and perform logical operations on these values.

Python Program

a = True
b = False

print('a and b :', (a and b))
print('a or  b :', (a or b))
print('  not a :', (not a))

Output

a and b : False
a or  b : True
  not a : False

The following tutorials cover Logical Operators in detail.

  • andPython Tutorial for Logical and operator, with syntax and examples.
  • orPython Tutorial for Logical or operator, with syntax and examples.
  • notPython Tutorial for Logical not operator, with syntax and examples.

Identity Operators

The following tutorials cover Identity Operators in detail.

  • isPython Tutorial for Identity is operator, with syntax and examples.
  • is notPython Tutorial for Identity is not operator, with syntax and examples.

Membership Operators

In the following program, we take a value in a, and a list in b; and check if a is member of b or not using membership operators.

Python Program

a = 'apple'
b = ['apple', 'banana', 'cherry']

print('a in b     :', (a in b))      #membership in
print('a not in b :', (a not in b))  #membership not in

Output

a in b     : True
a not in b : False

The following tutorials cover Membership Operators in detail.

  • inPython Tutorial for Membership in operator, with syntax and examples.
  • not inPython Tutorial for Membership not in operator, with syntax and examples.

Bitwise Operators

In the following program, we take boolean values in a, and b; and perform logical operations on these values.

Python Program

a = 5
b = 2

print('a & b  :', (a & b))   #bitwise AND
print('a | b  :', (a | b))   #bitwise OR
print('a ^ b  :', (a ^ b))   #bitwise XOR
print('  ~ b  :', (~ b))     #bitwise NOT
print('a << b :', (a << b))  #bitwise Left Shift
print('a >> b :', (a >> b))  #bitwise Right Shift

Output

a & b  : 0
a | b  : 7
a ^ b  : 7
  ~ b  : -3
a << b : 20
a >> b : 1

The following tutorials cover Bitwise Operators in detail.

  • Bitwise ANDPython Tutorial for Bitwise AND operator, with syntax and examples.
  • Bitwise ORPython Tutorial for Bitwise OR operator, with syntax and examples.
  • Bitwise XORPython Tutorial for Bitwise XOR operator, with syntax and examples.
  • Bitwise NOTPython Tutorial for Bitwise NOT operator, with syntax and examples.
  • Zero Fill Left ShiftPython Tutorial for Bitwise Left Shift operator, with syntax and examples.
  • Signed Right ShiftPython Tutorial for Bitwise Right Shift operator, with syntax and examples.