if_stmt ::= "if" assignment_expression ":" suite
("elif" assignment_expression ":" suite)*
["else" ":" suite]
用法:
if EXPRESSION1:
SUITE1
elif EXPRESSION2:
SUITE2
else:
SUITE
常用的操作符:
with_stmt ::= "with" ( "(" with_stmt_contents ","? ")" | with_stmt_contents ) ":" suite
with_stmt_contents ::= with_item ("," with_item)*
with_item ::= expression ["as" target]
用法:
with EXPRESSION as TARGET:
SUITE
或者
with A() as a, B() as b:
SUITE
或者
with A() as a:
with B() as b:
SUITE
或者
with (
A() as a,
B() as b,
):
SUITE
match_stmt ::= 'match' subject_expr ":" NEWLINE INDENT case_block+ DEDENT
subject_expr ::= star_named_expression "," star_named_expressions?
| named_expression
case_block ::= 'case' patterns [guard] ":" block
用法:
match variable: #這裏的variable是需要判斷的內容
case ["quit"]:
statement_block_1 # 對應案例的執行代碼,當variable="quit"時執行statement_block_1
case ["go", direction]:
statement_block_2
case ["drop", *objects]:
statement_block_3
... # 其他的case語句
case _: #如果上面的case語句沒有命中,則執行這個代碼塊,類似於Switch的default
statement_block_default