什么是Python中的条件语句?
Python中的条件语句根据特定的布尔约束评估为true还是false来执行不同的计算或操作。条件语句由Python中的IF语句处理。
在本教程中,我们将看到如何在Python中应用条件语句。
- 什么是If陈述?如何使用它?
- 当“如果条件”不满足时会发生什么
- 如何使用“其他条件”
- 当“其他条件”不起作用时
- 如何使用“ elif”条件
- 如何用最少的代码执行条件语句
- Python嵌套if语句
- 在Python中切换案例陈述
什么是Python If语句?
Python,如果将Statement用于决策操作。它包含一段代码,仅在if语句中给出的条件为true时运行。如果条件为假,则运行可选的else语句,其中包含else条件的一些代码。
如果您想证明一个条件的合理性而另一个条件不成立,则可以使用Python if else语句。
Python if语句语法:
如果表达式 语句 else 语句
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()
- Code Line 5:我们定义了两个变量x,y = 2,8
- Code Line 7: Python中的if语句检查条件x <y,在这种情况下为True
- Code Line 8: 变量st设置为“ x小于y”。
- Code Line 9:行输出st将输出变量st的值,”x is less than y”,
当“如果条件”不满足时会发生什么
在这一步中,我们将看到如果Python中的条件不满足会发生什么。
# # Example file for working with conditional statement # def main(): x, y = 8, 4 if (x < y): st = "x is less than y" print(st) if __name__ == "__main__": main()
- Code Line 5:我们定义了两个变量x,y = 2,8
- Code Line 7: Python中的if语句检查条件x <y,在这种情况下为false
- Code Line 8: 可变ST是NOT设置为“x是小于y”。
- Code Line 9: 试图打印从未声明过的变量的值。因此报错。
else使用
当您必须根据另一条陈述来判断另一条陈述时,通常使用“其他条件”。如果一个条件出错,那么应该有另一个条件可以证明该语句或逻辑是正确的。
范例:
# # 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 <y,在这种情况下为False
- 代码行9:程序控制的流程转到其他条件
- 代码行10:变量st设置为“ x大于y”。
- 代码行11:行输出st将输出变量st的值,即“ x大于y”,
当“else条件”不起作用时
在许多情况下,您的“其他条件”无法给您带来理想的结果。由于程序逻辑错误,它将打印出错误的结果。在大多数情况下,当您必须在程序中证明两个以上的语句或条件合理时,就会发生这种情况。
一个示例将更好地帮助您理解此概念。
在这里,两个变量都是相同的(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”条件
要更正先前由“其他条件”引起的错误,我们可以使用“ 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 <y,在这种情况下为False
- 代码行10:程序控制的流程转到elseif条件。它检查x == y是否为真
- 代码行11:变量st设置为“ x与y相同”。
- 代码行15:程序控制流退出if语句(它将不会到达else语句)。并打印变量st。输出是“ x与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”,否则将变量st设置为“ x大于或等于y”。在此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行,然后再次运行代码
在Python中切换案例陈述
什么是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 demo
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。
- “如果条件” –在其中一个条件为真或假时需要打印结果时使用。
- “其他条件”-当您的一种条件不符合要求时要打印出语句时使用
- “ elif条件” –当您有第三种可能性作为结果时使用。您可以使用多个elif条件来检查代码中第4,第5,第6的可能性
- 通过在单个语句中声明所有条件以运行代码,我们可以使用最少的代码来执行条件语句
- Python If语句可以嵌套