1.简单介绍
每条if语句的核心都是一个值为 True 或 False 的表达式,这种表达式被称为条件测试。Python 根据条件测试的值为True还是 False 来决定是否执行if语句中的代码。如果条件测试的值为True,Python就执行紧跟在if语句后面的代码;如果为False,Python就忽略这些代码。
要判断是否相等,我们可以使用==来进行判断:
car = 'Audi' car.lower() == 'audi'
输出的结果为:
true
比如说我们在测试用户的用户名是否与他人重合的时候我们可以使用到这个判断。
要判断两个值是否不等,可结合使用惊叹号和等号(!=),其中的惊叹号表示不, 在很多编程语言中都如此:
requested_topping = 'mushrooms' if requested_topping != 'anchovies': ? print("Hold the anchovies!")输出的结果为:
Hold the anchovies!
如果需要对多个条件进行比较,则可以使用and和or两个符号:
num1 = 15 num2 = 20 ? num3 = 25 num4 = 30 ? if num1 == 15 and num2 == 20: ? print("All Right") ? if num3 == 25 or num4 == 40: ? print("One of them is right")and需要多个条件同时成立才能够成立,而or只需要一个条件成立就能够成立。
2.if-else语句
最简单的if语句只有一个测试和一个操作,但是使用了if-else语句之后便可以有两个操作:
num = 50 ? if num < 60: ? print("不及格") else: ? print("及格了")输出的结果为:
不及格
if-else语句可以演变为if-elif-else语句,用来执行2个以上的条件判断对执行对应的操作:
num = 85 ? if num < 60: ? print("不及格") elif 60<=num and num<=80: ? print("及格") else: ? print("优秀")运行的结果为:
优秀
3.用if语句来处理列表
我们可以把if语句和列表相结合:
food_list = ['apple', 'banana','orange'] ? for food in food_list: ? if food == 'apple': ? ? print("Apple is here") ? elif food == 'bana': ? ? print("Banana is here") ? else: ? ? print("Orange is here")输出的结果为:
Apple is here
Orange is here
Orange is here或者我们可以用来检测列表是否为空:
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?
Python语言会在列表至少包含一个元素的时候返回 True ,而列表为空的是否返回 False 。
当我们有着多个列表的时候,我们可以:
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.
Finished making your pizza!
Sorry, we don't have french fries.
Finished making your pizza!
Adding extra cheese.
Finished making your pizza!到此这篇关于Python语言中的if语句详情的文章就介绍到这了,更多相关Python语言中的if语句内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
查看更多关于Python语言中的if语句详情的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did99787