+ 1
Please explain this code. Why output 12?
a = 3 b = 2 if 0&a: print (4) elif a | b: print (a*b*2) #output 12
5 Answers
+ 2
U don't need to seperate instructions using ';' in python
a=3
b=2
If 0&a: # it returns 0(false) so
print(4) # not executed
else:
print (a*b*2) # executes
Output is 12
#Ref
a & b == 0
a | b == 3
+ 2
Barkha Joshi
Please take note that semicolons have still uses in Python.
They still denote separations which enables you to write multiple statements on just a line.
Lets say this:
print('Statement 1'); print('Statement 2'); print('Statement 3')
Output:
Statement 1
Statement 2
Statement 3
+ 1
This could output a name error because Print with caps on P was not defined.
But if you change your code, then it may output 12.
Thanks for your question and happy coding.
+ 1
a = 3
b = 2
# 0000 (0)
# 0011 (<a> = 3)
# _____ & (bitwise AND)
# 0000 (0) -> Evaluated as boolean => False
if 0 & a:
print (4) # `if` block not executed
# 0011 (<a> = 3)
# 0010 (<b> = 2)
# _____ | (bitwise OR)
# 0011 (3) -> Evaluated as boolean = True (non zero)
elif a | b:
print (a*b*2) # `elif` block is executed