Python Operators

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
Run Code

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.

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
Run Code

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.

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))
Run Code

Output

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

The following tutorials cover Logical Operators in detail.

Identity Operators

The following tutorials cover Identity Operators in detail.

Membership Operators

In the following program, we take a value in a, and a list in b; and check if a is member of bor 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
Run Code

Output

a in b     : True
a not in b : False

The following tutorials cover Membership Operators in detail.

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
Run Code

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.

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us