diff options
Diffstat (limited to 'devdocs/python~3.12/tutorial%2Finterpreter.html')
| -rw-r--r-- | devdocs/python~3.12/tutorial%2Finterpreter.html | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/devdocs/python~3.12/tutorial%2Finterpreter.html b/devdocs/python~3.12/tutorial%2Finterpreter.html new file mode 100644 index 00000000..c0a9eaf3 --- /dev/null +++ b/devdocs/python~3.12/tutorial%2Finterpreter.html @@ -0,0 +1,29 @@ + <span id="tut-using"></span><h1> Using the Python Interpreter</h1> <section id="invoking-the-interpreter"> <span id="tut-invoking"></span><h2> +<span class="section-number">2.1. </span>Invoking the Interpreter</h2> <p>The Python interpreter is usually installed as <code>/usr/local/bin/python3.12</code> on those machines where it is available; putting <code>/usr/local/bin</code> in your Unix shell’s search path makes it possible to start it by typing the command:</p> <pre data-language="text">python3.12 +</pre> <p>to the shell. <a class="footnote-reference brackets" href="#id2" id="id1">1</a> Since the choice of the directory where the interpreter lives is an installation option, other places are possible; check with your local Python guru or system administrator. (E.g., <code>/usr/local/python</code> is a popular alternative location.)</p> <p>On Windows machines where you have installed Python from the <a class="reference internal" href="../using/windows#windows-store"><span class="std std-ref">Microsoft Store</span></a>, the <code>python3.12</code> command will be available. If you have the <a class="reference internal" href="../using/windows#launcher"><span class="std std-ref">py.exe launcher</span></a> installed, you can use the <code>py</code> command. See <a class="reference internal" href="../using/windows#setting-envvars"><span class="std std-ref">Excursus: Setting environment variables</span></a> for other ways to launch Python.</p> <p>Typing an end-of-file character (<kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">D</kbd></kbd> on Unix, <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">Z</kbd></kbd> on Windows) at the primary prompt causes the interpreter to exit with a zero exit status. If that doesn’t work, you can exit the interpreter by typing the following command: <code>quit()</code>.</p> <p>The interpreter’s line-editing features include interactive editing, history substitution and code completion on systems that support the <a class="reference external" href="https://tiswww.case.edu/php/chet/readline/rltop.html">GNU Readline</a> library. Perhaps the quickest check to see whether command line editing is supported is typing <kbd class="kbd compound docutils literal notranslate"><kbd class="kbd docutils literal notranslate">Control</kbd>-<kbd class="kbd docutils literal notranslate">P</kbd></kbd> to the first Python prompt you get. If it beeps, you have command line editing; see Appendix <a class="reference internal" href="interactive#tut-interacting"><span class="std std-ref">Interactive Input Editing and History Substitution</span></a> for an introduction to the keys. If nothing appears to happen, or if <code>^P</code> is echoed, command line editing isn’t available; you’ll only be able to use backspace to remove characters from the current line.</p> <p>The interpreter operates somewhat like the Unix shell: when called with standard input connected to a tty device, it reads and executes commands interactively; when called with a file name argument or with a file as standard input, it reads and executes a <em>script</em> from that file.</p> <p>A second way of starting the interpreter is <code>python -c command [arg] ...</code>, which executes the statement(s) in <em>command</em>, analogous to the shell’s <a class="reference internal" href="../using/cmdline#cmdoption-c"><code>-c</code></a> option. Since Python statements often contain spaces or other characters that are special to the shell, it is usually advised to quote <em>command</em> in its entirety.</p> <p>Some Python modules are also useful as scripts. These can be invoked using <code>python -m module [arg] ...</code>, which executes the source file for <em>module</em> as if you had spelled out its full name on the command line.</p> <p>When a script file is used, it is sometimes useful to be able to run the script and enter interactive mode afterwards. This can be done by passing <a class="reference internal" href="../using/cmdline#cmdoption-i"><code>-i</code></a> before the script.</p> <p>All command line options are described in <a class="reference internal" href="../using/cmdline#using-on-general"><span class="std std-ref">Command line and environment</span></a>.</p> <section id="argument-passing"> <span id="tut-argpassing"></span><h3> +<span class="section-number">2.1.1. </span>Argument Passing</h3> <p>When known to the interpreter, the script name and additional arguments thereafter are turned into a list of strings and assigned to the <code>argv</code> variable in the <code>sys</code> module. You can access this list by executing <code>import +sys</code>. The length of the list is at least one; when no script and no arguments are given, <code>sys.argv[0]</code> is an empty string. When the script name is given as <code>'-'</code> (meaning standard input), <code>sys.argv[0]</code> is set to <code>'-'</code>. When <a class="reference internal" href="../using/cmdline#cmdoption-c"><code>-c</code></a> <em>command</em> is used, <code>sys.argv[0]</code> is set to <code>'-c'</code>. When <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> <em>module</em> is used, <code>sys.argv[0]</code> is set to the full name of the located module. Options found after <a class="reference internal" href="../using/cmdline#cmdoption-c"><code>-c</code></a> <em>command</em> or <a class="reference internal" href="../using/cmdline#cmdoption-m"><code>-m</code></a> <em>module</em> are not consumed by the Python interpreter’s option processing but left in <code>sys.argv</code> for the command or module to handle.</p> </section> <section id="interactive-mode"> <span id="tut-interactive"></span><h3> +<span class="section-number">2.1.2. </span>Interactive Mode</h3> <p>When commands are read from a tty, the interpreter is said to be in <em>interactive mode</em>. In this mode it prompts for the next command with the <em>primary prompt</em>, usually three greater-than signs (<code>>>></code>); for continuation lines it prompts with the <em>secondary prompt</em>, by default three dots (<code>...</code>). The interpreter prints a welcome message stating its version number and a copyright notice before printing the first prompt:</p> <pre data-language="shell">$ python3.12 +Python 3.12 (default, April 4 2022, 09:25:04) +[GCC 10.2.0] on linux +Type "help", "copyright", "credits" or "license" for more information. +>>> +</pre> <p>Continuation lines are needed when entering a multi-line construct. As an example, take a look at this <a class="reference internal" href="../reference/compound_stmts#if"><code>if</code></a> statement:</p> <pre data-language="python">>>> the_world_is_flat = True +>>> if the_world_is_flat: +... print("Be careful not to fall off!") +... +Be careful not to fall off! +</pre> <p>For more on interactive mode, see <a class="reference internal" href="appendix#tut-interac"><span class="std std-ref">Interactive Mode</span></a>.</p> </section> </section> <section id="the-interpreter-and-its-environment"> <span id="tut-interp"></span><h2> +<span class="section-number">2.2. </span>The Interpreter and Its Environment</h2> <section id="source-code-encoding"> <span id="tut-source-encoding"></span><h3> +<span class="section-number">2.2.1. </span>Source Code Encoding</h3> <p>By default, Python source files are treated as encoded in UTF-8. In that encoding, characters of most languages in the world can be used simultaneously in string literals, identifiers and comments — although the standard library only uses ASCII characters for identifiers, a convention that any portable code should follow. To display all these characters properly, your editor must recognize that the file is UTF-8, and it must use a font that supports all the characters in the file.</p> <p>To declare an encoding other than the default one, a special comment line should be added as the <em>first</em> line of the file. The syntax is as follows:</p> <pre data-language="python"># -*- coding: encoding -*- +</pre> <p>where <em>encoding</em> is one of the valid <a class="reference internal" href="../library/codecs#module-codecs" title="codecs: Encode and decode data and streams."><code>codecs</code></a> supported by Python.</p> <p>For example, to declare that Windows-1252 encoding is to be used, the first line of your source code file should be:</p> <pre data-language="python"># -*- coding: cp1252 -*- +</pre> <p>One exception to the <em>first line</em> rule is when the source code starts with a <a class="reference internal" href="appendix#tut-scripts"><span class="std std-ref">UNIX “shebang” line</span></a>. In this case, the encoding declaration should be added as the second line of the file. For example:</p> <pre data-language="python">#!/usr/bin/env python3 +# -*- coding: cp1252 -*- +</pre> <h4 class="rubric">Footnotes</h4> <dl class="footnote brackets"> <dt class="label" id="id2"> +<code>1</code> </dt> <dd> +<p>On Unix, the Python 3.x interpreter is by default not installed with the executable named <code>python</code>, so that it does not conflict with a simultaneously installed Python 2.x executable.</p> </dd> </dl> </section> </section> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/tutorial/interpreter.html" class="_attribution-link">https://docs.python.org/3.12/tutorial/interpreter.html</a> + </p> +</div> |
