Python 条件语句:IF…Else、ELIF 和 Switch Case
什么是 Python 中的条件语句?
Python 中的条件语句根据特定布尔约束的计算结果为真或假,执行不同的计算或操作。条件语句在 Python 中由 IF 语句处理。
在本教程中,我们将了解如何在 Python 中应用条件语句。
- 什么是If语句?如何使用它?
- 当“如果条件”不满足时会发生什么
- 如何使用“其他条件”
- 当“其他条件”不起作用时
- 如何使用“elif”条件
- 如何用最少的代码执行条件语句
- Python 嵌套 if 语句
- Python 中的 Switch Case 语句
什么是 Python If 语句?
Python if 语句 用于决策操作。它包含一段代码,该代码仅在 if 语句中给出的条件为真时运行。如果条件为假,则运行可选的 else 语句,其中包含 else 条件的一些代码。
当你想证明一个条件而另一个条件不成立时,你可以使用 Python if else 语句。
Python if 语句语法:
if expression Statement else Statement
Python if…else 流程图
我们来看一个 Python if else 语句的例子:
# #Example file for working with conditional statement # def main(): x,y =2,8 if(x < y): st= "x is less than y" print(st) if __name__ == "__main__": main()
- 代码行 5:我们定义了两个变量 x, y =2, 8
- 第 7 行代码:Python 中的 if 语句检查条件 x
True 在这种情况下 - 代码第 8 行:变量 st 设置为“x 小于 y”。
- 代码第 9 行:print st 行将输出变量 st 的值,即“x 小于 y”,
如果条件不满足会发生什么
在这一步中,我们将看到 Python 中的 if 条件不满足时会发生什么。
- 代码行 5:我们定义了两个变量 x, y =8, 4
- 第 7 行代码:Python 中的 if 语句检查条件 x
False 在这种情况下 - 代码第 8 行:变量 st 是 NOT 设置为“x 小于 y”。
- 代码第 9 行:print st 行——试图打印一个从未声明过的变量的值。因此,我们得到一个错误。
如何使用“其他条件”
当您必须根据另一个陈述来判断一个陈述时,通常会使用“其他条件”。如果一个条件出错,那么应该有另一个条件来证明该语句或逻辑是正确的。
示例 :
# #Example file for working with conditional statement # def main(): x,y =8,4 if(x < y): st= "x is less than y" else: st= "x is greater than y" print (st) if __name__ == "__main__": main()
- 代码行 5:我们定义了两个变量 x, y =8, 4
- 第 7 行代码:Python 中的 if 语句检查条件 x
False 在这种情况下 - 第 9 行代码:程序控制流转到 else 条件
- 代码第 10 行:变量 st 设置为“x is greater 比 y。”
- 第 11 行代码:print st 行会输出变量 st 的值,即“x is greater than y”,
当“其他条件”不起作用时
在很多情况下,您的“其他条件”不会给您想要的结果。由于程序逻辑错误,它会打印出错误的结果。在大多数情况下,当您必须在一个程序中证明两个以上的语句或条件时,就会发生这种情况。
一个示例 会更好地帮助你理解这个概念。
这里两个变量都是相同的 (8,8) 并且程序输出是 “x is greater than y”, 这是错误 .这是因为它检查第一个条件(Python 中的 if 条件),如果失败,则默认打印出第二个条件(else 条件)。在下一步中,我们将看到如何纠正这个错误。
# #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" else: st= "x is greater than y" print(st) if __name__ == "__main__": main()
如何使用“elif”条件
为了纠正之前由“else condition”造成的错误,我们可以使用“elif” 陈述。通过使用“elif ”条件,您是在告诉程序在其他条件出错或不正确时打印出第三个条件或可能性。
示例
# #Example file for working with conditional statement # def main(): x,y =8,8 if(x < y): st= "x is less than y" elif (x == y): st= "x is same as y" else: st="x is greater than y" print(st) if __name__ == "__main__": main()
- 代码行 5:我们定义了两个变量 x, y =8, 8
- 代码行 7:if 语句检查条件 x
False 在这种情况下 - 代码第 10 行:程序控制流程进入 elseif 条件。它检查 x==y 是否为真
- 代码第 11 行:变量 st 设置为“x is same as 是的。”
- 第 15 行代码:程序控制流退出 if 语句(它不会到达 else 语句)。 并打印变量 st。输出是“x is same as y”这是正确的
如何用最少的代码执行条件语句
在这一步中,我们将看到如何压缩条件语句。我们可以将它们与单个代码一起使用,而不是单独为每个条件执行代码。
语法
A If B else C
示例 :
def main(): x,y = 10,8 st = "x is less than y" if (x < y) else "x is greater than or equal to y" print(st) if __name__ == "__main__": main()
- 代码行 2:我们定义了两个变量 x, y =10, 8
- 代码行 3:变量 st 设置为“x 小于 y”,如果 x
y 变量中,st 被设置为 “x 大于或等于 y”。 - 代码行 4:打印 st 的值并给出正确的输出
- 无需为条件语句编写冗长的代码,Python 让您可以自由地以简洁的方式编写代码。
Python 嵌套 if 语句
以下示例演示了 Python 中的嵌套 if 语句
total = 100
#country = "US"
country = "AU"
if country == "US":
if total <= 50:
print("Shipping Cost is $50")
elif total <= 100:
print("Shipping Cost is $25")
elif total <= 150:
print("Shipping Costs $5")
else:
print("FREE")
if country == "AU":
if total <= 50:
print("Shipping Cost is $100")
else:
print("FREE") 在上面的代码中取消注释第 2 行并注释第 3 行并再次运行代码
Python 中的 Switch Case 语句
什么是 Switch 语句?
switch 语句是一种多路分支语句,它将变量的值与 case 语句中指定的值进行比较。
Python 语言没有 switch 语句。
Python使用字典映射在Python中实现Switch Case
示例
function(argument){
switch(argument) {
case 0:
return "This is Case Zero";
case 1:
return " This is Case One";
case 2:
return " This is Case Two ";
default:
return "nothing";
};
};
对于上述 Python 中的 Switch 案例
def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")
if __name__ == "__main__":
argument = 1
print (SwitchExample(argument))
Python 2 示例
以上代码是 Python 3 示例,如果您想在 Python 2 中运行,请考虑以下代码。
# If Statement
#Example file for working with conditional statement
#
def main():
x,y =2,8
if(x < y):
st= "x is less than y"
print st
if __name__ == "__main__":
main()
# How to use "else condition"
#Example file for working with conditional statement
#
def main():
x,y =8,4
if(x < y):
st= "x is less than y"
else:
st= "x is greater than y"
print st
if __name__ == "__main__":
main()
# When "else condition" does not work
#Example file for working with conditional statement
#
def main():
x,y =8,8
if(x < y):
st= "x is less than y"
else:
st= "x is greater than y"
print st
if __name__ == "__main__":
main()
# How to use "elif" condition
#Example file for working with conditional statement
#
def main():
x,y =8,8
if(x < y):
st= "x is less than y"
elif (x == y):
st= "x is same as y"
else:
st="x is greater than y"
print st
if __name__ == "__main__":
main()
# How to execute conditional statement with minimal code
def main():
x,y = 10,8
st = "x is less than y" if (x < y) else "x is greater than or equal to y"
print st
if __name__ == "__main__":
main()
# Nested IF Statement
total = 100
#country = "US"
country = "AU"
if country == "US":
if total <= 50:
print "Shipping Cost is $50"
elif total <= 100:
print "Shipping Cost is $25"
elif total <= 150:
print "Shipping Costs $5"
else:
print "FREE"
if country == "AU":
if total <= 50:
print "Shipping Cost is $100"
else:
print "FREE"
#Switch Statement
def SwitchExample(argument):
switcher = {
0: " This is Case Zero ",
1: " This is Case One ",
2: " This is Case Two ",
}
return switcher.get(argument, "nothing")
if __name__ == "__main__":
argument = 1
print SwitchExample(argument)
总结:
Python 中的条件语句由 if 语句处理,我们在这里看到了可以使用 Python if else 等条件语句的各种其他方式。
- “if condition”——当其中一个条件为真或假时,需要打印结果时使用。
- “其他条件”- 当您的一个条件不满足要求时,您想打印出声明时使用它
- “elif 条件” - 当您有第三种可能性作为结果时使用它。您可以使用多个 elif 条件来检查 4 th ,5 th ,6 th 代码中的可能性
- 我们可以通过在单个语句中声明所有条件来运行代码,从而使用最少的代码来执行条件语句
- Python If 语句可以嵌套
Python