今天遇到个有意思的题目:
以下代码输入:666,会执行什么?
temp = input('请输入') if temp == 'Yes' or 'yes': print('if') else: print('else')
答案是if。
咋一看是要输入Yes或者yes才会执行if,但在python的运算中,==的优先级是高于or的,所以我们可以
这么看:if (temp == 'Yes') or ('yes') ,'yes'是非空字符,即表示布尔值True,or只需要有1真则为真,
那么if (temp == 'Yes') or ('yes') 等价于if True。所以无论输入什么都是执行if