Python: Difference between revisions

From EMC23 - Satellite Of Love
Jump to navigation Jump to search
mNo edit summary
Line 10: Line 10:


===Statements and control flow===
===Statements and control flow===
Python's [[Statement (computer science)|statements]] include (among others):
Python's Statement include (among others):
* The [[Assignment (computer science)|assignment]] statement, using a single equals sign <code>=</code>.
* 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 else-if).
* 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 else-if).
* 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>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>[[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; it also ensures that clean-up code in a <code>finally</code> block will always be run regardless of how the block exits.
 
* 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; it also ensures that clean-up code in a <code>finally</code> block will always be 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>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>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>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 (for example, acquiring a [[lock (computer science)|lock]] before the block of code is run and releasing the lock afterwards, or opening a [[Computer file|file]] and then closing it), allowing [[resource acquisition is initialization|resource-acquisition-is-initialization]] (RAII)-like behavior and replaces 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>
 
* The <code>dispose pattern#Language constructs|with</code> statement, which encloses a code block within a context manager (for example, acquiring a [[lock (computer science)|lock]] before the block of code is run and releasing the lock afterwards, or opening a [[Computer file|file]] and then closing it), allowing [[resource acquisition is initialization|resource-acquisition-is-initialization]] (RAII)-like behavior and replaces 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>
* The [[break statement|<code>break</code>]] statement, exits from a loop.
* The [[break statement|<code>break</code>]] statement, exits from a loop.
* The <code>continue</code> statement, skips this iteration and continues with the next item.
* The <code>continue</code> statement, skips this iteration and continues with the next item.
* The <code>del</code> statement, removes a variable, which means the reference from the name to the value is deleted and trying to use that variable will cause an error. A deleted variable can be reassigned.
* The <code>del</code> statement, removes a variable, which means the reference from the name to the value is deleted and trying to use that variable will cause an error. A deleted variable can be reassigned.
* The <code>pass</code> statement, which serves as a [[NOP (code)|NOP]]. It is syntactically needed to create an empty code block.
* The <code>pass</code> statement, which serves as a [[NOP (code)|NOP]]. It is syntactically needed to create an empty code block.
* The <code>[[assertion (programming)|assert]]</code> statement, used during debugging to check for conditions that should apply.
* The <code>assertion (programming)|assert</code> statement, used during 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 <code>yield</code> is also an operator. This form is used to implement [[coroutine]]s.
* The <code>yield</code> statement, which returns a value from a generator (computer programming)#Python|generator function and <code>yield</code> is also an operator. This form is used to implement coroutines.
* The <code>return</code> statement, used to return a value from a function.
* The <code>return</code> statement, used to return a value from a function.
* The <code>[[include directive|import]]</code> statement, which is used to import modules whose functions or variables can be used in the current program.
* The <code>include directive|import</code> statement, which is used to import modules whose functions or variables can be used in the current program.
 
The assignment statement (<code>=</code>) operates by binding a name as a [[pointer (computer programming)|reference]] to a separate, dynamically-allocated [[object (computer science)|object]]. Variables may be subsequently rebound at any time to any object. In Python, a variable name is a generic reference holder and does not have a fixed [[Type system|data type]] associated with it. However, at a given time, a variable will refer to ''some'' object, which will have a type. This is referred to as [[dynamic type|dynamic typing]] and is contrasted with [[statically-typed]] programming languages, where each variable may only contain values of a certain type.
 
Python does not support [[tail call]] optimization or [[first-class continuations]], and, according to Guido van Rossum, it never will.<ref name="AutoNT-55" /><ref name="AutoNT-56" /> However, better support for [[coroutine]]-like functionality is provided, by extending Python's [[generator (computer programming)|generators]].<ref name="AutoNT-57" /> Before 2.5, generators were [[lazy evaluation|lazy]] [[iterator]]s; information was passed unidirectionally out of the generator. From Python&nbsp;2.5, it is possible to pass information back into a generator function, and from Python&nbsp;3.3, the information can be passed through multiple stack levels.<ref name="AutoNT-58" />

Revision as of 21:02, 25 August 2021

Python is an interpreted high-level general-purpose programming language. Its design philosophy emphasizes code readability with its use of significant indentation. Its language constructs as well as its object-oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

The standard prrogramming language for data science and for us especially:

  • audio analysis and manipulation using librosa

Also used to interface with 3d software such as poser 3d/blender and audacity and Libre Office.

Statements and control flow[edit]

Python's Statement include (among others):

  • The Assignment (computer science)|assignment statement, using a single equals sign =.
  • The if-then-else|if statement, which conditionally executes a block of code, along with else and elif (a contraction of else-if).
  • The Foreach#Python|for]] statement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.
  • The While loop#Python|while]] statement, which executes a block of code as long as its condition is true.
  • The Exception handling syntax#Python|try]] statement, which allows exceptions raised in its attached code block to be caught and handled by except clauses; it also ensures that clean-up code in a finally block will always be run regardless of how the block exits.
  • The raise statement, used to raise a specified exception or re-raise a caught exception.
  • The class 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 def statement, which defines a function or method.
  • The dispose pattern#Language constructs|with statement, which encloses a code block within a context manager (for example, acquiring a lock before the block of code is run and releasing the lock afterwards, or opening a file and then closing it), allowing resource-acquisition-is-initialization (RAII)-like behavior and replaces a common try/finally idiom.[1]
  • The break statement, exits from a loop.
  • The continue statement, skips this iteration and continues with the next item.
  • The del statement, removes a variable, which means the reference from the name to the value is deleted and trying to use that variable will cause an error. A deleted variable can be reassigned.
  • The pass statement, which serves as a NOP. It is syntactically needed to create an empty code block.
  • The assertion (programming)|assert statement, used during debugging to check for conditions that should apply.
  • The yield statement, which returns a value from a generator (computer programming)#Python|generator function and yield is also an operator. This form is used to implement coroutines.
  • The return statement, used to return a value from a function.
  • The include directive|import statement, which is used to import modules whose functions or variables can be used in the current program.
  1. "Highlights: Python 2.5". Python.org. Archived from the original on 4 August 2019. Retrieved 20 March 2018.