You are on page 1of 2

CHAINED COMPARISON OPERATOR

In python we can chain relational operators along with var/exp./literals. But the logical expression
implicitly implement ‘and’ operation.

Example:-
(1)
a,b,c=15,12,10
b<a>c
will return True(explanation:- b<a and b>c)
(ii)
a,b,c = 10,10,10
a==b==c
Will return True.

Conditional Operator

ReturnValueifTrue if condition else ReturnValueIfFalse

ReturnValueIfTrue will be returned if the condition returns True otherwise it will return
ReturnValueIfFalse.

It may be nested(upto 255 levels)


Example:-
a,b,c=10,15,2
a if a>b and a>c else b if b>a and b>c else c

bitwise operators

Operates on Bits.

(i) & - Bitwise and operator. This operator operates on two bits. If both bits are 1 then it returns 1
otherwise it will return 0
Example:-
a=10 # its’ binary equiv. Is 1010
a & 7 #7’s binary equiv. Is 111
It will return 2
(I will discuss later regarding number system)

1 & 0 ->0
0 & 1 ->0
0 & 0 ->0
1 & 1 ->1

(ii) | - Bitwise OR operator. This operator operates on two bits. If any one of them is 1 then it
returns 1

Ex:-
a=10
a|7
will return 15
(iii) ^ (XOR) – It will return 0 when two bits are On(1). In all othercases sames as or gate.

** XOR ->Exclusive OR gate.


Bit1 Bit2 Bit1^Bit2
1 1 0
1 0 1
0 1 1
0 0 0

Example:
15 ^ 7
will return 8

(iv) ~ (NOT GATE) inverts the bit.


Example:-
~15
will return -16

You might also like