Python 条件语句:IF…Else、ELIF 和 Switch Case
什么是条件语句 Python?
条件语句 Python 根据特定布尔约束的计算结果为真还是假,执行不同的计算或操作。条件语句由 IF 语句处理 Python.
什么是 Python 如果语句?
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 检查条件 x 真 在这种情况下
- 代码第 8 行:变量 st 设置为“x 小于 y”。
- 代码第 9 行:print st 这一行将输出变量 st 的值,即“x 小于 y”,
当“if 条件”不满足时会发生什么
在此步骤中,我们将看到当条件为 Python 不符合。
- 代码行 5:我们定义两个变量 x, y = 8, 4
- 代码行 7: Python 检查条件 x 假 在这种情况下
- 代码行 8:变量 st 是 不是 设置为“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 检查条件 x 假 在这种情况下
- 代码行 9:程序控制流转到 else 条件
- 代码行 10:变量 st 设置为“x 是 更大的 比 y”。
- 代码第 11 行:print st 这一行将输出变量 st 的值,即“x 大于 y”,
当“其他条件”不起作用时
在许多情况下,您的“其他条件”不会给您所需的结果。它会打印出错误的结果,因为程序逻辑存在错误。在大多数情况下,当您必须在程序中证明两个以上的语句或条件时,就会发生这种情况。
An 例子 将更好地帮助您理解这个概念。
这里两个变量相同(8,8),程序输出为 “x 大于 y”, 这是 WRONG。这是因为它检查第一个条件(如果条件在 Python),如果失败,则默认打印出第二个条件(其他条件)。在下一步中,我们将了解如何纠正此错误。
# #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 假 在这种情况下
- 代码行 10:程序控制流转到 elseif 条件。它检查 x==y 是否为真
- 代码行 11:变量 st 设置为“x 是 一样 y。”
- 代码行 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:如果 x y 变量 st 设置为 “x 大于或等于 y。”
- 代码行 4:打印 st 的值并给出正确的输出
-
无需为条件语句编写长代码, Python 让您可以自由地以简短、简洁的方式编写代码。
Python 嵌套 if 语句
以下示例演示了嵌套 if 语句 Python
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 行,然后再次运行代码
Switch Case 语句 Python
什么是 Switch 语句?
switch 语句是一种多路分支语句,它将变量的值与 case 语句中指定的值进行比较。
Python 语言没有 switch 语句。
Python 使用字典 映射以实现 Switch Case Python
例如:
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";
};
};
对于上述 Switch 案例 Python
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 condition”- 当你想在一个条件不满足要求时打印出语句时使用它
- “elif 条件” – 当结果有第三种可能性时使用。您可以使用多个 elif 条件来检查 4th,5th,6th 代码中的可能性
- 我们可以使用最少的代码来执行条件语句,通过在单个语句中声明所有条件来运行代码
- Python If 语句可以嵌套







