Python逻辑运算

python逻辑运算

逻辑运算符号

  • 布尔运算
  • 优先级:not > and > or
  • 优先级高的会被先求值。
  • 可以通过括号改变运算顺序,和数学运算上的用法一致。
  • not (x > 5 and (x < 10 or x == 12))

and

  • 与(且),x > 5 and x < 10
  • 可以用and连接两个或两个以上的操作对象。
  • 只有所有的连接对象都为True,结果才会返回True。
  • 只要有一个连接对象为False,结果就会返回False。
  • 悲观主义者,只要一件事不开心,就会不开心。

or

  • 或,x > 5 or x < 10
  • 可以用or连接两个或两个以上的操作对象。
  • 只要有一个或以上的连接对象为True,结果就会返回True。
  • 只有所有的连接对象都为False,结果才会返回False。
  • 乐观主义者,只要一件事开心,就会开心。

not

  • 非,not x > 5
  • 只能用于对一个操作对象进行运算。
  • 把原先的布尔值反过来。
  • 如果原先操作对象为True,则会返回False。
  • 如果原先操作对象为False,则会返回True。

练习

house_work_count = int(input("做了多少次家务?:"))
red_envelop_count = int(input("发了多少次红包?:"))
shopping_count = int(input("逛了多少次街?:"))
has_been_angry = int(input("生气了吗?(1生气了,0没生气):"))
has_been_angry == 1
if (house_work_count > 10 and red_envelop_count > 1 and shopping_count > 4 and not has_been_angry):
    print("等待switch!")
else:
    print("switch随风散去。。。")
posted @ 2024-06-05 23:52  花鸿渐  阅读(3)  评论(0编辑  收藏  举报