0

recently I studied a lot of information and improvement from leetcode

Usually, I prefer & | operator than and or operator, because of short typo (not really big difference though).

But some question of leetcode using logical operator and solve the question really suprisingly method.

For example,

s = []
print(s and s[0]) # it returns []
print(s[0] and s) # it returns IndexError: list index out of range of course..

also I tested somethings with integer and empty list like above,

a = 1
print(s and a) # it returns []
print(a and s) # it returns [] without any errors

For this reason, I think my code written methods when using condition (i.e. if methods) with bitwise operator rather than logical operators.

I want to know,

  1. Why above example works and how it works different results by order (a and b // b and a)
  2. Is it problem using bitwise operator than logical operator with some conditions or handling the data (include bolean)?
  3. If is there any rules or important things must I know, please let me know.

Thanks for your help even with the basic knowledge of Python.

Gangil Seo
  • 256
  • 1
  • 3
  • 16

0 Answers0