一、条件测试
1.检查是否相等(“=”是赋值,“==”是判断值是否相等)
2.检查相等时不考虑大小写
在Python中检查是否相等时区分大小写,但如果大小写无关紧要,而只想检查变量的值,可
将变量的值转换为小写。
car = 'Audi'
car.lower() == ''audi
#输出结果:True
#########
car = 'Audi'
car.lower() == 'audi'
True
car
'Audi'
#不会影响原来的变量
3.检查是否不相等(!=)
4.比较数字(>、<、>=、<=)
<u>为符合编程规范,在以上符号中的两端各空一格。</u>
5.检查多个条件
使用and和or(相当于java 中的&&、||)
5.1 使用and检查多个条件
age_0 = 22
age_1 = 18
(age_0 >= 21) and (age_1 >= 21 )
False
age_1 = 22
(age_0 >= 21) and (age_1 >= 21)
True
5.2 使用or检查多个条件
age_0 = 22
age_1 = 18
age_0 >= 21 or age_1 >= 21
True
age_0 = 18
age_0 >= 21 or age_1 >= 21
False
6. 检查特定值是否包含/不包含在列表中(关键字:in、not in)
###################包含(in)
requested_toppings = ['mushrooms', 'onions', 'pineapple']
'mushrooms' in requested_toppings
True
'pepperoni' in requested_toppings
False
###############不包含(not in)
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(user.title() + ", you can post a response if you wish.")
#结果:Marie, you can post a response if you wish.
-
布尔表达式
game_active = True can_edit = False
二、if 语句
1. 简单if语句
if conditional_test: do something1 do something2
缩进:缩进的作用与for循环中相同。如果测试通过了,将执行if语句后面所有缩进
的代码行,否则将忽略它们。
2. 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!") ##输出: Sorry, you are too young to vote. Please register to vote as soon as you turn 18!
-
if-elif-else 结构
检查超过一个或者多个的情形
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) + ".")
注意:<u>elif 的作用类似于java中的 else if</u>,其中最后的else(第8行)也可以胜率
-
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
elif age >= 65:
price = 5
print("Your admission cost is $" + str(price) + ".")
4. 测试多个条件
#不同于 if-elif-else结构,这里的每个if条件都会得到判断(类似于java中的switch语句)
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
如果你只想执行一个代码块,就使用if-elif-else结构;如果要运行多个代码块,就
使用一系列独立的if语句。
三、使用if语句处理列表
1. 检查特殊元素
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("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
#输出结果
Adding mushrooms.
Sorry, we are out of green peppers right now.
Adding extra cheese.
Finished making your pizza!
2. 确定列表是否为空
#在运行for循环前确定列表是否为空
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print("Adding " + requested_topping + ".")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")
#结果:Are you sure you want a plain pizza?
3. 使用多个列表
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("Adding " + requested_topping + ".")
else:
print("Sorry, we don't have " + requested_topping + ".")
print("\nFinished making your pizza!")
#输出:
Adding mushrooms.
Sorry, we don't have french fries.
Adding extra cheese.
Finished making your pizza!
网友评论