# Operators in Python

by Monika Dadool 23-Jan-19

0 502

In python programming language, all the mathematical operations such as addition, subtraction, modulus, and division etc can be performed with the help of Arithmetic operators. Operators are also known as the constructs that can be used for modification of operand's value.

**For example**

7*5

In which * is the operator and 7 and 5 are known as operands.

**Types of Operators in Python**

* Comparison (Relational) Operators

* Assignment Operators

* Bitwise Operators

* Identity Operators

* Arithmetic Operators

* Logical Operators

* Membership Operators

**1. Arithmetic Operators**

These operators are used for performing mathematical operations.

**A simple program illustrating Arithmetic operators in python language**

# Examples of Arithmetic Operator

p = 7

q = 5

# Addition

addition = p + q

# Subtraction

subtraction = p - q

# Multiplication

multiplication = p * q

# Division(float)

division1 = p / q

# Division(floor)

division2 = p // q

# Modulo

modulus = p % q

# print all the results

print(addition)

print(subtraction)

print(multiplication)

print(division1)

print(division2)

print(modulus)**Output**

12

2

35

1.4

1

2

**2. Assignment Operators**

There are (=) assignment operator and "(==) equal to" operator that are found in python language in which assignment operators (=) are used to assigns the right side value to the variable of left side whereas (==) is known as the binary operators working on two operands.

**Example**

p = 34

q = 56

r = 0

r = p + q

print " Value of r is ", r

r += p

print "Value of r is ", r

r *= p

print " Value of r is ", r

r /= p

print "Value of r is ", r

r = 5

r %= p

print "Value of r is ", r

r **= p

print "Value of r is ", r

r //= p

print "Value of r is ", r

**Output**

Value of r is 90

Value of r is 124

Value of r is 4216

Value of r is 124

Value of r is 5

Value of r is 582076609134674072265625

Value of r is 17119900268666884478400

**3. Identity Operators**

The memory locations of the two objects can be compared with the help of Identity operators and There are two types of identity operators that are available in python i.e. is and is not operators.**Is Operators**

If the variables on either side of the Operator, point to the similar object, evaluates to true otherwise it is false. **Example**

r is s, here is results in 1 if id(r) equals id(s).

**# A simple python program to illustrate 'is' identity operator**

*z = 34.65if (type(z) is int): print ("true") else: print ("false")*

**Output**

false**Is not Operator**

If the variables on either side of the operator point to the similar object, it evaluates to false and otherwise it evaluates to true.**Example**

r is not s; here is not results in 1 if id(r) is not equal to id(s).

# Python program to illustrate the 'is not' identity operator

*z = 67.5if (type(z) is not int): print ("true") else: print ("false")*

**Output**

true

**4. Logical Operators**

Logical operators are used to perform all logical operations such as logical AND, OR and NOT operations.

Logical AND: it returns true if both operands are true

Logical OR: it returns true if any of the two operands is true

Logical NOT: It return true if the operand is false

**A simple example illustration logical operator in python language**

*j = Truek = Falseprint(j and k) print(not k) print(j or k) *

**Output**

False

True

True

**5. Membership Operators**

Membership operators in python language are used to verify the membership of the value or in other words, we can say it is used to identify the presence of a value in the specified sequence. There is two type of membership operator that are available in python language i.e. in and in not.**A simple program illustrating membership operators i.e. in and in not**

*r = 2s = 9series = [1, 2, 3, 4, 5 ];if ( r in series ): print("r is present in the series")else: print("r is not present in the series")if ( s not in series ): print("s is not present in the series")else: print("s is present in the series")*

**Output**

r is present in the series

s is not present in the series

Share:

## Comments

Waiting for your comments