Operators in Python || Operators || Learn Python

Operators in Python

0 2226

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


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.

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

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.

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.65
if (type(z) is int):
    print ("true")
    print ("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.

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.5
if (type(z) is not int):
    print ("true")
    print ("false")


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 = True
k = False

print(j and k)
print(not k)
print(j or k)


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 = 2
s = 9
series = [1, 2, 3, 4, 5 ];
if ( r in series ):
   print("r is present in the series")
   print("r is not present in the series")
if ( s not in series ):
   print("s is not present in the series")
   print("s is present in the series")

r is present in the series
s is not present in the series

Best WordPress Hosting


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price

Leave a Reply

    Waiting for your comments