csq17

导航

csq-蓝桥杯python-基础语法1-逻辑运算与条件语句

一、什么是逻辑运算?

在 Python 中,比较运算(如 > < ==)的结果是布尔值 True 或 False。
实际开发中经常需要组合多个条件,例如:年龄大于 18 并且身高大于 170 cm,该怎么判断?

Python 提供了 3 个逻辑运算符 来组合布尔值:

运算符 名称 含义
and 两个条件都为 True 时,结果才为 True
or 两个条件中只要有一个为 True,结果就为 True
not 对布尔值取反,True → FalseFalse → True

二、逻辑运算符详解

1. and(与)

规则:当且仅当所有条件都为 True,最终结果才为 True

age = 20
score = 95

print(age > 18 and age < 30)     # True(年龄在 18~30 之间)
print(age > 18 and score > 90)   # True(成年且分数 > 90)
print(age > 18 and score > 100)  # False(分数未超过 100)

2. or(或)

规则:只要有一个条件为 True,最终结果就为 True

# 判断是否为周末
day = "Sunday"
is_weekend = (day == "Saturday" or day == "Sunday")
print(is_weekend)  # True

# 判断是否有资格获得折扣
is_student = False
age = 65
has_discount = is_student or age > 60
print(has_discount)  # True(年龄符合条件)

3. not(非)

规则:对布尔值取反

print(not False)  # True
print(not True)   # False

三、逻辑运算符的优先级

逻辑运算符的优先级低于算术运算符和比较运算符,顺序为:

      () > 算术运算符 > 比较运算符 > not > and > or 

建议:不确定优先级时,用括号 () 明确顺序,避免逻辑错误

# 默认优先级:and 高于 or
print(True or False and False)  # True
# 等价于 True or (False and False) => True or False => True

# 使用括号改变优先级
print((True or False) and False)  # False
# 等价于 (True or False) and False => True and False => False

四、链式比较

Python 支持多个比较运算符链接,让代码更简洁、更符合数学直觉

传统写法:

age = 25
is_young = age > 18 and age < 30
print(is_young)  # True

链式写法:

age = 25
is_young = 18 < age < 30
print(is_young)  # True

底层等价于 18 < age and age < 30,但更简洁直观

五、条件语句

1. if 语句:最基本的判断

作用:根据条件决定执行哪段代码

语法

if condition:
    # 条件为 True 时执行的代码(注意缩进)

示例

age = 20
if age >= 18:
    print("您已成年。")

score = 50
if score >= 60:
    print("恭喜,考试及格!")  # 不会执行

关键点
• 以 if 开头,条件后加 :(分号)

• 下一行必须是缩进的代码块

2. Python 的缩进规则

Python 用缩进而非大括号 {} 定义代码块,这是其核心特征

正确缩进(4 个空格):

age = 20
if age >= 18:
    print("成年人")   # 属于 if 块
    print("欢迎!")   # 属于 if 块
print("程序结束")     # 不属于 if 块

错误缩进

age = 20
if age >= 18:
print("IndentationError!")  # 缺少缩进,报错

规范
• 统一使用 4 个空格作为一级缩进

• 同一代码块缩进量必须相同

• 错误缩进会导致 IndentationError 或逻辑错误

3. if-else 语句:两种选择

作用:处理“如果…否则…”的场景

语法

if condition:
    # 条件为 True 时执行
else:
    # 条件为 False 时执行

示例

# 判断奇偶数
number = 10
if number % 2 == 0:
    print("这是一个偶数。")
else:
    print("这是一个奇数。")

# 登录验证
password_correct = True
if password_correct:
    print("登录成功!")
else:
    print("密码错误!")

4. if-elif-else 语句:多种选择

作用:处理多个互斥条件(如成绩评级)

语法

if condition1:
    # condition1 为 True 时执行
elif condition2:
    # condition2 为 True 时执行
else:
    # 所有条件都不满足时执行

执行逻辑
从上到下依次检查条件,遇到第一个为 True 的条件就执行对应代码块,然后结束整个结构

示例:成绩评级:

score = 85
if score >= 90:
    print("优秀")
elif score >= 80:
    print("良好")  # 输出:良好
elif score >= 60:
    print("及格")
else:
    print("不及格")

六、总结速记

1. 逻辑运算
• and:全 True 才 True
• or:一 True 则 True
• not:取反
• 优先级:() > 算术 > 比较 > not > and > or
​​
2. 链式比较
• 18 < age < 30 等价于 age > 18 and age < 30,更简洁

3. 条件语句
• if:单条件
• if-else:二选一
• if-elif-else:多条件互斥

4. 缩进是生命
• Python 靠缩进界定代码块,统一 4 空格,否则会报错

学习建议:逻辑运算与条件语句是 Python 语法的基石,后续学算法、做项目都会高频使用。建议我们初学者动手练习,加深理解

posted on 2025-12-07 22:10  陈诗棋  阅读(9)  评论(0)    收藏  举报