Python编程基础:第九节 逻辑运算Logical Operators

第九节 逻辑运算Logical Operators

前言

常用的逻辑运算共分为三种:与(and)、或(or)、非(not)。与运算就是同真才真,有假则假;或运算就是有真则真,同假才假;非运算就是真变假,假变真。我们接下来举个例子加以分析。

实践

我们从键盘获取输入作为今天的温度,然后判断若今天温度满足[0, 30],则输出今天是个好天气,适合出去玩耍:

temp = int(input("What is the temperature outside?: "))
if temp>=0 and temp<=30:
    print("the temperature is good today!")
    print("go outside!")
>>> What is the temperature outside?: 15
>>> the temperature is good today!
>>> go outside!

若今天温度低于0°或高于30°我们就呆在家里:

temp = int(input("What is the temperature outside?: "))
if temp<0 or temp>30:
    print("the temperature is bad today!")
    print("stay inside!")
>>> What is the temperature outside?: -2
>>> the temperature is bad today!
>>> stay inside!

我们将上述两段代码进行合并:

temp = int(input("What is the temperature outside?: "))
if temp>=0 and temp<=30:
    print("the temperature is good today!")
    print("go outside!")
elif temp<0 or temp>30:
    print("the temperature is bad today!")
    print("stay inside!")

取反运算的使用方法如下:

if not(True):
    print("False")
else:
    print("True")
>>> True

以上便是逻辑运算的全部内容,感谢大家的收藏、点赞、评论。我们下一节将介绍while循环(While Loops),敬请期待~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值