Logical Operators

Using Logical Operator in Python

·         And Operator
·         Or Operator
·         Not Operator
·         In & Not in
·         Is & is not
·         All and Any

Using ‘and’ operator. Equivalent of  && is ‘and’. & is used for bit wise operator.
self = 1
arr = {1,2,3,4,5,6}

if ( self != 1) and ( self != 2)  : print self, 'is not 1 and 2'
if ( self == 1) or  ( self == 2)  : print self, 'is Either 1 or 2'
else: print "not present"

if ( self in arr) : print self,'self is in arr'
if ( self not in arr)  : print  self, 'self is not in arr'

if ( self is not 2) : print self,'Yes self is not 2'
if ( self is 1 ) : print  self, 'yes self is 1.'

Using not in with if
if  0 not in (1,2) : print “zero present”


Using all and any
cond1 =2
cond2= 7
cond3=10
if  all( [ cond1 == 2, cond2 ==3, cond3 ==10] ):   print 'valid-All'
else:print 'invalid-All'

if  any( [ cond1 == 2 ,cond2 ==3, cond3 ==10] ): print 'valid-Any'
else:print 'invalid-Any'

   

Result

No comments:

Post a Comment