栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python学习——if语句

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python学习——if语句

if语句 条件测试

条件测试指if语句中一个值为True或False的表达式,如果条件测试的值为True,执行紧跟在if语句后面的代码;如果条件测试的值为False,则忽略这些代码

检查是否相等
  1. 检查相等:条件测试可将一个变量的当前值同特定值进行比较,"=="表示表示发问****(相等运算符),如

    car='bmw'#一个等号将car的值设置为bmw
    car=='bmw'
    

    大小写问题:若大小写很重要,则大小写不一致时会判断为False,但如果大小写无关紧要,只需检查变量的值,可将变量的值转换为小写,如

    car='Audi'
    car=='audi'
    
    car='Audi'
    car.lower()=='audi'#lower()不会影响关联到变量car的值
    
  2. 检查不相等:结合使用惊叹号和等号**(!=),其中惊叹号表示不**,如

    requested_topping='mushrooms'
    if requested_topping!='anchovies':
       print('Hold the anchovies!')
    
数值比较
answer=17
if answer!=42
   print('That is not the correct answer,Please try again!')
检查多个条件

利用布尔运算符and,or,not进行操作

  1. 使用and检查多个条件,True要求条件全部满足,如

    age_0=22
    age_1=18
    (age_0>=21) and (age_1>=18)#括号的目的是增加可读性,去掉页可以
    
  2. 使用or检查多个条件,True仅要求一个条件满足即可,如

    age_0=22
    age_1=18
    age_0>=21 or age_1>=18
    
特定值是否包含在列表中
  1. 特定值包含在列表中:使用关键词in判断,如

    requested_toppings=['mushrooms','onions','pineapple']
    'mushrooms' in requested_toppings
    
  2. 特定值不包含在列表中:只需判断值不在列表中即可,使用关键词not in判断,如

    banned_users=['andrew','carolina','david']
    user='marie'
    if user not in banned_users:
       print(f'{user.title()}, you can post a resoinse if you wish.')
    
if语句 简单的if语句

原理与条件测试相同,在if语句中,缩进的作用与在for循环中相同,如

age=19
if age>=18:#冒号
   print('You are old enough to vote!')
   print('Have you registered to vote yet?')
if-else语句

适用于两种操作二选一的情形,当条件测试通过时执行一个操作,在没有通过时执行另一个操作(通过else语句实现)

age=17
if age>=18:
   print('You are old enough to vote!')
   print('Have you registered to vote yet?')
else
   print('Sorry, you are too young to vote.')
   print('Please register to vote as soon as you turn 18!')
if-elif-else结构

原理:适用于检查两个条件的情形,python只执行if-elif-else结构中的一个代码块,依次检查每个条件测试,直到遇到通过了的条件测试,测试通过后,python将执行紧跟在它后面的代码,并跳过余下的测试

一个elif代码块
age=12
if age<4:
   price=0
elif age<18:
   price=25
else:
   price=40
print(f'Your admission cost is ${price}.')
多个elif代码块
age=12
if age<4:
   price=0
elif age<18:
   price=25
elif age<65:
   price=40
else:
   price=20
print(f'Your admission cost is ${price}.')
省略else代码块

省略原因:只要不满足任何if或elif中的条件测试,else中的代码就会执行,可能会引入无效甚至恶意的数据,若知道最终要测试的条件,应考虑用一个elif代码块待替代else代码块,保证仅当满足相应的条件时,代码才会执行

age=12
if age<4:
   price=0
elif age<18:
   price=25
elif age<65:
   price=40
elif:
   price=20
print(f'Your admission cost is ${price}.')
测试多个条件

应用条件:if-elif-else结构仅适合用于只有一个条件满足的情况,若存在多个条件为true且需要在每个条件为true时都采取相应措施,if-elif-else结构会漏掉信息

requested_toppings=['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
   print('adding mushrooms.')
if 'extra cheese' in requested_toppings:
   print('adding extra cheese.')
if 'pepperoni' in requested_toppings:
   print('addingpepperoni.')
print('nFinished making your pizza!')
使用if语句处理列表

目的:通过结合if语句和列表,可对列表中特定的值做特殊处理、高效管理不断变化的情况,证明代码在各种情形下都将按照预期那样运行

检查特殊类型
requested_toppings=['mushrooms','green peppers','extra cheese']
for requested_topping in requested_toppings:
    if requested_topping=='green peppers':
       print('sorry, we are out of green peppers right now.')
    else:
       print(f'adding {requested_topping}.')
print('nfinished making your pizza!')
确定列表是否为空

在if语句中将列表名用作条件表达式时,python将在列表至少包含一个元素时返回true,在列表为空时返回false,在运行for循环前确定列表是否为空很重要

requested_toppings=[]
if requested_toppings:
   for requested_topping in requested_toppings:
       print(f'adding {requested_topping}.')
   print('nfinished making your pizza!')
else:
   print('are you sure you want a plain pizza?')
使用多个列表
available_toppings=['mushrooms','olives','green_peppers','pepperoni','pineapple','extra cheese']
requested_toppings=['mushrooms','french fries','extra cheese']
for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
       print(f'adding {requested_topping}.')
    else:
       print(f"sorry, we don't have {requested_topping}")
print('nfinished making your pizza!')
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号