教程:
高能:语句结构都是由关键字开头,用冒号结束!
一:语句结构
if condition_1:
statement_block_1
elif condition_2:
statement_block_2
else:
statement_block_3
二:基本规则
(1)使用缩进来划分语句块,相同缩进数的语句在一起组成一个语句块。
(2)在Python中没有switch – case语句。
三:条件为真
不为 0, True, 'None', 字符串不为空串
四:可以括号限定代码域,加强代码可读性。
五:if条件的嵌套结构
CODE:
# -----------------------------------------------------------------------------------------------------#
# 条件结构if
# -----------------------------------------------------------------------------------------------------# a = 0
b = 1
if (a > 0) and (b / a > 2):
print("yes")
else:
print("no")
# -----------------------------------------------------------------------------------------------------#
# 条件结构加上括号,提高代码可读性
# -----------------------------------------------------------------------------------------------------# name = "pag"
if name == "pag":
print(name == "pag") # True if (name == "pag"):{
print(name == "pag") # True
} # -----------------------------------------------------------------------------------------------------#
# 条件结构的嵌套
# -----------------------------------------------------------------------------------------------------# name = "pag"
if name == "pag":
print(name == "pag") # True
if name == "pag1":
print(name == "pag1") # True
else:
print(name == "pag2") # True
else:
print(name == "pag3") # True