Python_If语句

1. if语句
# _*_ coding:utf8 _*_

# if 语句
age = 19
if age >= 18:
    print("you are my choose")

# if-else语句
age = 17
if age >= 18:
    print("you are my choose")
    print(" have you already register?")
else:
    print("you are not my choose")
    print("sorry,")

# if-elif-else结构
age = 12
if age <= 4:
    print("free")
elif 18 >= age > 4:
    print("five")
else:
    print("ten")

# 使用多个 elif语句判断

age = 10

if age <= 4:
    print(" fighting,free")
elif 13 >= age > 4:
    print("two")
elif 18 >= age > 13:
    print("ten")
else:
    print("bad,bad")

# 省略else 语句 Python中 if-elif结构中并不要求必须写else语句,在有些情况下可以不用else

age = 29
if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
elif age > 65:
    price = 5
print(" your price is " + str(price))

# 如果你想执行一个代码块,就是使用 if-elif-else 结构,如果你都想使用那么,你就使用多个if结构的

2. if操作列表

# _*_ coding:utf8 _*_
# if语句操作列表
# 操作特殊元素
request_toppings = ['you', 'he', 'her', 'she', 'my']

for request_topping in request_toppings:
    if request_topping == 'she':
        print('sorry ' + request_topping)
    else:
        print(request_topping)

# 确定列表不是为空的
# 当列表的长度大于1的时候 用if判断就会返回 True  为空就会返回False

request_toppings = []
if request_toppings:
    print(" sorry is null")
else:
    print(" is not null")

# 使用多个列表 循环嵌套 条件判断

one = ['one','two','three','four','five','six']
two = ['five','eight','ten','seven']

for value in one:
    if value in two:
        print(value)
    else:
        print('sorry')

### Python `if` 语句的使用方法 #### 基本概念 Python 中的 `if` 语句用于基于条件测试的结果来控制程序流程。每条 `if` 语句的核心是一个返回布尔值 (`True` 或 `False`) 的表达式,称为 **条件测试**[^1]。 当条件测试的结果为 `True` 时,Python 执行紧跟其后的代码块;如果结果为 `False`,则跳过该代码块[^2]。 --- #### 语法结构 以下是 `if` 语句的基本形式: ```python if condition: # 当 condition 为 True 时执行此代码块 else: # 当 condition 为 False 时执行此代码块 ``` 可以扩展为多分支结构: ```python if condition1: # 当 condition1 为 True 时执行此代码块 elif condition2: # 当 condition1 为 False 且 condition2 为 True 时执行此代码块 else: # 当所有条件均为 False 时执行此代码块 ``` --- #### 示例代码 以下是一些典型的 `if` 语句应用实例: ##### 示例 1:简单的条件判断 检查两个变量是否相等并打印相应消息: ```python a = 5 b = 10 if a == b: print("a 和 b 相等") # 此部分不会被执行,因为条件为 False else: print("a 和 b 不相等") # 输出: a 和 b 不相等 ``` ##### 示例 2:多重条件判断 利用 `and` 和 `or` 实现复杂的逻辑判断: ```python age = 25 is_student = True if age >= 18 and is_student: print("您是一名成年学生") # 输出: 您是一名成年学生 elif age < 18 or not is_student: print("您不符合条件") ``` 注意:由于 Python 支持短路评估,上述代码中第二个条件可能无需完全计算[^4]。 ##### 示例 3:登录验证(带有限制尝试次数) 模拟用户登录场景,允许最多三次错误尝试: ```python name = 'root' passwd = 'westos' attempts = 0 max_attempts = 3 while attempts < max_attempts: input_name = input("请输入用户名: ") input_passwd = input("请输入密码: ") if input_name == name and input_passwd == passwd: print("登录成功!") break else: attempts += 1 if attempts < max_attempts: print(f"用户名或密码错误! 还有 {max_attempts - attempts} 次机会.") else: print("尝试次数过多,登录失败!") # 超过最大尝试次数后退出循环 ``` --- #### 特殊用途:结合 `continue` 提高效率 在循环中,可以通过 `if` 结合 `continue` 跳过特定情况下的后续操作,从而提高代码运行效率[^3]。 例如,过滤掉负数列表中的所有负值: ```python numbers = [-1, 2, -3, 4, -5] for num in numbers: if num < 0: continue # 遇到负数时直接进入下一次迭代 print(num) # 输出正数: 2, 4 ``` --- ### 总结 通过合理设计条件测试和嵌套层次,`if` 语句可以帮助开发者实现复杂业务逻辑的同时保持代码清晰易读。此外,善用短路求值机制以及与其他控制流工具配合,可进一步提升性能与灵活性[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值