Python —— 比较运算符 + 逻辑运算符

Python —— 比较运算符 + 逻辑运算符

比较运算符

逻辑运算符

逻辑运算符:& ^ or and not

 and + or 

仅数值:

  1 and 3  ==> 3

  0 and 3  ==> 0

  3 and 0  ==> 0

  1 or 3  ==> 1

  0 or 3  ==> 3

布偶值:

  1 and True  ==> True

  True and 1  ==> 1,第一个值为True,但仍需判断and后的值,返回第二个值

  0 and True  ==> 0,0已经是False,不需判断and后的值

  True and 0  ==> 0,需要判断and后的值,并返回

  1 or True  ==> 1,已判断1为True,不需判断or后的值,直接返回

  0 or True  ==> True,

  0 or False  ==> False,

  True or 1  ==> True,已判断第一个值为True,不需判断or后的值,直接返回

  False or 1  ==> 1,已判断第一个值为False,需判断or后的值,直接返回

 

练习:

2 & 5  ==> 0

2 ^ 5  ==> 7

1 or 3  ==> 1

1 and 3  ==> 3

0 and 2 and 1  ==> 0

0 and 2 or 1  ==> 1

0 and 2 or 1 or 4  ==> 1

0 or False and 1  ==> False

 

not

not False  ==> True

not True  ==> False

not 1  ==> False

not 0  ==> True

posted @ 2017-08-17 19:50  乖巧Clare  阅读(331)  评论(0编辑  收藏  举报