Python – Operators

In Python, operators are symbols and used to perform a specific operation on given values. Python has a rich collection of operators, we will learn here.

Types of Operators in Python

  1. Arithmetic Operators
  2. Assignment Operators
  3. Relational Operators
  4. Logical Operators
  5. Bitwise Operators
  6. Membership Operators
  7. Identity Operators

Arithmetic Operators (Mathematical Operators)

Arithmetic or Mathematical operators in Python are used to perform mathematical tasks.

OperatorDescription
+For Addition
For Subtraction
*Multiplication
/Divison
//Floor Divison
%Remainder of Division (modulus)
**Exponent

Example 1: Arithmetic operators in Python.

#Example of Arithmetic operators in Python
a=25
b=3
c=a+b
print("Addition =",c)
c=a-b
print("Subtract =",c)
c=a*b
print("Multiply =",c)
c=a/b
print("Divison =",c)
c=a//b
print("Floor Divison =",c)
c=a%b
print("Remainder =",c)
c=a**b
print("Power =",c)

Assignment Operators

Assign a value to a variable, the assignment operator is used.

OperatorExample
= a=5, 5 assign to variable a
+= a=a+b can also write as a+=b
-= a=a-b can also write as a-=b
*= a=a*b can also write as a*=b
/= a=a/b can also write as a/=b
//= a=a//b can also write as a//=b
%= a=a%b can also write as a%=b
**= a=a**b can also write as a**=b

Relational Operator (Comparison Operator)

A relational operator compares or check the relation of two operands and return Boolean value as a result.

OperatorDescription
< Less then
> Greater then
<= Less then or Equal
>= Greater then or equal
==Equal to
!=Not equal to

Example 2: Relational operators in Python

#Example of Relational operators in Python
a=25
b=25
print(a<b)
print(a>b)
print(a<=b)
print(a>=b)
print(a==b)
print(a!=b)

Logical Operators

Logical operators in Python are used to combine relational expression and return boolean value as a result.

Operator Name Description
and Logical ANDReturn TRUE if all expression is true, otherwise FALSE.
or Logical ORReturn TRUE if all or any one expression is true, otherwise FALSE.
not Logical NOTReverse the state of the expression(True expression to False and False to True).

Example 3: Logical operators in Python

#Example of Logical operators in Python
a=10
print(a>0 and a<100)
print(a>0 and a>100)
print(a>0 or a<100)
print(a>0 or a>100)
print(a>0 and a<100)
print(not a>0)

Bitwise Operators in Python

Bitwise operators are used to working with binary numbers. Decimal numbers are converted into binary first, then bitwise operator works on each bit

Operators Description
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
~ Bitwise Complement
<< Left Shift
>> Right Shift

Example 4: Bitwise operators in Python

#Example of Bitwise operators in Python
a=11
b=3
print("Bitwise And",a & b)
# 00001011 & 00000011, result 00000011 ( 3 as Decimal) 
print("Bitwise Or",a | b)
# 00001011 | 00000011, result 00001011 ( 11 as Decimal)
print("Bitwise XOR",a ^ b)
# 00001011 ^ 00000011, result 00001000 ( 8 as Decimal)
print("Bitwise Complement",~a)
# 2's Complement
print("Left Shift",a << b)
# 00001011 << 3, result 01011000 ( 88 as Decimal), remove 3 digit from left and add three 0 to right
print("Right Shift",a >> b)
# 00001011 >> 3, result 00000001 ( 1 as Decimal), remove 3 digit from right and add three 0 to left

Membership Operators

Membership Operators in Python is used test value is in object or not. It returns a boolean value as a result.

Operators Description
in Returns True, if the value or variable is present in sequence.
not in Returns True, if the value or variable is not present in sequence.

Example 5: Membership operators in Python

#Example of Membership operators in Python
n=[11,22,33,44]
print(11 in n)
print(11 not in n)

Identity Operators

Identity operators in Python are used to compare the objects and return boolean value as a result.

Operators Description
is Returns True, if both the value or variable is the same object.
is not Returns True, if both the value or variable is not the same object.

Example 6: Identity operators in Python

#Example of Identity operators in Python
n=10
print(n is 10)
print(n is not 10)

31
Created on By S. Shekhar

Python - Quiz 6

1 / 4

Which of the following is true?

2 / 4

Which of the following is not an assignment operator?

3 / 4

Which of the following returns True, if both the value or variable is the same object?

4 / 4

What will be the output of
n=10
print(n>0 and n<100)

Your score is

The average score is 73%

0%