python编程 从入门到实践 第五章 if语句

第五章 学习笔记

#5.1 一个简单的示例
cars=['audi','bmw','subaru','toyota']
for car in cars:
    if car=='bmw':
        print(car.upper())
    else:
        print(car.title())
#5.2 条件测试
#5.2.1 检查是否相等
car='audi'
print(car=='audi')
##条件测试的值为True和False
## 两个等号为相等运算符,两边的值相等则返回True,否则False

#5.2.2 检查是否相等时不考虑大小写
car='Audi'
print(car=='audi')
print(car.lower()=='audi')
print(car)
## 函数lower()不会修改存储在变量car中的值,所以这样的比较不会影响原来的值

#5.2.3 检查是否不相等
requested_topping='mushrooms'
if requested_topping!='anchovies':
    print("hold the anchovies!")

#5.2.4 比较数字
answer=17
if answer!=42:
    print("that is not the correct answer,please try again!")
print(answer<=17)
print(answer>18)

#5.2.5 检查多个条件
# 1。 使用and 检查多个条件
age_0=22
age_1=18
print(age_0>=21 and age_1>=21)
print(age_0>=21 or age_1>=21)

#5.2.6 检查特定值是否包含在列表中
requested_toppings=['mushrooms','onions','pineapple']
print('mushrooms'in requested_toppings)
##可以检查列表中是否含有特定的值

#5.2.7 检查特定值是否不包含在列表中
user='abc'
if user not in requested_toppings:
    print("true")

#5.2.8 布尔表达式
##条件测试的别名
##用于记录条件,在跟踪程序状态或程序中的重要条件方面
game_active=True
can_edit=False

#5.3 if语句
#5.3.1 简单的if语句
age=19
if age>=18:
    print("ye")
    print("ha ")
##如果两个都缩进,如果条件成立,两个都执行

#5.3.2 if-else语句
age=19
if age<=18:
    print("ye")
    print("ha ")
else:
    print("sorry")
    print("!")

#5.3.3 if-elif-else结构
##检查超过两个的情形
age=12
if age<4:
    print("cost is 0")
elif age <18:
    print("cost is $5")
else:
    print("cost is $10")
#简化
age=12
if age<4:
    price=0
elif age <18:
    price = 5
else:
    price = 10
print("cost is "+str(price)+'.')

#5.3.4 使用多个elif代码块
age=12
if age<4:
    price=0
elif age <18:
    price = 5
elif age<65:
    price=10
else:
    price = 5
print("cost is "+str(price)+'.')

#5.3.5 省略else代码块
age=12
if age<4:
    price=0
elif age <18:
    price = 5
elif age<65:
    price=10
elif age>=65:
    price = 5
print("cost is "+str(price)+'.')
##else可以省略哦

#5.3.6 测试多个条件
cars=['audi','bmw','subaru','toyota']
if 'audi'in cars:
    print("adding sudi.")
if 'bmw' in cars:
    print("adding bmw.")
if 'abc' in cars:
    print("adding abc.")
##如果想运行多个代码块,就用一系列独立的if语句
# 5.4 使用if语句处理列表
# 5.4.1 检查特殊元素
requested_toppings=['mushrooms','onions','pineapple']
for requested_topping in requested_toppings:
    print("Adding "+requested_topping+".")
print("\nFinished making your pizza!")

requested_toppings=['mushrooms','onions','pineapple']
for requested_topping in requested_toppings:
    if requested_topping =='mushrooms':
        print("sorry")
    else:
        print("Adding "+requested_topping+".")
print("\nFinished making your pizza!")

#5.4.2 确定列表不是空的
requested_toppings=[]
if requested_toppings:
    for requested_topping in requested_toppings:
        print("Adding " + requested_topping + ".")
    print("\nFinished making your pizza!")
else:
    print("\nare you sure want a pizza?")
## 首先创建了一个空列表,在if语句中将列表名用在条件表达式里时python将至少包含一个元素时返回True,如果是空就返回false
#5.4.3 使用多个列表
available_toppings=['mushrooms','olives','green peppers','pepperoni','pineapple','extra chess']
requested_toppings=['mushrooms','extra chess','french fries']
for requested_topping in requested_toppings:
    if requested_topping in available_toppings:
        print("Adding " + requested_topping + ".")
    else:
        print("sorry!")
print("\nFinished making your pizza!")
#5.5 设置if语句的格式
## 在==、 >=、 <=等比较运算符的两边添加一个空格

GOOD LUCK!!!

你可能感兴趣的:(python编程 从入门到实践 第五章 if语句)