Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 3.138.135.4
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
tutorial /
[ HOME SHELL ]
Name
Size
Permission
Action
appetite.html
10.28
KB
-rw-r--r--
classes.html
77.01
KB
-rw-r--r--
controlflow.html
69.18
KB
-rw-r--r--
datastructures.html
82.82
KB
-rw-r--r--
errors.html
45.4
KB
-rw-r--r--
floatingpoint.html
20.59
KB
-rw-r--r--
index.html
24.28
KB
-rw-r--r--
inputoutput.html
48.57
KB
-rw-r--r--
interactive.html
17.99
KB
-rw-r--r--
interpreter.html
26.6
KB
-rw-r--r--
introduction.html
58.9
KB
-rw-r--r--
modules.html
55.63
KB
-rw-r--r--
stdlib.html
40.52
KB
-rw-r--r--
stdlib2.html
53.09
KB
-rw-r--r--
whatnow.html
9.69
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : introduction.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>3. An Informal Introduction to Python — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="The Python Tutorial" href="index.html" /> <link rel="next" title="4. More Control Flow Tools" href="controlflow.html" /> <link rel="prev" title="2. Using the Python Interpreter" href="interpreter.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="controlflow.html" title="4. More Control Flow Tools" accesskey="N">next</a> |</li> <li class="right" > <a href="interpreter.html" title="2. Using the Python Interpreter" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" accesskey="U">The Python Tutorial</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="an-informal-introduction-to-python"> <span id="tut-informal"></span><h1>3. An Informal Introduction to Python<a class="headerlink" href="#an-informal-introduction-to-python" title="Permalink to this headline">¶</a></h1> <p>In the following examples, input and output are distinguished by the presence or absence of prompts (<tt class="docutils literal"><span class="pre">>>></span></tt> and <tt class="docutils literal"><span class="pre">...</span></tt>): to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter. Note that a secondary prompt on a line by itself in an example means you must type a blank line; this is used to end a multi-line command.</p> <p>Many of the examples in this manual, even those entered at the interactive prompt, include comments. Comments in Python start with the hash character, <tt class="docutils literal"><span class="pre">#</span></tt>, and extend to the end of the physical line. A comment may appear at the start of a line or following whitespace or code, but not within a string literal. A hash character within a string literal is just a hash character. Since comments are to clarify code and are not interpreted by Python, they may be omitted when typing in examples.</p> <p>Some examples:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># this is the first comment</span> <span class="n">SPAM</span> <span class="o">=</span> <span class="mi">1</span> <span class="c"># and this is the second comment</span> <span class="c"># ... and now a third!</span> <span class="n">STRING</span> <span class="o">=</span> <span class="s">"# This is not a comment."</span> </pre></div> </div> <div class="section" id="using-python-as-a-calculator"> <span id="tut-calculator"></span><h2>3.1. Using Python as a Calculator<a class="headerlink" href="#using-python-as-a-calculator" title="Permalink to this headline">¶</a></h2> <p>Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt, <tt class="docutils literal"><span class="pre">>>></span></tt>. (It shouldn’t take long.)</p> <div class="section" id="numbers"> <span id="tut-numbers"></span><h3>3.1.1. Numbers<a class="headerlink" href="#numbers" title="Permalink to this headline">¶</a></h3> <p>The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators <tt class="docutils literal"><span class="pre">+</span></tt>, <tt class="docutils literal"><span class="pre">-</span></tt>, <tt class="docutils literal"><span class="pre">*</span></tt> and <tt class="docutils literal"><span class="pre">/</span></tt> work just like in most other languages (for example, Pascal or C); parentheses can be used for grouping. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span> <span class="go">4</span> <span class="gp">>>> </span><span class="c"># This is a comment</span> <span class="gp">... </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span> <span class="go">4</span> <span class="gp">>>> </span><span class="mi">2</span><span class="o">+</span><span class="mi">2</span> <span class="c"># and a comment on the same line as code</span> <span class="go">4</span> <span class="gp">>>> </span><span class="p">(</span><span class="mi">50</span><span class="o">-</span><span class="mi">5</span><span class="o">*</span><span class="mi">6</span><span class="p">)</span><span class="o">/</span><span class="mi">4</span> <span class="go">5</span> <span class="gp">>>> </span><span class="c"># Integer division returns the floor:</span> <span class="gp">... </span><span class="mi">7</span><span class="o">/</span><span class="mi">3</span> <span class="go">2</span> <span class="gp">>>> </span><span class="mi">7</span><span class="o">/-</span><span class="mi">3</span> <span class="go">-3</span> </pre></div> </div> <p>The equal sign (<tt class="docutils literal"><span class="pre">'='</span></tt>) is used to assign a value to a variable. Afterwards, no result is displayed before the next interactive prompt:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">width</span> <span class="o">=</span> <span class="mi">20</span> <span class="gp">>>> </span><span class="n">height</span> <span class="o">=</span> <span class="mi">5</span><span class="o">*</span><span class="mi">9</span> <span class="gp">>>> </span><span class="n">width</span> <span class="o">*</span> <span class="n">height</span> <span class="go">900</span> </pre></div> </div> <p>A value can be assigned to several variables simultaneously:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">0</span> <span class="c"># Zero x, y and z</span> <span class="gp">>>> </span><span class="n">x</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">y</span> <span class="go">0</span> <span class="gp">>>> </span><span class="n">z</span> <span class="go">0</span> </pre></div> </div> <p>Variables must be “defined” (assigned a value) before they can be used, or an error will occur:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">n</span> <span class="c"># try to access an undefined variable</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n"><module></span> <span class="gr">NameError</span>: <span class="n">name 'n' is not defined</span> </pre></div> </div> <p>There is full support for floating point; operators with mixed type operands convert the integer operand to floating point:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">3</span> <span class="o">*</span> <span class="mf">3.75</span> <span class="o">/</span> <span class="mf">1.5</span> <span class="go">7.5</span> <span class="gp">>>> </span><span class="mf">7.0</span> <span class="o">/</span> <span class="mi">2</span> <span class="go">3.5</span> </pre></div> </div> <p>Complex numbers are also supported; imaginary numbers are written with a suffix of <tt class="docutils literal"><span class="pre">j</span></tt> or <tt class="docutils literal"><span class="pre">J</span></tt>. Complex numbers with a nonzero real component are written as <tt class="docutils literal"><span class="pre">(real+imagj)</span></tt>, or can be created with the <tt class="docutils literal"><span class="pre">complex(real,</span> <span class="pre">imag)</span></tt> function.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="mi">1</span><span class="n">j</span> <span class="o">*</span> <span class="mi">1</span><span class="n">J</span> <span class="go">(-1+0j)</span> <span class="gp">>>> </span><span class="mi">1</span><span class="n">j</span> <span class="o">*</span> <span class="nb">complex</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span> <span class="go">(-1+0j)</span> <span class="gp">>>> </span><span class="mi">3</span><span class="o">+</span><span class="mi">1</span><span class="n">j</span><span class="o">*</span><span class="mi">3</span> <span class="go">(3+3j)</span> <span class="gp">>>> </span><span class="p">(</span><span class="mi">3</span><span class="o">+</span><span class="mi">1</span><span class="n">j</span><span class="p">)</span><span class="o">*</span><span class="mi">3</span> <span class="go">(9+3j)</span> <span class="gp">>>> </span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">2</span><span class="n">j</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">1</span><span class="o">+</span><span class="mi">1</span><span class="n">j</span><span class="p">)</span> <span class="go">(1.5+0.5j)</span> </pre></div> </div> <p>Complex numbers are always represented as two floating point numbers, the real and imaginary part. To extract these parts from a complex number <em>z</em>, use <tt class="docutils literal"><span class="pre">z.real</span></tt> and <tt class="docutils literal"><span class="pre">z.imag</span></tt>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="mf">1.5</span><span class="o">+</span><span class="mf">0.5</span><span class="n">j</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">real</span> <span class="go">1.5</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span> <span class="go">0.5</span> </pre></div> </div> <p>The conversion functions to floating point and integer (<a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a>, <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> and <a class="reference internal" href="../library/functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a>) don’t work for complex numbers — there is no one correct way to convert a complex number to a real number. Use <tt class="docutils literal"><span class="pre">abs(z)</span></tt> to get its magnitude (as a float) or <tt class="docutils literal"><span class="pre">z.real</span></tt> to get its real part.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="o">=</span><span class="mf">3.0</span><span class="o">+</span><span class="mf">4.0</span><span class="n">j</span> <span class="gp">>>> </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> <span class="gr">TypeError</span>: <span class="n">can't convert complex to float; use abs(z)</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">real</span> <span class="go">3.0</span> <span class="gp">>>> </span><span class="n">a</span><span class="o">.</span><span class="n">imag</span> <span class="go">4.0</span> <span class="gp">>>> </span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="c"># sqrt(a.real**2 + a.imag**2)</span> <span class="go">5.0</span> </pre></div> </div> <p>In interactive mode, the last printed expression is assigned to the variable <tt class="docutils literal"><span class="pre">_</span></tt>. This means that when you are using Python as a desk calculator, it is somewhat easier to continue calculations, for example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">tax</span> <span class="o">=</span> <span class="mf">12.5</span> <span class="o">/</span> <span class="mi">100</span> <span class="gp">>>> </span><span class="n">price</span> <span class="o">=</span> <span class="mf">100.50</span> <span class="gp">>>> </span><span class="n">price</span> <span class="o">*</span> <span class="n">tax</span> <span class="go">12.5625</span> <span class="gp">>>> </span><span class="n">price</span> <span class="o">+</span> <span class="n">_</span> <span class="go">113.0625</span> <span class="gp">>>> </span><span class="nb">round</span><span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="go">113.06</span> </pre></div> </div> <p>This variable should be treated as read-only by the user. Don’t explicitly assign a value to it — you would create an independent local variable with the same name masking the built-in variable with its magic behavior.</p> </div> <div class="section" id="strings"> <span id="tut-strings"></span><h3>3.1.2. Strings<a class="headerlink" href="#strings" title="Permalink to this headline">¶</a></h3> <p>Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes or double quotes:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">'spam eggs'</span> <span class="go">'spam eggs'</span> <span class="gp">>>> </span><span class="s">'doesn</span><span class="se">\'</span><span class="s">t'</span> <span class="go">"doesn't"</span> <span class="gp">>>> </span><span class="s">"doesn't"</span> <span class="go">"doesn't"</span> <span class="gp">>>> </span><span class="s">'"Yes," he said.'</span> <span class="go">'"Yes," he said.'</span> <span class="gp">>>> </span><span class="s">"</span><span class="se">\"</span><span class="s">Yes,</span><span class="se">\"</span><span class="s"> he said."</span> <span class="go">'"Yes," he said.'</span> <span class="gp">>>> </span><span class="s">'"Isn</span><span class="se">\'</span><span class="s">t," she said.'</span> <span class="go">'"Isn\'t," she said.'</span> </pre></div> </div> <p>The interpreter prints the result of string operations in the same way as they are typed for input: inside quotes, and with quotes and other funny characters escaped by backslashes, to show the precise value. The string is enclosed in double quotes if the string contains a single quote and no double quotes, else it’s enclosed in single quotes. The <a class="reference internal" href="../reference/simple_stmts.html#print"><tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt></a> statement produces a more readable output for such input strings.</p> <p>String literals can span multiple lines in several ways. Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">"This is a rather long string containing</span><span class="se">\n\</span> <span class="s">several lines of text just as you would do in C.</span><span class="se">\n\</span> <span class="s"> Note that whitespace at the beginning of the line is</span><span class="se">\</span> <span class="s"> significant."</span> <span class="k">print</span> <span class="n">hello</span> </pre></div> </div> <p>Note that newlines still need to be embedded in the string using <tt class="docutils literal"><span class="pre">\n</span></tt> – the newline following the trailing backslash is discarded. This example would print the following:</p> <div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant. </pre></div> </div> <p>Or, strings can be surrounded in a pair of matching triple-quotes: <tt class="docutils literal"><span class="pre">"""</span></tt> or <tt class="docutils literal"><span class="pre">'''</span></tt>. End of lines do not need to be escaped when using triple-quotes, but they will be included in the string.</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">print</span> <span class="s">"""</span> <span class="s">Usage: thingy [OPTIONS]</span> <span class="s"> -h Display this usage message</span> <span class="s"> -H hostname Hostname to connect to</span> <span class="s">"""</span> </pre></div> </div> <p>produces the following output:</p> <div class="highlight-text"><div class="highlight"><pre>Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to </pre></div> </div> <p>If we make the string literal a “raw” string, <tt class="docutils literal"><span class="pre">\n</span></tt> sequences are not converted to newlines, but the backslash at the end of the line, and the newline character in the source, are both included in the string as data. Thus, the example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">hello</span> <span class="o">=</span> <span class="s">r"This is a rather long string containing\n</span><span class="se">\</span> <span class="s">several lines of text much as you would do in C."</span> <span class="k">print</span> <span class="n">hello</span> </pre></div> </div> <p>would print:</p> <div class="highlight-text"><div class="highlight"><pre>This is a rather long string containing\n\ several lines of text much as you would do in C. </pre></div> </div> <p>Strings can be concatenated (glued together) with the <tt class="docutils literal"><span class="pre">+</span></tt> operator, and repeated with <tt class="docutils literal"><span class="pre">*</span></tt>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span> <span class="o">=</span> <span class="s">'Help'</span> <span class="o">+</span> <span class="s">'A'</span> <span class="gp">>>> </span><span class="n">word</span> <span class="go">'HelpA'</span> <span class="gp">>>> </span><span class="s">'<'</span> <span class="o">+</span> <span class="n">word</span><span class="o">*</span><span class="mi">5</span> <span class="o">+</span> <span class="s">'>'</span> <span class="go">'<HelpAHelpAHelpAHelpAHelpA>'</span> </pre></div> </div> <p>Two string literals next to each other are automatically concatenated; the first line above could also have been written <tt class="docutils literal"><span class="pre">word</span> <span class="pre">=</span> <span class="pre">'Help'</span> <span class="pre">'A'</span></tt>; this only works with two literals, not with arbitrary string expressions:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">'str'</span> <span class="s">'ing'</span> <span class="c"># <- This is ok</span> <span class="go">'string'</span> <span class="gp">>>> </span><span class="s">'str'</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">+</span> <span class="s">'ing'</span> <span class="c"># <- This is ok</span> <span class="go">'string'</span> <span class="gp">>>> </span><span class="s">'str'</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="s">'ing'</span> <span class="c"># <- This is invalid</span> <span class="go"> File "<stdin>", line 1, in ?</span> <span class="go"> 'str'.strip() 'ing'</span> <span class="go"> ^</span> <span class="go">SyntaxError: invalid syntax</span> </pre></div> </div> <p>Strings can be subscripted (indexed); like in C, the first character of a string has subscript (index) 0. There is no separate character type; a character is simply a string of size one. Like in Icon, substrings can be specified with the <em>slice notation</em>: two indices separated by a colon.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="go">'A'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="go">'He'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="go">'lp'</span> </pre></div> </div> <p>Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="c"># The first two characters</span> <span class="go">'He'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span> <span class="c"># Everything except the first two characters</span> <span class="go">'lpA'</span> </pre></div> </div> <p>Unlike a C string, Python strings cannot be changed. Assigning to an indexed position in the string results in an error:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">'x'</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> <span class="gr">TypeError</span>: <span class="n">object does not support item assignment</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="s">'Splat'</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> <span class="gr">TypeError</span>: <span class="n">object does not support slice assignment</span> </pre></div> </div> <p>However, creating a new string with the combined content is easy and efficient:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">'x'</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="go">'xelpA'</span> <span class="gp">>>> </span><span class="s">'Splat'</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="go">'SplatA'</span> </pre></div> </div> <p>Here’s a useful invariant of slice operations: <tt class="docutils literal"><span class="pre">s[:i]</span> <span class="pre">+</span> <span class="pre">s[i:]</span></tt> equals <tt class="docutils literal"><span class="pre">s</span></tt>.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span> <span class="go">'HelpA'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="n">word</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span> <span class="go">'HelpA'</span> </pre></div> </div> <p>Degenerate slice indices are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound returns an empty string.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">100</span><span class="p">]</span> <span class="go">'elpA'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">10</span><span class="p">:]</span> <span class="go">''</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="mi">2</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span> <span class="go">''</span> </pre></div> </div> <p>Indices may be negative numbers, to start counting from the right. For example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c"># The last character</span> <span class="go">'A'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="c"># The last-but-one character</span> <span class="go">'p'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">:]</span> <span class="c"># The last two characters</span> <span class="go">'pA'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[:</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="c"># Everything except the last two characters</span> <span class="go">'Hel'</span> </pre></div> </div> <p>But note that -0 is really the same as 0, so it does not count from the right!</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">0</span><span class="p">]</span> <span class="c"># (since -0 equals 0)</span> <span class="go">'H'</span> </pre></div> </div> <p>Out-of-range negative slice indices are truncated, but don’t try this for single-element (non-slice) indices:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">100</span><span class="p">:]</span> <span class="go">'HelpA'</span> <span class="gp">>>> </span><span class="n">word</span><span class="p">[</span><span class="o">-</span><span class="mi">10</span><span class="p">]</span> <span class="c"># error</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> <span class="gr">IndexError</span>: <span class="n">string index out of range</span> </pre></div> </div> <p>One way to remember how slices work is to think of the indices as pointing <em>between</em> characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of <em>n</em> characters has index <em>n</em>, for example:</p> <div class="highlight-python"><pre> +---+---+---+---+---+ | H | e | l | p | A | +---+---+---+---+---+ 0 1 2 3 4 5 -5 -4 -3 -2 -1</pre> </div> <p>The first row of numbers gives the position of the indices 0...5 in the string; the second row gives the corresponding negative indices. The slice from <em>i</em> to <em>j</em> consists of all characters between the edges labeled <em>i</em> and <em>j</em>, respectively.</p> <p>For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of <tt class="docutils literal"><span class="pre">word[1:3]</span></tt> is 2.</p> <p>The built-in function <a class="reference internal" href="../library/functions.html#len" title="len"><tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt></a> returns the length of a string:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">s</span> <span class="o">=</span> <span class="s">'supercalifragilisticexpialidocious'</span> <span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span> <span class="go">34</span> </pre></div> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <dt><a class="reference internal" href="../library/stdtypes.html#typesseq"><em>Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange</em></a></dt> <dd>Strings, and the Unicode strings described in the next section, are examples of <em>sequence types</em>, and support the common operations supported by such types.</dd> <dt><a class="reference internal" href="../library/stdtypes.html#string-methods"><em>String Methods</em></a></dt> <dd>Both strings and Unicode strings support a large number of methods for basic transformations and searching.</dd> <dt><a class="reference internal" href="../library/string.html#new-string-formatting"><em>String Formatting</em></a></dt> <dd>Information about string formatting with <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> is described here.</dd> <dt><a class="reference internal" href="../library/stdtypes.html#string-formatting"><em>String Formatting Operations</em></a></dt> <dd>The old formatting operations invoked when strings and Unicode strings are the left operand of the <tt class="docutils literal"><span class="pre">%</span></tt> operator are described in more detail here.</dd> </dl> </div> </div> <div class="section" id="unicode-strings"> <span id="tut-unicodestrings"></span><h3>3.1.3. Unicode Strings<a class="headerlink" href="#unicode-strings" title="Permalink to this headline">¶</a></h3> <p>Starting with Python 2.0 a new data type for storing text data is available to the programmer: the Unicode object. It can be used to store and manipulate Unicode data (see <a class="reference external" href="http://www.unicode.org/">http://www.unicode.org/</a>) and integrates well with the existing string objects, providing auto-conversions where necessary.</p> <p>Unicode has the advantage of providing one ordinal for every character in every script used in modern and ancient texts. Previously, there were only 256 possible ordinals for script characters. Texts were typically bound to a code page which mapped the ordinals to script characters. This lead to very much confusion especially with respect to internationalization (usually written as <tt class="docutils literal"><span class="pre">i18n</span></tt> — <tt class="docutils literal"><span class="pre">'i'</span></tt> + 18 characters + <tt class="docutils literal"><span class="pre">'n'</span></tt>) of software. Unicode solves these problems by defining one code page for all scripts.</p> <p>Creating Unicode strings in Python is just as simple as creating normal strings:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u'Hello World !'</span> <span class="go">u'Hello World !'</span> </pre></div> </div> <p>The small <tt class="docutils literal"><span class="pre">'u'</span></tt> in front of the quote indicates that a Unicode string is supposed to be created. If you want to include special characters in the string, you can do so by using the Python <em>Unicode-Escape</em> encoding. The following example shows how:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u'Hello</span><span class="se">\u0020</span><span class="s">World !'</span> <span class="go">u'Hello World !'</span> </pre></div> </div> <p>The escape sequence <tt class="docutils literal"><span class="pre">\u0020</span></tt> indicates to insert the Unicode character with the ordinal value 0x0020 (the space character) at the given position.</p> <p>Other characters are interpreted by using their respective ordinal values directly as Unicode ordinals. If you have literal strings in the standard Latin-1 encoding that is used in many Western countries, you will find it convenient that the lower 256 characters of Unicode are the same as the 256 characters of Latin-1.</p> <p>For experts, there is also a raw mode just like the one for normal strings. You have to prefix the opening quote with ‘ur’ to have Python use the <em>Raw-Unicode-Escape</em> encoding. It will only apply the above <tt class="docutils literal"><span class="pre">\uXXXX</span></tt> conversion if there is an uneven number of backslashes in front of the small ‘u’.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">ur'Hello\u0020World !'</span> <span class="go">u'Hello World !'</span> <span class="gp">>>> </span><span class="s">ur'Hello</span><span class="se">\\</span><span class="s">u0020World !'</span> <span class="go">u'Hello\\\\u0020World !'</span> </pre></div> </div> <p>The raw mode is most useful when you have to enter lots of backslashes, as can be necessary in regular expressions.</p> <p>Apart from these standard encodings, Python provides a whole set of other ways of creating Unicode strings on the basis of a known encoding.</p> <p id="index-0">The built-in function <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><tt class="xref py py-func docutils literal"><span class="pre">unicode()</span></tt></a> provides access to all registered Unicode codecs (COders and DECoders). Some of the more well known encodings which these codecs can convert are <em>Latin-1</em>, <em>ASCII</em>, <em>UTF-8</em>, and <em>UTF-16</em>. The latter two are variable-length encodings that store each Unicode character in one or more bytes. The default encoding is normally set to ASCII, which passes through characters in the range 0 to 127 and rejects any other characters with an error. When a Unicode string is printed, written to a file, or converted with <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a>, conversion takes place using this default encoding.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u"abc"</span> <span class="go">u'abc'</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="s">u"abc"</span><span class="p">)</span> <span class="go">'abc'</span> <span class="gp">>>> </span><span class="s">u"äöü"</span> <span class="go">u'\xe4\xf6\xfc'</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="s">u"äöü"</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> File <span class="nb">"<stdin>"</span>, line <span class="m">1</span>, in <span class="n">?</span> <span class="gr">UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2</span>: <span class="n">ordinal not in range(128)</span> </pre></div> </div> <p>To convert a Unicode string into an 8-bit string using a specific encoding, Unicode objects provide an <tt class="xref py py-func docutils literal"><span class="pre">encode()</span></tt> method that takes one argument, the name of the encoding. Lowercase names for encodings are preferred.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="s">u"äöü"</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">'utf-8'</span><span class="p">)</span> <span class="go">'\xc3\xa4\xc3\xb6\xc3\xbc'</span> </pre></div> </div> <p>If you have data in a specific encoding and want to produce a corresponding Unicode string from it, you can use the <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><tt class="xref py py-func docutils literal"><span class="pre">unicode()</span></tt></a> function with the encoding name as the second argument.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">unicode</span><span class="p">(</span><span class="s">'</span><span class="se">\xc3\xa4\xc3\xb6\xc3\xbc</span><span class="s">'</span><span class="p">,</span> <span class="s">'utf-8'</span><span class="p">)</span> <span class="go">u'\xe4\xf6\xfc'</span> </pre></div> </div> </div> <div class="section" id="lists"> <span id="tut-lists"></span><h3>3.1.4. Lists<a class="headerlink" href="#lists" title="Permalink to this headline">¶</a></h3> <p>Python knows a number of <em>compound</em> data types, used to group together other values. The most versatile is the <em>list</em>, which can be written as a list of comma-separated values (items) between square brackets. List items need not all have the same type.</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">'spam'</span><span class="p">,</span> <span class="s">'eggs'</span><span class="p">,</span> <span class="mi">100</span><span class="p">,</span> <span class="mi">1234</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">['spam', 'eggs', 100, 1234]</span> </pre></div> </div> <p>Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="go">'spam'</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="go">1234</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="go">100</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="go">['eggs', 100]</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">'bacon'</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">2</span><span class="p">]</span> <span class="go">['spam', 'eggs', 'bacon', 4]</span> <span class="gp">>>> </span><span class="mi">3</span><span class="o">*</span><span class="n">a</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s">'Boo!'</span><span class="p">]</span> <span class="go">['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']</span> </pre></div> </div> <p>All slice operations return a new list containing the requested elements. This means that the following slice returns a shallow copy of the list <em>a</em>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="p">[:]</span> <span class="go">['spam', 'eggs', 100, 1234]</span> </pre></div> </div> <p>Unlike strings, which are <em>immutable</em>, it is possible to change individual elements of a list:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="go">['spam', 'eggs', 100, 1234]</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mi">23</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">['spam', 'eggs', 123, 1234]</span> </pre></div> </div> <p>Assignment to slices is also possible, and this can even change the size of the list or clear it entirely:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># Replace some items:</span> <span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">12</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[1, 12, 123, 1234]</span> <span class="gp">>>> </span><span class="c"># Remove some:</span> <span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[123, 1234]</span> <span class="gp">>>> </span><span class="c"># Insert some:</span> <span class="gp">... </span><span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="p">[</span><span class="s">'bletch'</span><span class="p">,</span> <span class="s">'xyzzy'</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[123, 'bletch', 'xyzzy', 1234]</span> <span class="gp">>>> </span><span class="c"># Insert (a copy of) itself at the beginning</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[:</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]</span> <span class="gp">>>> </span><span class="c"># Clear the list: replace all items with an empty list</span> <span class="gp">>>> </span><span class="n">a</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[]</span> <span class="gp">>>> </span><span class="n">a</span> <span class="go">[]</span> </pre></div> </div> <p>The built-in function <a class="reference internal" href="../library/functions.html#len" title="len"><tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt></a> also applies to lists:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s">'a'</span><span class="p">,</span> <span class="s">'b'</span><span class="p">,</span> <span class="s">'c'</span><span class="p">,</span> <span class="s">'d'</span><span class="p">]</span> <span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="go">4</span> </pre></div> </div> <p>It is possible to nest lists (create lists containing other lists), for example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">q</span> <span class="o">=</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> <span class="gp">>>> </span><span class="n">p</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="n">q</span><span class="p">,</span> <span class="mi">4</span><span class="p">]</span> <span class="gp">>>> </span><span class="nb">len</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="go">3</span> <span class="gp">>>> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="go">[2, 3]</span> <span class="gp">>>> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="go">2</span> <span class="gp">>>> </span><span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">'xtra'</span><span class="p">)</span> <span class="c"># See section 5.1</span> <span class="gp">>>> </span><span class="n">p</span> <span class="go">[1, [2, 3, 'xtra'], 4]</span> <span class="gp">>>> </span><span class="n">q</span> <span class="go">[2, 3, 'xtra']</span> </pre></div> </div> <p>Note that in the last example, <tt class="docutils literal"><span class="pre">p[1]</span></tt> and <tt class="docutils literal"><span class="pre">q</span></tt> really refer to the same object! We’ll come back to <em>object semantics</em> later.</p> </div> </div> <div class="section" id="first-steps-towards-programming"> <span id="tut-firststeps"></span><h2>3.2. First Steps Towards Programming<a class="headerlink" href="#first-steps-towards-programming" title="Permalink to this headline">¶</a></h2> <p>Of course, we can use Python for more complicated tasks than adding two and two together. For instance, we can write an initial sub-sequence of the <em>Fibonacci</em> series as follows:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># Fibonacci series:</span> <span class="gp">... </span><span class="c"># the sum of two elements defines the next</span> <span class="gp">... </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="gp">>>> </span><span class="k">while</span> <span class="n">b</span> <span class="o"><</span> <span class="mi">10</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">b</span> <span class="gp">... </span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span> <span class="gp">...</span> <span class="go">1</span> <span class="go">1</span> <span class="go">2</span> <span class="go">3</span> <span class="go">5</span> <span class="go">8</span> </pre></div> </div> <p>This example introduces several new features.</p> <ul> <li><p class="first">The first line contains a <em>multiple assignment</em>: the variables <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated first before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right.</p> </li> <li><p class="first">The <a class="reference internal" href="../reference/compound_stmts.html#while"><tt class="xref std std-keyword docutils literal"><span class="pre">while</span></tt></a> loop executes as long as the condition (here: <tt class="docutils literal"><span class="pre">b</span> <span class="pre"><</span> <span class="pre">10</span></tt>) remains true. In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison. The standard comparison operators are written the same as in C: <tt class="docutils literal"><span class="pre"><</span></tt> (less than), <tt class="docutils literal"><span class="pre">></span></tt> (greater than), <tt class="docutils literal"><span class="pre">==</span></tt> (equal to), <tt class="docutils literal"><span class="pre"><=</span></tt> (less than or equal to), <tt class="docutils literal"><span class="pre">>=</span></tt> (greater than or equal to) and <tt class="docutils literal"><span class="pre">!=</span></tt> (not equal to).</p> </li> <li><p class="first">The <em>body</em> of the loop is <em>indented</em>: indentation is Python’s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount.</p> </li> <li><p class="first">The <a class="reference internal" href="../reference/simple_stmts.html#print"><tt class="xref std std-keyword docutils literal"><span class="pre">print</span></tt></a> statement writes the value of the expression(s) it is given. It differs from just writing the expression you want to write (as we did earlier in the calculator examples) in the way it handles multiple expressions and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely, like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">i</span> <span class="o">=</span> <span class="mi">256</span><span class="o">*</span><span class="mi">256</span> <span class="gp">>>> </span><span class="k">print</span> <span class="s">'The value of i is'</span><span class="p">,</span> <span class="n">i</span> <span class="go">The value of i is 65536</span> </pre></div> </div> <p>A trailing comma avoids the newline after the output:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span> <span class="gp">>>> </span><span class="k">while</span> <span class="n">b</span> <span class="o"><</span> <span class="mi">1000</span><span class="p">:</span> <span class="gp">... </span> <span class="k">print</span> <span class="n">b</span><span class="p">,</span> <span class="gp">... </span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span> <span class="gp">...</span> <span class="go">1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987</span> </pre></div> </div> <p>Note that the interpreter inserts a newline before it prints the next prompt if the last line was not completed.</p> </li> </ul> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">3. An Informal Introduction to Python</a><ul> <li><a class="reference internal" href="#using-python-as-a-calculator">3.1. Using Python as a Calculator</a><ul> <li><a class="reference internal" href="#numbers">3.1.1. Numbers</a></li> <li><a class="reference internal" href="#strings">3.1.2. Strings</a></li> <li><a class="reference internal" href="#unicode-strings">3.1.3. Unicode Strings</a></li> <li><a class="reference internal" href="#lists">3.1.4. Lists</a></li> </ul> </li> <li><a class="reference internal" href="#first-steps-towards-programming">3.2. First Steps Towards Programming</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="interpreter.html" title="previous chapter">2. Using the Python Interpreter</a></p> <h4>Next topic</h4> <p class="topless"><a href="controlflow.html" title="next chapter">4. More Control Flow Tools</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/tutorial/introduction.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="controlflow.html" title="4. More Control Flow Tools" >next</a> |</li> <li class="right" > <a href="interpreter.html" title="2. Using the Python Interpreter" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Tutorial</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close