|
|
| ================================================================================DOMAIN_URL: docs.python.org |
| PAGE_URL: https://docs.python.org/3.7/reference/compound_stmts.html |
| TITLE: 8. Compound statements — Python 3.7.17 documentation |
| CRAWLED: 2026-06-07T21:58:41Z |
| CATEGORY: technology |
| RELEVANCE_SCORE: 88.00 |
| WORD_COUNT: 3243 |
|
|
| KEYWORDS: exception, suite, executed, clause, statements, item, except, one, loop, expression, thefinallyclause, compound, execution, sequence, statement |
|
|
| IMAGES (2): |
| - URL: https://docs.python.org/3.7/_static/py.png |
| - URL: https://docs.python.org/3.7/_static/py.png |
|
|
| INTERNAL_LINKS (50): |
| - https://docs.python.org/3/reference/compound_stmts.html |
| - https://docs.python.org/3.7/genindex.html |
| - https://docs.python.org/3.7/py-modindex.html |
| - https://docs.python.org/3.7/reference/toplevel_components.html |
| - https://docs.python.org/3.7/reference/simple_stmts.html |
| - https://python.org/ |
| - https://docs.python.org/3.7/index.html |
| - https://docs.python.org/3.7/reference/index.html |
| - https://docs.python.org/3.7/library/functions.html |
| - https://docs.python.org/3.7/reference/simple_stmts.html |
| - https://docs.python.org/3.7/reference/simple_stmts.html |
| - https://docs.python.org/3.7/reference/expressions.html |
| - https://docs.python.org/3.7/reference/expressions.html |
| - https://docs.python.org/3.7/reference/expressions.html |
| - https://docs.python.org/3.7/reference/expressions.html |
| - https://docs.python.org/3.7/reference/simple_stmts.html |
| - https://docs.python.org/3.7/reference/simple_stmts.html |
| - https://docs.python.org/3.7/reference/simple_stmts.html |
| - https://docs.python.org/3.7/reference/expressions.html |
| - https://docs.python.org/3.7/reference/simple_stmts.html |
|
|
| EXTERNAL_LINKS (2): |
| - https://github.com/python/cpython/blob/3.7/Doc/reference/compound_stmts.rst |
| - http://sphinx.pocoo.org/ |
|
|
| METADATA: |
| - generator: Docutils 0.17.1: http://docutils.sourceforge.net/ |
|
|
| CONTENT: |
| EnglishSpanish | españolFrench | françaisItalian | italianoJapanese | 日本語Korean | 한국어Polish | polskiBrazilian Portuguese | Português brasileiroTurkish | TürkçeSimplified Chinese | 简体中文Traditional Chinese | 繁體中文dev (3.14)3.133.123.113.103.93.83.7.173.63.53.43.33.23.13.02.72.6 |
|
|
| Compound statements contain (groups of) other statements; they affect or control |
| the execution of those other statements in some way. In general, compound |
| statements span multiple lines, although in simple incarnations a whole compound |
| statement may be contained in one line. |
|
|
| Theif,whileandforstatements implement |
| traditional control flow constructs.tryspecifies exception |
| handlers and/or cleanup code for a group of statements, while thewithstatement allows the execution of initialization and |
| finalization code around a block of code. Function and class definitions are |
| also syntactically compound statements. |
|
|
| A compound statement consists of one or more ‘clauses.’ A clause consists of a |
| header and a ‘suite.’ The clause headers of a particular compound statement are |
| all at the same indentation level. Each clause header begins with a uniquely |
| identifying keyword and ends with a colon. A suite is a group of statements |
| controlled by a clause. A suite can be one or more semicolon-separated simple |
| statements on the same line as the header, following the header’s colon, or it |
| can be one or more indented statements on subsequent lines. Only the latter |
| form of a suite can contain nested compound statements; the following is illegal, |
| mostly because it wouldn’t be clear to whichifclause a followingelseclause would belong: |
|
|
| Also note that the semicolon binds tighter than the colon in this context, so |
| that in the following example, either all or none of theprint()calls are |
| executed: |
|
|
| Note that statements always end in aNEWLINEpossibly followed by aDEDENT. Also note that optional continuation clauses always begin with a |
| keyword that cannot start a statement, thus there are no ambiguities (the |
| ‘danglingelse’ problem is solved in Python by requiring nestedifstatements to be indented). |
|
|
| The formatting of the grammar rules in the following sections places each clause |
| on a separate line for clarity. |
|
|
| Theifstatement is used for conditional execution: |
|
|
| It selects exactly one of the suites by evaluating the expressions one by one |
| until one is found to be true (see sectionBoolean operationsfor the definition of |
| true and false); then that suite is executed (and no other part of theifstatement is executed or evaluated). If all expressions are |
| false, the suite of theelseclause, if present, is executed. |
|
|
| Thewhilestatement is used for repeated execution as long as an |
| expression is true: |
|
|
| This repeatedly tests the expression and, if it is true, executes the first |
| suite; if the expression is false (which may be the first time it is tested) the |
| suite of theelseclause, if present, is executed and the loop |
| terminates. |
|
|
| Abreakstatement executed in the first suite terminates the loop |
| without executing t |
| ================================================================================ |
|
|
|
|