Python学习之路day01——if语句

一、if语句的案例

上面代码,输出结果是: Audi    BMW  Subaru  Toyota

二、条件测试(条件表达式,格式如上图)

每条if语句的核心都是一个值为TrueFalse的表达式,这种表达式被称为条件测试Python
根据条件测试的值为True还是False来决定是否执行if语句中的代码。如果条件测试的值为True
Python就执行紧跟在if语句后面的代码;如果为FalsePython就忽略这些代码。  

1、条件检查的符号:== :相等,!=:不等,>:大于, >=:大于等于,<=:小于等于,<:小于

此处忽略,无需再述。

2、关键字and:多条件与门

3、关键字or:多条件或门

4、关键字in:检查是否包含某个元素

 

5、关键字not in:检查是否不包含某个元素

      与in的用法是一样的

6、布尔表达式(boolearn数据类型)

boolearn类型的变量只有两个选择,True 和False

代码:   

value = True
if value ==False :
print(22)
elif value ==True:
print(11)
else:
print(00)

结果:11

注意:if语句的一般性结构,“if ---elif---else---”,“if ---elif---”,“if ---else---”,“if ---”。

 7、if语句的利用

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!

 

 

 

猜你喜欢

转载自www.cnblogs.com/jokerwang/p/9240152.html