Python中&、^与and、or

导火索:给定两个列表,怎么找出他们相同的元素和不通的元素?

list1 = [1, 2, 3, 4, 57, 8, 90]  list2 = [2, 3, 4, 5, 6, 7, 8]  lis = list1 + list2  print('&的结果:', set(list1) & set(list2))  print('^的结果:', set(list1) ^ set(list2))  print('and的结果:', set(list1) and set(list2))  print('or的结果:', set(list1) or set(list2))

结果:

&的结果: {8, 2, 3, 4}  ^的结果: {1, 5, 6, 7, 57, 90}  and的结果: {2, 3, 4, 5, 6, 7, 8}  or的结果: {1, 2, 3, 4, 8, 57, 90}

总结:

  & 不等于and,|不等于or

  &、^代表的是位运算符,andor代表的是逻辑运算符

&的源码:

def __and__(self, *args, **kwargs): # real signature unknown      """ Return self&value. """      pass