CC++ & Algorithm

Python逻辑运算

中等0
语言版本:C++
概述:用“冰激凌和巧克力”的比喻,教会你and、or、not三种逻辑运算,并学会用它们做判断。

Python 里的逻辑运算:and、or、not 让判断更聪明

写程序的时候,常常要根据好几个条件一起做决定。比如:“如果今天是晴天 而且 温度大于20度,我就去公园玩。” 这里的“而且”就是逻辑上的“与”操作。Python 里用三个好玩的词来表示逻辑运算:and(并且)、or``(或者)、**not`(不)。它们就像三个小裁判,帮你把多个条件组合在一起,得出一个最终的“真”或“假”。

一、and——两个都要满足,才算真

and 的意思是:左右两边的条件都得是 True,结果才是 True。只要有一个是 False,结果就是 False

还记得吃冰激凌的例子吗?妈妈定下规矩:“写完作业 并且 洗完手” 才能吃。如果你只做完作业但没洗手,对不起,不能吃。只有两个全做到,才能美滋滋地吃。

homework_done = True   # 作业写完了吗?True 表示是
hands_washed = False    # 洗手了吗?False 表示没有
can_eat = homework_done and hands_washed   # 两个都要 True
print(can_eat)   # 输出 False,因为没洗手

再举一个例子:体育老师说要 “身高超过120cm 并且 体重不超过50kg” 才能玩高空滑索。如果小明身高130cm(合格),但体重55kg(超标),结果就是 False

height_over_120 = True   # 身高超过120cm
weight_under_50 = False  # 体重不超过50kg
can_play = height_over_120 and weight_under_50
print(can_play)   # 输出 False

二、or——只要一个满足,就算真

or 的意思是:左右两边的条件只要有一个是 True,结果就是 True。只有两个都是 False 时,结果才是 False

妈妈讲:“喝完牛奶 或者 吃完蔬菜” 就能吃巧克力。你只需要完成其中一件,就可以拿到巧克力啦!要是两件都没做,那就只能看着巧克力流口水了。

drank_milk = True    # 喝了牛奶
ate_veggie = False   # 没吃蔬菜
get_choco = drank_milk or ate_veggie   # 一个 True 就够了
print(get_choco)    # 输出 True

生活中还有这样的场景:学校规定 “考试及格 或者 平时作业全交” 就可以参加课外活动。只要满足一个条件就行。如果小明考试没及格(False),但他作业每次都交(True),那他还是能去参加。如果两样都不行,那就不能去。

pass_exam = False        # 考试没及格
homework_done_all = True # 作业全部交了
can_join = pass_exam or homework_done_all
print(can_join)          # 输出 True

三、not——颠倒黑白,反转真假

not 就是个反转开关:把 True 变成 False,把 False 变成 True

比如:你没有被老师点名回答问题,才能出去玩。named = False(没被点名),not named 就变成了 True,所以可以出去玩。

named = False               # 没有被点名
can_play = not named        # not False 变成 True
print(can_play)             # 输出 True

再比如,学校说“不是下雨天才能上体育课”。如果 rainTrue(下雨),not rain 就是 False,体育课泡汤了。

rain = True                 # 下雨了
has_gym = not rain          # 取反
print(has_gym)              # 输出 False,没体育课

not 经常用在判断“不符合条件”的情况。比如:“如果变量 is_holiday 不是 True,就表示还要上学。”

四、优先级:谁先算?用括号最安全

andornot 一起出现时,Python 有自己的计算顺序:

  • not 最优先(先算否定)
  • and 其次
  • or 最后

但记住这个顺序有点麻烦,而且容易搞错。最好的办法就是——加括号!括号能让你清楚地表达你想先算什么,就像数学课上的 (2+3)*4 一样。

# 不加括号的写法(容易误会)
a = True
b = False
c = True
result = a or b and c   # 到底是 (a or b) and c 还是 a or (b and c)?
# 按照优先级,and 比 or 高,所以相当于 a or (b and c)
print(result)  # 输出 True(因为 a 是 True,or 已经得到 True)

# 加上括号,意思一目了然
result2 = (a or b) and c   # (True or False) and True → True and True → True
result3 = a or (b and c)   # True or (False and True) → True or False → True

虽然有时结果一样,但加括号能让代码更清晰,别人读起来不费劲。强烈建议在组合条件里多用括号!

五、新手容易犯的错误

错误1:把 TrueFalse 写成小写

Python 区分大小写,truefalse 是不认识的,会报错(NameError)。必须写成 TrueFalse,首字母大写。

# 错误写法
a = true   # 报错:NameError: name 'true' is not defined

错误2:忘记括号导致条件意思出错

比如想表达“(不是下雨天)并且(有门票或者今天是生日)”,如果不加括号,会写成 not rain and ticket or birthday。这会被理解为 (not rain and ticket) or birthday,意思完全不一样了!要记住:not 优先级最高,and 其次,or 最低。所以最好加括号。

rain = True
ticket = False
birthday = True

# 想要的结果:不是下雨并且(有门票或者生日) → False and (False or True) → False and True → False
result_wrong = not rain and ticket or birthday   # 等价于 (not rain and ticket) or birthday → (False and False) or True → False or True → True
# 天啊!明明是下雨天,却因为生日是True,结果变成了True,不符合我们的意图

result_right = not rain and (ticket or birthday)  # 加上括号,结果正确 → False

错误3:混用 ===

在条件里判断是否相等要用 ==(两个等号),而 = 是赋值,别搞混了。比如:

score = 90        # 赋值
if score == 90:   # 判断是否等于90,用两个等号
    print("优秀")

六、完整示例:判断能不能去看电影

我们来编一个综合程序,根据条件判断能不能去看电影。规则:

  1. 不是休息日(is_weekend)?休息日才能去。
  2. 有电影票(has_ticket
  3. 而且(and)作业已经写完(homework_done
  4. 或者(or)今天是你的生日(is_birthday)——生日那天破例,不管作业。

另外,如果下雨(rain)就不能去,除非你带了伞(has_umbrella)。

用代码表示:

rain = False                # 没下雨
has_umbrella = True         # 带了伞
has_ticket = True           # 有电影票
homework_done = False       # 作业没写完
is_birthday = False         # 不是生日
is_weekend = True           # 是周末

# 核心判断:
# 是周末 并且 ( (有票并且作业写完) 或者 生日 ) 并且 ( 没下雨 或者 带伞了 )
can_watch = is_weekend and ( (has_ticket and homework_done) or is_birthday ) and ( not rain or has_umbrella )

print("能去看电影吗?", can_watch)   # 输出:能去看电影吗? False
# 因为作业没写完,又不是生日,所以 (has_ticket and homework_done) 是 False,整个括号是 False
# 周末是 True,但 and 连接,得到 False,所以不能去

如果我们改成 homework_done = True,就能去了——结果变为 True

七、下一步学什么?

你已经学会了用 andornot 来做逻辑判断。这些运算符最常用的地方是 if 条件语句,比如:

if score >= 60 and score < 80:
    print("成绩良好")

你还应该了解布尔类型bool:只有 TrueFalse 两个值),以及比较运算符==!=<><=>=)。把这些和逻辑运算结合起来,就能写出非常灵活的程序判断了!

逻辑运算就像生活中的“选择题组合器”——你定好规则,它帮你算出能不能做某件事。多练几次,你会觉得它既简单又实用!

例题精讲

1单选题

小明想吃冰激凌,但妈妈说:『如果今天天气热(hot=True)并且你写完作业(homework_done=True),才可以吃冰激凌。』下面哪个条件组合能让小明吃到冰激凌?

Ahot=False, homework_done=True
Bhot=True, homework_done=False
Chot=True, homework_done=True
Dhot=False, homework_done=False
2单选题

小红想吃巧克力,但爸爸说:『如果你考试考了100分(score_100=True)或者今天过生日(birthday_today=True),就可以吃巧克力。』下列哪个情况会让小红吃不到巧克力?

Ascore_100=True, birthday_today=False
Bscore_100=False, birthday_today=True
Cscore_100=True, birthday_today=True
Dscore_100=False, birthday_today=False
3判断题

在Python中,not True 的结果是 False。

4填空题
下面是判断一个数是否在0到100之间(包含端点)的代码。请填写逻辑运算符,使得程序能正确输出结果。\n\nnum = int(input())\nif num ___ 0 and num ___ 100:\n    print('在区间内')\nelse:\n    print('不在区间内')
5填空题
完成以下函数,该函数接收两个布尔值参数:has_icecream(有冰激凌)和 has_chocolate(有巧克力)。如果两种都有,返回『全都有』;如果至少有一种,返回『有一样』;否则返回『都没有』。请补全逻辑表达式。\n\ndef check_treats(has_icecream, has_chocolate):\n    if has_icecream ___ has_chocolate:\n        return '全都有'\n    elif has_icecream ___ has_chocolate:\n        return '有一样'\n    else:\n        return '都没有'