Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1510
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1515
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1537
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1538
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1539
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1510
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1515
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1537
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1538
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1539
什么是条件语句?
Python中的条件语句,是根据条件表达式为true还是false来执行不同的计算或操作。
条件语句叫“IF语句”。
什么是If语句?
在Python中,仅当IF语句为true时,它才会运行代码体。
当您想根据一个条件进行判断时,则使用“ if语句”。
句法:
if expression
Statement
else
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()
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 is less than y ”,否则将其设置为“ x is greater than or equal to y”。 现在结果是x> y,所以变量st设置为“ x is greater than or equal to y”。
代码行4:打印st的值并提供正确的输出
Python无需为条件语句编写冗长的代码,而是使您可以自由地以简洁的方式编写代码。
嵌套的IF语句
下面的示例演示嵌套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")
Switch语句
什么是switch语句?
switch语句是一个多路分支语句,它将变量的值与case语句中指定的值进行比较。
Python语言没有switch语句。
Python使用字典映射在Python中实现switch语句
Example
对于上面的switch语句Python中的替换是:
阅读: 589
Warning: Trying to access array offset on null in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\includes\content-single.php on line 281
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1639
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1639
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1639
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1639
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1639
Warning: Trying to access array offset on false in D:\Apache24\htdocs\yippeex_wordpress\wp-content\themes\accountant\functions\theme-functions.php on line 1639