Special operators:
1.
Identity operators.
2.
Membership operators.
Identity operators
Identity operators
are (is, is not).
r1 is r2 = True
If it is
show True then both ids pointing to the same address (ids).
r1 is not r2 = False
If it is
show False then both ids not pointing to the same address (ids).
Ex:
a = 10
b = 10
l1 = (a is b
)
l2 = (a is not b)
print (l1,l2)
True False # Result
Just remember
all fundamental data types are immutable. (Fundamental data types are int,
float, complex, bool,str) and object reusable concepts all are accepted. Except for complex
data type. but remaining data types reusable not accepted.
Ex:
a= 10
b = 10
print(id(a),id(b))
list_1 = [10,20,30,40]
list_2 = [10,20,30,40]
print(id(list_1),id(list_2))
# Result
1761978432, 1761978432
24768200, 24888680
Now apply ‘is’ and ‘==’.
a= 10
b = 10
print(a is b)
print(a is not b)
list_1 = [10,20,30,40]
list_2 = [10,20,30,40]
print(list_1 is list_2)
print(list_1 is not list_2)
# Result
True # ‘is’ operator meant for reference
comparison. That’s why it is true.
False
False
True # ‘is not’ operator meant for content
comparison. That’s why it is true.
Membership operators.
Membership operators
are (in, not in).
If our requirement object is there in the sequence of variable
data, then it’s true otherwise it’s false.
Ex:
word =
'smart program'
print('a' in
word)
print('a'
not in word)
print('pro'
in word)
Result
True
False
True
Operator Precedence
If multiple
operators are there, in which order, these operators are executed. In this sequence
called operator precedence.
Ex:
print
(4+2*3)
print((4+2)*3)
Result:
10
18
In first
case ‘*’ is the first preference. So result look like
(4+2*3)
(4+6)
(10)
In second
case () is the first preference. So result look like
((4+2)*3)
(6*3)
(18)
The operator
precedences are listed below.
(upper group
has higher precedence than the lower ones).
Operators |
Meaning |
() |
Parentheses |
** |
Exponent |
+x, -x, ~x |
Unary plus, Unary minus, Bitwise NOT |
*, /, //, % |
Multiplication, Division, Floor division, Modulus |
+, - |
Addition, Subtraction |
<<, >> |
Bitwise shift operators |
& |
Bitwise AND |
^ |
Bitwise XOR |
| |
Bitwise OR |
==, !=, >, >=, <, <=, |
Comparisons operators |
=,+=,-=,*= |
Relation operators |
is, is not |
Identity operators |
in, not in |
Membership operators |
not |
Logical NOT |
and |
Logical AND |
or |
Logical OR |
Post a Comment