Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Python (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Statements and control flow=== Python's [[statement (computer science)|statements]] include the following: * The [[Assignment (computer science)|assignment]] statement, using a single equals sign <code>=</code> * The <code>[[if-then-else|if]]</code> statement, which conditionally executes a block of code, along with <code>else</code> and <code>elif</code> (a contraction of <code>else if</code>) * The <code>[[Foreach#Python|for]]</code> statement, which iterates over an ''iterable'' object, capturing each element to a local variable for use by the attached block * The <code>[[While loop#Python|while]]</code> statement, which executes a block of code as long as its condition is true * The <code>[[Exception handling syntax#Python|try]]</code> statement, which allows exceptions raised in its attached code block to be caught and handled by <code>except</code> clauses (or new syntax <code>except*</code> in Python 3.11 for exception groups<ref>{{Cite web |title=8. Errors and Exceptions – Python 3.12.0a0 documentation |url=https://docs.python.org/3.11/tutorial/errors.html |access-date=2022-05-09 |website=docs.python.org |archive-date=9 May 2022 |archive-url=https://web.archive.org/web/20220509145745/https://docs.python.org/3.11/tutorial/errors.html |url-status=live}}</ref>); the <code>try</code> statement also ensures that clean-up code in a <code>finally</code> block is always run regardless of how the block exits * The <code>raise</code> statement, used to raise a specified exception or re-raise a caught exception * The <code>class</code> statement, which executes a block of code and attaches its local namespace to a [[class (computer science)|class]], for use in object-oriented programming * The <code>def</code> statement, which defines a [[function (computing)|function]] or [[method (computing)|method]] * The <code>[[dispose pattern#Language constructs|with]]</code> statement, which encloses a code block within a context manager, allowing [[resource acquisition is initialization|resource-acquisition-is-initialization]] (RAII)-like behavior and replacing a common try/finally idiom<ref>{{cite web|url=https://www.python.org/download/releases/2.5/highlights/|title=Highlights: Python 2.5|website=Python.org|access-date=20 March 2018|archive-date=4 August 2019|archive-url=https://web.archive.org/web/20190804120408/https://www.python.org/download/releases/2.5/highlights/|url-status=live}}</ref> Examples of a context include acquiring a [[lock (computer science)|lock]] before some code is run, and then releasing the lock; or opening and then closing a [[Computer file|file]] * The <code>[[break statement|break]]</code> statement, which exits a loop * The <code>continue</code> statement, which skips the rest of the current iteration and continues with the next * The <code>del</code> statement, which removes a variable—deleting the reference from the name to the value, and producing an error if the variable is referred to before it is redefined {{efn|<code>del</code> in Python does not behave the same way <code>delete</code> in languages such as [[C++]] does, where such a word is used to call the [[Destructor (computer programming)|destructor]] and deallocate heap memory.}} * The <code>pass</code> statement, serving as a [[NOP (code)|NOP]] (i.e., no operation), which is syntactically needed to create an empty code block * The <code>[[assertion (programming)|assert]]</code> statement, used in debugging to check for conditions that should apply * The <code>yield</code> statement, which returns a value from a [[generator (computer programming)#Python|generator]] function (and also an operator); used to implement [[coroutine]]s * The <code>return</code> statement, used to return a value from a function * The <code>[[include directive|import]]</code> and <code>from</code> statements, used to import modules whose functions or variables can be used in the current program * The <code>match</code> and <code>case</code> statements, analogous to a [[switch statement]] construct, which compares an expression against one or more cases as a control-flow measure The assignment statement (<code>=</code>) binds a name as a [[pointer (computer programming)|reference]] to a separate, dynamically allocated [[object (computer science)|object]]. Variables may subsequently be rebound at any time to any object. In Python, a variable name is a generic reference holder without a fixed [[Type system|data type]]; however, it always refers to ''some'' object with a type. This is called [[Type system#Dynamic type checking and runtime type information|dynamic typing]]—in contrast to [[statically-typed]] languages, where each variable may contain only a value of a certain type. Python does not support [[tail call]] optimization or [[first-class continuations]]; according to Van Rossum, the language never will.<ref name="AutoNT-55"/><ref name="AutoNT-56"/> However, better support for [[coroutine]]-like functionality is provided by extending Python's generators.<ref name="AutoNT-57"/> Before 2.5, generators were [[lazy evaluation|lazy]] [[iterator]]s; data was passed unidirectionally out of the generator. From Python 2.5 on, it is possible to pass data back into a generator function; and from version 3.3, data can be passed through multiple stack levels.<ref name="AutoNT-58"/>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)