if语句
实例
cars = ['bmw','toyota','honda','camery']
for car in cars: # 条件测试
if car == 'bmw':
print(car.upper())
else:
print(car.title())
条件测试(预备知识)
比较是否相等和不相等
需要注意的是“=”是赋值符号,“==”是相等符号,“!=”表示不相等,后两者一般用来判断符号前后两者是否相等,如上实例所示
判断是否区分大小写
car = 'Audi'
car == 'audi' # 大小写不同,结果为false,因此python中区分大小写
car.lower() == 'audi' # 结果为true,但是这并不改变car本身的值,仍为'Audi'
比较数字
条件语句中不仅包含“==”和“!=”,还包含小于(<),大于(>),小于等于(<=),大于等于(>=)
检查多个条件
-
在判断两个条件均为真时,使用and进行检查
-
判断两个条件只需满足一个时,使用or进行检查
age_0 >= 18 and age_1 <=22 # 判断年龄是否在[18,22]之间 age_0 >18 or age_1 >22 # 判断年龄小于18或大于22
检查特定值
检查特定值是否在列表或者元组中,使用关键词in和not in
users = ['david','leo','kurt']
'john' in users # False
'leo' in users # True
'john' not in users # True
布尔表达式
判定为真的使用true或1表示,为假的使用false或0表示
课后习题
5-1条件测试
car = 'honda'
print("Is car == 'honda'?")
print(car == 'honda') # 首先在括号内进行判定,结果为真,因此返回true
5-2更多的测试
char1 = 'hello'
char2 = 'Hello'
print(char1 == char2) # False
print(char1 == char2.lower()) # True
num1 = 45
num2 = 78
print(num1 == num2) #False
print(num1 > num2) # False
print(num1 != num2) # True
print(num1 >= num2) # False
print(num1 <= num2) # True
print(num1 < num2) # True
test = ['int','char','float']
print('int' in test) # True
print('long' in test) # False
print('long' not in test) # True
joe = 15
print(joe > 10 and joe < 20) # True
print(joe < 10 or joe > 20) # False
if语句
单一if语句
if 判断:
操作
单一if语句只含一个判断与一个操作,如果判断为真,则执行缩进中的操作语句,否则不执行操作
if-else语句
if 判断1:
操作1
else:
操作2
if-else语句中只含有两种情况,真或假,为真执行操作1,否则执行操作2
if-elif-else结构
if 判断1:
操作1
elif 判断2:
操作2
...
else:
操作n
此结构下适用于有多种条件限制的情况,满足其中一个条件便执行相应的操作,此处需要注意else语句并不是必需的,else包含列举情况之外的情况,可能包含一些无效的数据,因此在明确知道限制条件时,使用elif代码块就会更清晰明了
例:游乐场年龄小于4岁免票,4-18岁5美元,18岁及以上10美元,65岁及以上5美元
if age < 4:
prince = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 5
if age < 4:
prince = 0
elif age <18:
price = 5
elif age >= 18:
price = 10
elif age >= 65:
price = 10
上述两组代码前者使用了else语句,后者没有使用,相比较而言后者更为清晰,因此使用时要合理的考量else语句的使用
多条件语句
前面所提及的语句多为满足一个条件而进行的判定与操作,当面对多个条件需要判定与操作时,就需要多个if语句进行处理
例:餐厅顾客点单,点菜前后没有因果等关系
menu = ['meshrooms','tomato','cheese']
if 'meshrooms' in menu:
print('Adding meshrooms')
if 'tomato' in menu:
print('Adding tomato')
if 'cheese' in menu:
print('Adding cheese')
课后习题
5-3外星人颜色#1
alien_color = 'green'
if alien == alien_color:
print('you get 5 points')
alien_color = 'green'
if alien != alien_color:
print('you get 5 points')
5-4外星人颜色#2
alien_color = ['green','yellow','red']
if alien == 'green':
print('you get 5 points')
else:
print('you get 10 points')
alien_color = ['green','yellow','red']
if alien != 'green':
print('you get 10 points')
else:
print('you get 5 points')
5-5外星人颜色#3
alien_color = ['green','yellow','red']
#alien = 'green'/'yellow'/'red'三种情况
if alien == 'green':
print('you get 5 points')
elif alien == 'yellow':
print('you get 10 points')
else:
print('you get 15 points')
5-6人生的不同阶段
age = 22
if age < 2:
print('baby')
elif age < 4:
print('learing step')
elif age < 13:
print('child')
elif age < 20:
print('teenager')
elif age < 65:
print('adult')
else:
print('old man')
5-7喜欢的水果
favorite_fruits = ['apple','banana','mango']
if 'apple' in favorite_fruits:
print('you really like apple!')
if 'banana' in favorite_fruits:
print('you really like banana!')
if 'mangp' in favorite_fruits:
print('you really like mango')
if 'straberry' in favorite_fruits:
print('you really like straberry')
if 'grapes' in favorite_fruits:
print('you really like grapes')
小白入门python,如有错误,欢迎指出与探讨