if语句
1.简单的if语句
最简单的if语句只有一个测试和一个操作,格式:
if conditional_test:
do something
举个具体的例子来看一看:
age = 19
if age >= 18:
print("You are old enough to vote!")
看看结果:
book@www.100ask.org:~/python/L7$ python votings.py
You are old enough to vote!
在if语句中,缩进的作用与for循环中相同。如果测试通过了,将执行if语句后面所有缩进的代码行,否则将忽略它们。
在紧跟在if语句后面的代码块中,可根据需要包含任意数量的代码行,比如:
age = 19
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
结果:
book@www.100ask.org:~/python/L7$ python votings.py
You are old enough to vote!
Have you registered to vote yet?
2.if-else语句
在编写程序时,经常需要在条件测试通过后执行一个操作,在没有通过时执行另一个操作,python提供了if-else语句。if-else语句块类似于简单的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!")
看看结果如何:
book@www.100ask.org:~/python/L7$ python votings.py
Sorry, you are too young to vote.
Please register to vote as soon as you turn 18!
if-else结构非常适合用于python中执行两种操作之一的情形,在这种简单的if-else结构中,总是会执行两个操作中的一个。
3.if-elif-else结构
在应用场景中经常会出现需要检查多个条件的情形,python为此需求提供了if-elif-else结构。python只执行if-elif-else结构中的一个代码块,一次检查每个条件测试,直到遇到满足的条件,条件满足后就会执行该条件后所进的代码。
举个具体的例子来感受一下吧。
在游乐场所有很多项目是根据年龄收费的,比如:
4岁一下免费
4~18岁收费5元
18岁及以上收费10元
#amusement_park.py
age = 12
if age < 4:
print("Your admission cost is 0RMB.")
elif age < 18:
print("Your admission cost is 5RMB")
else:
print("Your admission cost is 10RMB")
年龄不满足18岁,但是大于4岁,结果呢:
book@www.100ask.org:~/python/L7$ python amusement_park.py
Your admission cost is 5RMB
嗯,符合code的逻辑结果。
当然,在coding的过程中也会有一些比较好的编码风格,比如,可以不在if-elif-else代码块中打印门票价格,而只是在判断条件中设置门票的价格,然后再最后执行print语句。
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
else:
price = 10
print("Your admission yuan is " + str(price) + "RMB.")
book@www.100ask.org:~/python/L7$ python amusement_park.py
Your admission yuan is 5RMB.
这样coding除了效率高些,还有其他的好处,比如,这些代码更容易修改,想要调整输出的信息内容,只需要修改一条print语句就可以了,所以我们再coding的时候,可以多想想怎么可以上逻辑更加清晰,然后呢更加的便于维护和修改。
4.使用多个elif代码块
可以根据需要使用任意数量的elif代码块,举个例子吧:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
else:
price = 10
print("Your admission yuan is " + str(price) + "RMB.")
5,省略else代码块
python和C语言一样,并不要求if-elif结构后面必须有else代码块,例如:
age = 12
if age < 4:
price = 0
elif age < 18:
price = 5
elif age < 65:
price = 10
print("Your admission yuan is " + str(price) + "RMB.")
执行结果:
book@www.100ask.org:~/python/L7$ python amusement_park.py
Your admission yuan is 5RMB.
else语句可以说是一条包罗万象的语句,只要不满足任何if和elif中的条件测试,其中的代码就不会执行,这样的结果是可能会引入无效甚至恶意的数据,因此如果知道最终要测试的条件时,应考虑使用一个elif代码块来代替else代码块。这样,你就可以肯定,仅当满足相应的条件时,你的代码才会执行。
6.测试多个条件
if-elif-else结构功能强大,但仅适合用于只有一个条件满足的情况:遇到通过了的测试后,python就跳过余下的测试。
有时候呢必须检查你关心的所有条件,在这种条件下,应使用一系列不包括elif和else代码块的简单if语句。在可能有多个条件为true,且你需要再每一个条件为true时都采取相应措施时,适合使用这个方法。
举个例子吧,如果顾客再披萨店里面点了两种配料,就需要确保再其披萨中包含这些配料:
requested_toppings = ['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepproni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
结果呢?
book@www.100ask.org:~/python/L7$ python toppings-1.py
Adding mushrooms.
Adding extra cheese.
Finished making your pizza!
再看看下面的例子,使用的结构不一样:
requested_toppings = ['mushrooms','extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
elif 'pepproni' in requested_toppings:
print("Adding pepperoni.")
elif 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")
执行结果:
book@www.100ask.org:~/python/L7$ python toppings-1.py
Adding mushrooms.
Finished making your pizza!
使用不同的判断结构,得到的结果就不是想要的。
所以如果执行执行一个代码块,就是用if-elif-else结构;如果要运行多个代码块,就是用一系列独立的if语句。
alien_color = 'green'
if alien_color == 'green':
print("Congratulations on winning 5 points.")
else:
print(" ")
book@www.100ask.org:~/python/L7$ python alien-color.py
Congratulations on winning 5 points.
age =1
if age < 2:
print("he is a baby.")
elif age < 4:
print("he is a child.")
elif age < 20:
print("he is a young man.")
elif age < 65:
print("he is a Adults.")
else:
print("he is a old man.")
执行结果:
book@www.100ask.org:~/python/L7$ python baby.py
he is a baby.