17db96d56Sopenharmony_ci 27db96d56Sopenharmony_ci.. _introduction: 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci************ 57db96d56Sopenharmony_ciIntroduction 67db96d56Sopenharmony_ci************ 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ciThis reference manual describes the Python programming language. It is not 97db96d56Sopenharmony_ciintended as a tutorial. 107db96d56Sopenharmony_ci 117db96d56Sopenharmony_ciWhile I am trying to be as precise as possible, I chose to use English rather 127db96d56Sopenharmony_cithan formal specifications for everything except syntax and lexical analysis. 137db96d56Sopenharmony_ciThis should make the document more understandable to the average reader, but 147db96d56Sopenharmony_ciwill leave room for ambiguities. Consequently, if you were coming from Mars and 157db96d56Sopenharmony_citried to re-implement Python from this document alone, you might have to guess 167db96d56Sopenharmony_cithings and in fact you would probably end up implementing quite a different 177db96d56Sopenharmony_cilanguage. On the other hand, if you are using Python and wonder what the precise 187db96d56Sopenharmony_cirules about a particular area of the language are, you should definitely be able 197db96d56Sopenharmony_cito find them here. If you would like to see a more formal definition of the 207db96d56Sopenharmony_cilanguage, maybe you could volunteer your time --- or invent a cloning machine 217db96d56Sopenharmony_ci:-). 227db96d56Sopenharmony_ci 237db96d56Sopenharmony_ciIt is dangerous to add too many implementation details to a language reference 247db96d56Sopenharmony_cidocument --- the implementation may change, and other implementations of the 257db96d56Sopenharmony_cisame language may work differently. On the other hand, CPython is the one 267db96d56Sopenharmony_ciPython implementation in widespread use (although alternate implementations 277db96d56Sopenharmony_cicontinue to gain support), and its particular quirks are sometimes worth being 287db96d56Sopenharmony_cimentioned, especially where the implementation imposes additional limitations. 297db96d56Sopenharmony_ciTherefore, you'll find short "implementation notes" sprinkled throughout the 307db96d56Sopenharmony_citext. 317db96d56Sopenharmony_ci 327db96d56Sopenharmony_ciEvery Python implementation comes with a number of built-in and standard 337db96d56Sopenharmony_cimodules. These are documented in :ref:`library-index`. A few built-in modules 347db96d56Sopenharmony_ciare mentioned when they interact in a significant way with the language 357db96d56Sopenharmony_cidefinition. 367db96d56Sopenharmony_ci 377db96d56Sopenharmony_ci 387db96d56Sopenharmony_ci.. _implementations: 397db96d56Sopenharmony_ci 407db96d56Sopenharmony_ciAlternate Implementations 417db96d56Sopenharmony_ci========================= 427db96d56Sopenharmony_ci 437db96d56Sopenharmony_ciThough there is one Python implementation which is by far the most popular, 447db96d56Sopenharmony_cithere are some alternate implementations which are of particular interest to 457db96d56Sopenharmony_cidifferent audiences. 467db96d56Sopenharmony_ci 477db96d56Sopenharmony_ciKnown implementations include: 487db96d56Sopenharmony_ci 497db96d56Sopenharmony_ciCPython 507db96d56Sopenharmony_ci This is the original and most-maintained implementation of Python, written in C. 517db96d56Sopenharmony_ci New language features generally appear here first. 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ciJython 547db96d56Sopenharmony_ci Python implemented in Java. This implementation can be used as a scripting 557db96d56Sopenharmony_ci language for Java applications, or can be used to create applications using the 567db96d56Sopenharmony_ci Java class libraries. It is also often used to create tests for Java libraries. 577db96d56Sopenharmony_ci More information can be found at `the Jython website <https://www.jython.org/>`_. 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ciPython for .NET 607db96d56Sopenharmony_ci This implementation actually uses the CPython implementation, but is a managed 617db96d56Sopenharmony_ci .NET application and makes .NET libraries available. It was created by Brian 627db96d56Sopenharmony_ci Lloyd. For more information, see the `Python for .NET home page 637db96d56Sopenharmony_ci <https://pythonnet.github.io/>`_. 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ciIronPython 667db96d56Sopenharmony_ci An alternate Python for .NET. Unlike Python.NET, this is a complete Python 677db96d56Sopenharmony_ci implementation that generates IL, and compiles Python code directly to .NET 687db96d56Sopenharmony_ci assemblies. It was created by Jim Hugunin, the original creator of Jython. For 697db96d56Sopenharmony_ci more information, see `the IronPython website <https://ironpython.net/>`_. 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ciPyPy 727db96d56Sopenharmony_ci An implementation of Python written completely in Python. It supports several 737db96d56Sopenharmony_ci advanced features not found in other implementations like stackless support 747db96d56Sopenharmony_ci and a Just in Time compiler. One of the goals of the project is to encourage 757db96d56Sopenharmony_ci experimentation with the language itself by making it easier to modify the 767db96d56Sopenharmony_ci interpreter (since it is written in Python). Additional information is 777db96d56Sopenharmony_ci available on `the PyPy project's home page <https://www.pypy.org/>`_. 787db96d56Sopenharmony_ci 797db96d56Sopenharmony_ciEach of these implementations varies in some way from the language as documented 807db96d56Sopenharmony_ciin this manual, or introduces specific information beyond what's covered in the 817db96d56Sopenharmony_cistandard Python documentation. Please refer to the implementation-specific 827db96d56Sopenharmony_cidocumentation to determine what else you need to know about the specific 837db96d56Sopenharmony_ciimplementation you're using. 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci.. _notation: 877db96d56Sopenharmony_ci 887db96d56Sopenharmony_ciNotation 897db96d56Sopenharmony_ci======== 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ci.. index:: BNF, grammar, syntax, notation 927db96d56Sopenharmony_ci 937db96d56Sopenharmony_ciThe descriptions of lexical analysis and syntax use a modified BNF grammar 947db96d56Sopenharmony_cinotation. This uses the following style of definition: 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci.. productionlist:: notation 977db96d56Sopenharmony_ci name: `lc_letter` (`lc_letter` | "_")* 987db96d56Sopenharmony_ci lc_letter: "a"..."z" 997db96d56Sopenharmony_ci 1007db96d56Sopenharmony_ciThe first line says that a ``name`` is an ``lc_letter`` followed by a sequence 1017db96d56Sopenharmony_ciof zero or more ``lc_letter``\ s and underscores. An ``lc_letter`` in turn is 1027db96d56Sopenharmony_ciany of the single characters ``'a'`` through ``'z'``. (This rule is actually 1037db96d56Sopenharmony_ciadhered to for the names defined in lexical and grammar rules in this document.) 1047db96d56Sopenharmony_ci 1057db96d56Sopenharmony_ciEach rule begins with a name (which is the name defined by the rule) and 1067db96d56Sopenharmony_ci``::=``. A vertical bar (``|``) is used to separate alternatives; it is the 1077db96d56Sopenharmony_cileast binding operator in this notation. A star (``*``) means zero or more 1087db96d56Sopenharmony_cirepetitions of the preceding item; likewise, a plus (``+``) means one or more 1097db96d56Sopenharmony_cirepetitions, and a phrase enclosed in square brackets (``[ ]``) means zero or 1107db96d56Sopenharmony_cione occurrences (in other words, the enclosed phrase is optional). The ``*`` 1117db96d56Sopenharmony_ciand ``+`` operators bind as tightly as possible; parentheses are used for 1127db96d56Sopenharmony_cigrouping. Literal strings are enclosed in quotes. White space is only 1137db96d56Sopenharmony_cimeaningful to separate tokens. Rules are normally contained on a single line; 1147db96d56Sopenharmony_cirules with many alternatives may be formatted alternatively with each line after 1157db96d56Sopenharmony_cithe first beginning with a vertical bar. 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci.. index:: lexical definitions, ASCII 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ciIn lexical definitions (as the example above), two more conventions are used: 1207db96d56Sopenharmony_ciTwo literal characters separated by three dots mean a choice of any single 1217db96d56Sopenharmony_cicharacter in the given (inclusive) range of ASCII characters. A phrase between 1227db96d56Sopenharmony_ciangular brackets (``<...>``) gives an informal description of the symbol 1237db96d56Sopenharmony_cidefined; e.g., this could be used to describe the notion of 'control character' 1247db96d56Sopenharmony_ciif needed. 1257db96d56Sopenharmony_ci 1267db96d56Sopenharmony_ciEven though the notation used is almost the same, there is a big difference 1277db96d56Sopenharmony_cibetween the meaning of lexical and syntactic definitions: a lexical definition 1287db96d56Sopenharmony_cioperates on the individual characters of the input source, while a syntax 1297db96d56Sopenharmony_cidefinition operates on the stream of tokens generated by the lexical analysis. 1307db96d56Sopenharmony_ciAll uses of BNF in the next chapter ("Lexical Analysis") are lexical 1317db96d56Sopenharmony_cidefinitions; uses in subsequent chapters are syntactic definitions. 1327db96d56Sopenharmony_ci 133