What is different in | and OR operators in Python?


In Python or is a logical operator and | is a bitwise operator. The or operator requires two opeans of any type and may be true or false. It returns true if any one operand evaluates to true.

>>> a=50
>>> b=25
>>> a>40 or b>40
True
>>> a>100 or b<50
True
>>> a==0 or b==0
False
>>> a>0 or b>0
True

The | operator takes bits as operands and returns 1 if any one operand is 1

>>> a=10   #0000 1010
>>> bin(a)
'0b1010'
>>> b=20   #0001 0100
>>> bin(b)
'0b10100'
>>> c=a|b
>>> c
30         #0001 1110
>>> bin(c)
'0b11110'

Updated on: 26-Feb-2020

326 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements