程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python3學習筆記--條件控制用法整理

編輯:Python

if

if_stmt ::= "if" assignment_expression ":" suite
("elif" assignment_expression ":" suite)*
["else" ":" suite]

用法:

if EXPRESSION1:
SUITE1
elif EXPRESSION2:
SUITE2
else:
SUITE

常用的操作符:

  • “<”:小於
  • “<=”:小於等於
  • “>”:大於
  • “>=”:大於等於
  • “==”:等於
  • “!=”:不等於
  • “and”:並且
  • “or”:或者

with

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(3.10新特性)

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved