Monday, 23 January 2017

SPECIAL OPERATORS IN PYTHON

Python supports two types of special operators.

1. Identity Operator - Identity operators are used to compare the addresses of the memory locations which are pointed by the operands. Identity operators returns True (or) False

Operators
Meaning

is
True if the operands are identical ( refer to the same object )
is not
True if the operands are not identical ( do not refer to the same object )

Example 1 -
x1=5
y1=5
x2='hello'
y2='hello'
print 'x1 is y1 : ',x1 is y1
print 'x1 is not y1 : ',x1 is not y1
print 'x2 is y2 : ',x2 is y2

print 'x2 is not y2 : ',x2 is not y2
Output -
x1 is y1 :  True
x1 is not y1 :  False
x2 is y2 :  True

x2 is not y2 :  False


2. Membership Operator - These operators are used to search for a particular element in a string, list, tuple, set and so on. Membership operators returns True or False values.

Operators
Meaning

in
True if value / variable is found in sequence
not in
True if value / variable is not found in sequence

Example 1 -
x='hello world'
print 'h' in x
print 'h' not in x
print 'hello' in x

print 'hello' not in x
Output -
True
False
True

False

No comments:

Post a Comment