boolean-expression

'and' (boolean) vs '&' (bitwise) - Why difference in behavior with lists vs numpy arrays?

我与影子孤独终老i 提交于 2019-11-26 03:15:43
问题 What explains the difference in behavior of boolean and bitwise operations on lists vs NumPy arrays? I\'m confused about the appropriate use of & vs and in Python, illustrated in the following examples. mylist1 = [True, True, True, False, True] mylist2 = [False, True, False, True, False] >>> len(mylist1) == len(mylist2) True # ---- Example 1 ---- >>> mylist1 and mylist2 [False, True, False, True, False] # I would have expected [False, True, False, False, False] # ---- Example 2 ---- >>>

Why does `a == b or c or d` always evaluate to True?

ⅰ亾dé卋堺 提交于 2019-11-25 21:34:27
问题 I am writing a security system that denies access to unauthorized users. import sys print(\"Hello. Please enter your name:\") name = sys.stdin.readline().strip() if name == \"Kevin\" or \"Jon\" or \"Inbar\": print(\"Access granted.\") else: print(\"Access denied.\") It grants access to authorized users as expected, but it also lets in unauthorized users! Hello. Please enter your name: Bob Access granted. Why does this occur? I\'ve plainly stated to only grant access when name equals Kevin,