python入门——选择结构

if语句

cars = ['audi','sys','caller']
for car in cars :
    if car ==  "audi":
    	print(car.upper())
    else : 
        print(car.title())
        

条件测试

每个if语句的核心都是一个值为true或false的表达式,这种表达式成为条件测试

切记if条件判断语句也是要冒号:的

else也是

检查是否相等时要考虑大小写
car ='audi'
car == 'AUDI'
#false
比较数字
answer = 17
if answer != 42:
	print('that is not the correct answer.please try again')
    #也可以包含各种数字比较
    if age <= 17:
        print ''
    if age > 20
    	print('')

检查多个条件

1.使用and检查多个条件
age_0 = 22
age_1 = 18
if age_0 >= 21 and age_1 >= 21:
	print("all people 's name bigger than 21")

如果每个条件都是true,则结果为true

2,使用or 检查多个对象
age_0 = 22
age_1 = 18
if age_0 >= 21 or age_1 >= 21:
	print("there is one people bigger than 21")
检查特定值是否包含在列表中
形式
toppings = ['mushroom','onions','pineapple']
if 'mushroom' in toppings:
    print('true')
检查特定值不包含在列表中
banned_users = ['anna','kirito','sys']
user = 'marri'
if user not in banned_users :
    print("you are ok")
布尔表达式

布尔表达式通常用于记录条件

数字在列表
number = [1,2,3,4]
if number[0] == 1:
    print('my conclusion is true')

简单的if语句

最简单的if语句只有一个测试和一个操作

if condition_test:
	do something

if _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结构

需要检查超过两个的情形

在现实世界中, 很多情况下需要考虑的情形都超过两个。 例如, 来看一个根据年龄段收费的游乐场:
4岁以下免费;
4~18岁收费5美元;
18岁(含) 以上收费10美元。

age = 12
if age < 4: 
    print("Your admission cost is $0")
elif age < 18:
    print("Your admission cost is $5")
else:
    print("Your admission cost is $10")
使用多个elif结构
age  = 12

if age < 4:
    price = 0
elif age < 18:
    price = 5
elif age < 65:
    price = 10
else :
    price = 5
print("Your admission cost is "+str(price)+" .")

if-elif-else 结构功能强大, 但仅适合用于只有一个条件满足的情况: 遇到通过了的测试后, Python就跳过余下的测试。 这种行为很好, 效率很高, 让你能够测试一个特定的
条件

if语句处理列表

即在for循环里面加if语句

对列表的东西进行判断

requests_toppings = ["mushrooms","green pepper","extra cheese"]
for request in request_toppings:
	if request = "green pepper":
        print("Sorry,we are out of green peppers rightnow")
     else:
        print("Adding" + request +" .")
 print("your pizza")

确定列表不是空的

requested = []
#建立一个空列表
if requested:#若列表不为空,会返回ture。否则false
    for request in requested:
        print("Adding"+request +" .")
else:
    print("Are you sure you want a plain pizza")
        

在这里, 我们首先创建了一个空列表, 其中不包含任何配料(见❶) 。 在❷处我们进行了简单检查, 而不是直接执行for 循环。 在if 语句中将列表名用在条件表达式中
时, Python将在列表至少包含一个元素时返回True , 并在列表为空时返回False 。 如果requested_toppings 不为空, 就运行与前一个示例相同的for 循环; 否则, 就打印
一条消息, 询问顾客是否确实要点不加任何配料的普通比萨(见❸

使用多个列表

创建俩个列表,用来判断第一个列表中的元素是否在另一个列表中

available_toppings = ['mushroom','olives','pepper','pineapple','extra cheese']
requested_toppings = ['mushroom','french fires','extra cheese']
for request in requested_toppings:
    if request in available:
        print("Sorry,we don't have "+request+" .")
    else:
        print("Adding "+request+" .")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值