Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > e1011ddec34cda34f3a002b121247943 > files > 1005

python-docs-2.7.17-1.1.mga7.noarch.rpm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>5. Expressions &#8212; Python 2.7.17 documentation</title>
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></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/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.17 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="6. Simple statements" href="simple_stmts.html" />
    <link rel="prev" title="4. Execution model" href="executionmodel.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/2/reference/expressions.html" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <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="simple_stmts.html" title="6. Simple statements"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="executionmodel.html" title="4. Execution model"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">The Python Language Reference</a> &#187;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="expressions">
<span id="id1"></span><h1>5. Expressions<a class="headerlink" href="#expressions" title="Permalink to this headline">¶</a></h1>
<p id="index-0">This chapter explains the meaning of the elements of expressions in Python.</p>
<p id="index-1"><strong>Syntax Notes:</strong> In this and the following chapters, extended BNF notation will
be used to describe syntax, not lexical analysis.  When (one alternative of) a
syntax rule has the form</p>
<pre>
<strong id="grammar-token-name">name</strong> ::=  <code class="xref docutils literal notranslate"><span class="pre">othername</span></code>
</pre>
<p id="index-2">and no semantics are given, the semantics of this form of <code class="docutils literal notranslate"><span class="pre">name</span></code> are the same
as for <code class="docutils literal notranslate"><span class="pre">othername</span></code>.</p>
<div class="section" id="arithmetic-conversions">
<span id="conversions"></span><h2>5.1. Arithmetic conversions<a class="headerlink" href="#arithmetic-conversions" title="Permalink to this headline">¶</a></h2>
<p id="index-3">When a description of an arithmetic operator below uses the phrase “the numeric
arguments are converted to a common type,” the arguments are coerced using the
coercion rules listed at  <a class="reference internal" href="datamodel.html#coercion-rules"><span class="std std-ref">Coercion rules</span></a>.  If both arguments are standard
numeric types, the following coercions are applied:</p>
<ul class="simple">
<li><p>If either argument is a complex number, the other is converted to complex;</p></li>
<li><p>otherwise, if either argument is a floating point number, the other is
converted to floating point;</p></li>
<li><p>otherwise, if either argument is a long integer, the other is converted to
long integer;</p></li>
<li><p>otherwise, both must be plain integers and no conversion is necessary.</p></li>
</ul>
<p>Some additional rules apply for certain operators (e.g., a string left argument
to the ‘%’ operator). Extensions can define their own coercions.</p>
</div>
<div class="section" id="atoms">
<span id="id2"></span><h2>5.2. Atoms<a class="headerlink" href="#atoms" title="Permalink to this headline">¶</a></h2>
<p id="index-4">Atoms are the most basic elements of expressions.  The simplest atoms are
identifiers or literals.  Forms enclosed in reverse quotes or in parentheses,
brackets or braces are also categorized syntactically as atoms.  The syntax for
atoms is:</p>
<pre>
<strong id="grammar-token-atom">atom     </strong> ::=  <a class="reference internal" href="lexical_analysis.html#grammar-token-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> | <a class="reference internal" href="#grammar-token-literal"><code class="xref docutils literal notranslate"><span class="pre">literal</span></code></a> | <a class="reference internal" href="#grammar-token-enclosure"><code class="xref docutils literal notranslate"><span class="pre">enclosure</span></code></a>
<strong id="grammar-token-enclosure">enclosure</strong> ::=  <a class="reference internal" href="#grammar-token-parenth-form"><code class="xref docutils literal notranslate"><span class="pre">parenth_form</span></code></a> | <a class="reference internal" href="#grammar-token-list-display"><code class="xref docutils literal notranslate"><span class="pre">list_display</span></code></a>
               | <a class="reference internal" href="#grammar-token-generator-expression"><code class="xref docutils literal notranslate"><span class="pre">generator_expression</span></code></a> | <a class="reference internal" href="#grammar-token-dict-display"><code class="xref docutils literal notranslate"><span class="pre">dict_display</span></code></a> | <a class="reference internal" href="#grammar-token-set-display"><code class="xref docutils literal notranslate"><span class="pre">set_display</span></code></a>
               | <a class="reference internal" href="#grammar-token-string-conversion"><code class="xref docutils literal notranslate"><span class="pre">string_conversion</span></code></a> | <a class="reference internal" href="#grammar-token-yield-atom"><code class="xref docutils literal notranslate"><span class="pre">yield_atom</span></code></a>
</pre>
<div class="section" id="atom-identifiers">
<span id="identifiers-names"></span><h3>5.2.1. Identifiers (Names)<a class="headerlink" href="#atom-identifiers" title="Permalink to this headline">¶</a></h3>
<p id="index-5">An identifier occurring as an atom is a name.  See section <a class="reference internal" href="lexical_analysis.html#identifiers"><span class="std std-ref">Identifiers and keywords</span></a>
for lexical definition and section <a class="reference internal" href="executionmodel.html#naming"><span class="std std-ref">Naming and binding</span></a> for documentation of naming and
binding.</p>
<p id="index-6">When the name is bound to an object, evaluation of the atom yields that object.
When a name is not bound, an attempt to evaluate it raises a <a class="reference internal" href="../library/exceptions.html#exceptions.NameError" title="exceptions.NameError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">NameError</span></code></a>
exception.</p>
<p id="index-7"><strong>Private name mangling:</strong> When an identifier that textually occurs in a class
definition begins with two or more underscore characters and does not end in two
or more underscores, it is considered a <em class="dfn">private name</em> of that class.
Private names are transformed to a longer form before code is generated for
them.  The transformation inserts the class name, with leading underscores
removed and a single underscore inserted, in front of the name.  For example,
the identifier <code class="docutils literal notranslate"><span class="pre">__spam</span></code> occurring in a class named <code class="docutils literal notranslate"><span class="pre">Ham</span></code> will be transformed
to <code class="docutils literal notranslate"><span class="pre">_Ham__spam</span></code>.  This transformation is independent of the syntactical
context in which the identifier is used.  If the transformed name is extremely
long (longer than 255 characters), implementation defined truncation may happen.
If the class name consists only of underscores, no transformation is done.</p>
</div>
<div class="section" id="literals">
<span id="atom-literals"></span><h3>5.2.2. Literals<a class="headerlink" href="#literals" title="Permalink to this headline">¶</a></h3>
<p id="index-8">Python supports string literals and various numeric literals:</p>
<pre>
<strong id="grammar-token-literal">literal</strong> ::=  <a class="reference internal" href="lexical_analysis.html#grammar-token-stringliteral"><code class="xref docutils literal notranslate"><span class="pre">stringliteral</span></code></a> | <a class="reference internal" href="lexical_analysis.html#grammar-token-integer"><code class="xref docutils literal notranslate"><span class="pre">integer</span></code></a> | <a class="reference internal" href="lexical_analysis.html#grammar-token-longinteger"><code class="xref docutils literal notranslate"><span class="pre">longinteger</span></code></a>
             | <a class="reference internal" href="lexical_analysis.html#grammar-token-floatnumber"><code class="xref docutils literal notranslate"><span class="pre">floatnumber</span></code></a> | <a class="reference internal" href="lexical_analysis.html#grammar-token-imagnumber"><code class="xref docutils literal notranslate"><span class="pre">imagnumber</span></code></a>
</pre>
<p>Evaluation of a literal yields an object of the given type (string, integer,
long integer, floating point number, complex number) with the given value.  The
value may be approximated in the case of floating point and imaginary (complex)
literals.  See section <a class="reference internal" href="lexical_analysis.html#literals"><span class="std std-ref">Literals</span></a> for details.</p>
<p id="index-9">All literals correspond to immutable data types, and hence the object’s identity
is less important than its value.  Multiple evaluations of literals with the
same value (either the same occurrence in the program text or a different
occurrence) may obtain the same object or a different object with the same
value.</p>
</div>
<div class="section" id="parenthesized-forms">
<span id="parenthesized"></span><h3>5.2.3. Parenthesized forms<a class="headerlink" href="#parenthesized-forms" title="Permalink to this headline">¶</a></h3>
<p id="index-10">A parenthesized form is an optional expression list enclosed in parentheses:</p>
<pre>
<strong id="grammar-token-parenth-form">parenth_form</strong> ::=  &quot;(&quot; [<a class="reference internal" href="#grammar-token-expression-list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a>] &quot;)&quot;
</pre>
<p>A parenthesized expression list yields whatever that expression list yields: if
the list contains at least one comma, it yields a tuple; otherwise, it yields
the single expression that makes up the expression list.</p>
<p id="index-11">An empty pair of parentheses yields an empty tuple object.  Since tuples are
immutable, the rules for literals apply (i.e., two occurrences of the empty
tuple may or may not yield the same object).</p>
<p id="index-12">Note that tuples are not formed by the parentheses, but rather by use of the
comma operator.  The exception is the empty tuple, for which parentheses <em>are</em>
required — allowing unparenthesized “nothing” in expressions would cause
ambiguities and allow common typos to pass uncaught.</p>
</div>
<div class="section" id="list-displays">
<span id="lists"></span><h3>5.2.4. List displays<a class="headerlink" href="#list-displays" title="Permalink to this headline">¶</a></h3>
<p id="index-13">A list display is a possibly empty series of expressions enclosed in square
brackets:</p>
<pre>
<strong id="grammar-token-list-display">list_display       </strong> ::=  &quot;[&quot; [<a class="reference internal" href="#grammar-token-expression-list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a> | <a class="reference internal" href="#grammar-token-list-comprehension"><code class="xref docutils literal notranslate"><span class="pre">list_comprehension</span></code></a>] &quot;]&quot;
<strong id="grammar-token-list-comprehension">list_comprehension </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> <a class="reference internal" href="#grammar-token-list-for"><code class="xref docutils literal notranslate"><span class="pre">list_for</span></code></a>
<strong id="grammar-token-list-for">list_for           </strong> ::=  &quot;for&quot; <a class="reference internal" href="simple_stmts.html#grammar-token-target-list"><code class="xref docutils literal notranslate"><span class="pre">target_list</span></code></a> &quot;in&quot; <a class="reference internal" href="#grammar-token-old-expression-list"><code class="xref docutils literal notranslate"><span class="pre">old_expression_list</span></code></a> [<a class="reference internal" href="#grammar-token-list-iter"><code class="xref docutils literal notranslate"><span class="pre">list_iter</span></code></a>]
<strong id="grammar-token-old-expression-list">old_expression_list</strong> ::=  <a class="reference internal" href="#grammar-token-old-expression"><code class="xref docutils literal notranslate"><span class="pre">old_expression</span></code></a> [(&quot;,&quot; <a class="reference internal" href="#grammar-token-old-expression"><code class="xref docutils literal notranslate"><span class="pre">old_expression</span></code></a>)+ [&quot;,&quot;]]
<strong id="grammar-token-old-expression">old_expression     </strong> ::=  <a class="reference internal" href="#grammar-token-or-test"><code class="xref docutils literal notranslate"><span class="pre">or_test</span></code></a> | <a class="reference internal" href="#grammar-token-old-lambda-expr"><code class="xref docutils literal notranslate"><span class="pre">old_lambda_expr</span></code></a>
<strong id="grammar-token-list-iter">list_iter          </strong> ::=  <a class="reference internal" href="#grammar-token-list-for"><code class="xref docutils literal notranslate"><span class="pre">list_for</span></code></a> | <a class="reference internal" href="#grammar-token-list-if"><code class="xref docutils literal notranslate"><span class="pre">list_if</span></code></a>
<strong id="grammar-token-list-if">list_if            </strong> ::=  &quot;if&quot; <a class="reference internal" href="#grammar-token-old-expression"><code class="xref docutils literal notranslate"><span class="pre">old_expression</span></code></a> [<a class="reference internal" href="#grammar-token-list-iter"><code class="xref docutils literal notranslate"><span class="pre">list_iter</span></code></a>]
</pre>
<p id="index-14">A list display yields a new list object.  Its contents are specified by
providing either a list of expressions or a list comprehension.  When a
comma-separated list of expressions is supplied, its elements are evaluated from
left to right and placed into the list object in that order.  When a list
comprehension is supplied, it consists of a single expression followed by at
least one <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> clause and zero or more <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> or <a class="reference internal" href="compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a>
clauses.  In this case, the elements of the new list are those that would be
produced by considering each of the <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> or <a class="reference internal" href="compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a> clauses a
block, nesting from left to right, and evaluating the expression to produce a
list element each time the innermost block is reached <a class="footnote-reference brackets" href="#id19" id="id3">1</a>.</p>
</div>
<div class="section" id="displays-for-sets-and-dictionaries">
<span id="comprehensions"></span><h3>5.2.5. Displays for sets and dictionaries<a class="headerlink" href="#displays-for-sets-and-dictionaries" title="Permalink to this headline">¶</a></h3>
<p>For constructing a set or a dictionary Python provides special syntax
called “displays”, each of them in two flavors:</p>
<ul class="simple">
<li><p>either the container contents are listed explicitly, or</p></li>
<li><p>they are computed via a set of looping and filtering instructions, called a
<em class="dfn">comprehension</em>.</p></li>
</ul>
<p>Common syntax elements for comprehensions are:</p>
<pre>
<strong id="grammar-token-comprehension">comprehension</strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> <a class="reference internal" href="#grammar-token-comp-for"><code class="xref docutils literal notranslate"><span class="pre">comp_for</span></code></a>
<strong id="grammar-token-comp-for">comp_for     </strong> ::=  &quot;for&quot; <a class="reference internal" href="simple_stmts.html#grammar-token-target-list"><code class="xref docutils literal notranslate"><span class="pre">target_list</span></code></a> &quot;in&quot; <a class="reference internal" href="#grammar-token-or-test"><code class="xref docutils literal notranslate"><span class="pre">or_test</span></code></a> [<a class="reference internal" href="#grammar-token-comp-iter"><code class="xref docutils literal notranslate"><span class="pre">comp_iter</span></code></a>]
<strong id="grammar-token-comp-iter">comp_iter    </strong> ::=  <a class="reference internal" href="#grammar-token-comp-for"><code class="xref docutils literal notranslate"><span class="pre">comp_for</span></code></a> | <a class="reference internal" href="#grammar-token-comp-if"><code class="xref docutils literal notranslate"><span class="pre">comp_if</span></code></a>
<strong id="grammar-token-comp-if">comp_if      </strong> ::=  &quot;if&quot; <code class="xref docutils literal notranslate"><span class="pre">expression_nocond</span></code> [<a class="reference internal" href="#grammar-token-comp-iter"><code class="xref docutils literal notranslate"><span class="pre">comp_iter</span></code></a>]
</pre>
<p>The comprehension consists of a single expression followed by at least one
<a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> clause and zero or more <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> or <a class="reference internal" href="compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a> clauses.
In this case, the elements of the new container are those that would be produced
by considering each of the <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> or <a class="reference internal" href="compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a> clauses a block,
nesting from left to right, and evaluating the expression to produce an element
each time the innermost block is reached.</p>
<p>Note that the comprehension is executed in a separate scope, so names assigned
to in the target list don’t “leak” in the enclosing scope.</p>
</div>
<div class="section" id="generator-expressions">
<span id="genexpr"></span><h3>5.2.6. Generator expressions<a class="headerlink" href="#generator-expressions" title="Permalink to this headline">¶</a></h3>
<p id="index-15">A generator expression is a compact generator notation in parentheses:</p>
<pre>
<strong id="grammar-token-generator-expression">generator_expression</strong> ::=  &quot;(&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> <a class="reference internal" href="#grammar-token-comp-for"><code class="xref docutils literal notranslate"><span class="pre">comp_for</span></code></a> &quot;)&quot;
</pre>
<p>A generator expression yields a new generator object.  Its syntax is the same as
for comprehensions, except that it is enclosed in parentheses instead of
brackets or curly braces.</p>
<p>Variables used in the generator expression are evaluated lazily when the
<code class="xref py py-meth docutils literal notranslate"><span class="pre">__next__()</span></code> method is called for generator object (in the same fashion as
normal generators).  However, the leftmost <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> clause is immediately
evaluated, so that an error produced by it can be seen before any other possible
error in the code that handles the generator expression.  Subsequent
<a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> clauses cannot be evaluated immediately since they may depend on
the previous <a class="reference internal" href="compound_stmts.html#for"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">for</span></code></a> loop. For example: <code class="docutils literal notranslate"><span class="pre">(x*y</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">range(10)</span> <span class="pre">for</span> <span class="pre">y</span>
<span class="pre">in</span> <span class="pre">bar(x))</span></code>.</p>
<p>The parentheses can be omitted on calls with only one argument.  See section
<a class="reference internal" href="#calls"><span class="std std-ref">Calls</span></a> for the detail.</p>
</div>
<div class="section" id="dictionary-displays">
<span id="dict"></span><h3>5.2.7. Dictionary displays<a class="headerlink" href="#dictionary-displays" title="Permalink to this headline">¶</a></h3>
<p id="index-16">A dictionary display is a possibly empty series of key/datum pairs enclosed in
curly braces:</p>
<pre>
<strong id="grammar-token-dict-display">dict_display      </strong> ::=  &quot;{&quot; [<a class="reference internal" href="#grammar-token-key-datum-list"><code class="xref docutils literal notranslate"><span class="pre">key_datum_list</span></code></a> | <a class="reference internal" href="#grammar-token-dict-comprehension"><code class="xref docutils literal notranslate"><span class="pre">dict_comprehension</span></code></a>] &quot;}&quot;
<strong id="grammar-token-key-datum-list">key_datum_list    </strong> ::=  <a class="reference internal" href="#grammar-token-key-datum"><code class="xref docutils literal notranslate"><span class="pre">key_datum</span></code></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-key-datum"><code class="xref docutils literal notranslate"><span class="pre">key_datum</span></code></a>)* [&quot;,&quot;]
<strong id="grammar-token-key-datum">key_datum         </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> &quot;:&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
<strong id="grammar-token-dict-comprehension">dict_comprehension</strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> &quot;:&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> <a class="reference internal" href="#grammar-token-comp-for"><code class="xref docutils literal notranslate"><span class="pre">comp_for</span></code></a>
</pre>
<p>A dictionary display yields a new dictionary object.</p>
<p>If a comma-separated sequence of key/datum pairs is given, they are evaluated
from left to right to define the entries of the dictionary: each key object is
used as a key into the dictionary to store the corresponding datum.  This means
that you can specify the same key multiple times in the key/datum list, and the
final dictionary’s value for that key will be the last one given.</p>
<p>A dict comprehension, in contrast to list and set comprehensions, needs two
expressions separated with a colon followed by the usual “for” and “if” clauses.
When the comprehension is run, the resulting key and value elements are inserted
in the new dictionary in the order they are produced.</p>
<p id="index-17">Restrictions on the types of the key values are listed earlier in section
<a class="reference internal" href="datamodel.html#types"><span class="std std-ref">The standard type hierarchy</span></a>.  (To summarize, the key type should be <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>, which excludes
all mutable objects.)  Clashes between duplicate keys are not detected; the last
datum (textually rightmost in the display) stored for a given key value
prevails.</p>
</div>
<div class="section" id="set-displays">
<span id="set"></span><h3>5.2.8. Set displays<a class="headerlink" href="#set-displays" title="Permalink to this headline">¶</a></h3>
<p id="index-18">A set display is denoted by curly braces and distinguishable from dictionary
displays by the lack of colons separating keys and values:</p>
<pre>
<strong id="grammar-token-set-display">set_display</strong> ::=  &quot;{&quot; (<a class="reference internal" href="#grammar-token-expression-list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a> | <a class="reference internal" href="#grammar-token-comprehension"><code class="xref docutils literal notranslate"><span class="pre">comprehension</span></code></a>) &quot;}&quot;
</pre>
<p>A set display yields a new mutable set object, the contents being specified by
either a sequence of expressions or a comprehension.  When a comma-separated
list of expressions is supplied, its elements are evaluated from left to right
and added to the set object.  When a comprehension is supplied, the set is
constructed from the elements resulting from the comprehension.</p>
<p>An empty set cannot be constructed with <code class="docutils literal notranslate"><span class="pre">{}</span></code>; this literal constructs an empty
dictionary.</p>
</div>
<div class="section" id="string-conversions">
<span id="id4"></span><h3>5.2.9. String conversions<a class="headerlink" href="#string-conversions" title="Permalink to this headline">¶</a></h3>
<p id="index-19">A string conversion is an expression list enclosed in reverse (a.k.a. backward)
quotes:</p>
<pre>
<strong id="grammar-token-string-conversion">string_conversion</strong> ::=  &quot;`&quot; <a class="reference internal" href="#grammar-token-expression-list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a> &quot;`&quot;
</pre>
<p>A string conversion evaluates the contained expression list and converts the
resulting object into a string according to rules specific to its type.</p>
<p>If the object is a string, a number, <code class="docutils literal notranslate"><span class="pre">None</span></code>, or a tuple, list or dictionary
containing only objects whose type is one of these, the resulting string is a
valid Python expression which can be passed to the built-in function
<a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> to yield an expression with the same value (or an approximation, if
floating point numbers are involved).</p>
<p>(In particular, converting a string adds quotes around it and converts “funny”
characters to escape sequences that are safe to print.)</p>
<p id="index-20">Recursive objects (for example, lists or dictionaries that contain a reference
to themselves, directly or indirectly) use <code class="docutils literal notranslate"><span class="pre">...</span></code> to indicate a recursive
reference, and the result cannot be passed to <a class="reference internal" href="../library/functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> to get an equal value
(<a class="reference internal" href="../library/exceptions.html#exceptions.SyntaxError" title="exceptions.SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a> will be raised instead).</p>
<p id="index-21">The built-in function <a class="reference internal" href="../library/functions.html#repr" title="repr"><code class="xref py py-func docutils literal notranslate"><span class="pre">repr()</span></code></a> performs exactly the same conversion in its
argument as enclosing it in parentheses and reverse quotes does.  The built-in
function <a class="reference internal" href="../library/functions.html#str" title="str"><code class="xref py py-func docutils literal notranslate"><span class="pre">str()</span></code></a> performs a similar but more user-friendly conversion.</p>
</div>
<div class="section" id="yield-expressions">
<span id="yieldexpr"></span><h3>5.2.10. Yield expressions<a class="headerlink" href="#yield-expressions" title="Permalink to this headline">¶</a></h3>
<pre id="index-22">
<strong id="grammar-token-yield-atom">yield_atom      </strong> ::=  &quot;(&quot; <a class="reference internal" href="#grammar-token-yield-expression"><code class="xref docutils literal notranslate"><span class="pre">yield_expression</span></code></a> &quot;)&quot;
<strong id="grammar-token-yield-expression">yield_expression</strong> ::=  &quot;yield&quot; [<a class="reference internal" href="#grammar-token-expression-list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a>]
</pre>
<div class="versionadded">
<p><span class="versionmodified added">New in version 2.5.</span></p>
</div>
<p>The <a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression is only used when defining a generator function,
and can only be used in the body of a function definition. Using a
<a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression in a function definition is sufficient to cause that
definition to create a generator function instead of a normal function.</p>
<p>When a generator function is called, it returns an iterator known as a
generator.  That generator then controls the execution of a generator function.
The execution starts when one of the generator’s methods is called.  At that
time, the execution proceeds to the first <a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression, where it
is suspended again, returning the value of <a class="reference internal" href="#grammar-token-expression-list"><code class="xref std std-token docutils literal notranslate"><span class="pre">expression_list</span></code></a> to
generator’s caller.  By suspended we mean that all local state is retained,
including the current bindings of local variables, the instruction pointer, and
the internal evaluation stack.  When the execution is resumed by calling one of
the generator’s methods, the function can proceed exactly as if the
<a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression was just another external call. The value of the
<a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression after resuming depends on the method which resumed
the execution.</p>
<p id="index-23">All of this makes generator functions quite similar to coroutines; they yield
multiple times, they have more than one entry point and their execution can be
suspended.  The only difference is that a generator function cannot control
where should the execution continue after it yields; the control is always
transferred to the generator’s caller.</p>
<div class="section" id="generator-iterator-methods">
<span id="index-24"></span><h4>5.2.10.1. Generator-iterator methods<a class="headerlink" href="#generator-iterator-methods" title="Permalink to this headline">¶</a></h4>
<p>This subsection describes the methods of a generator iterator.  They can
be used to control the execution of a generator function.</p>
<p>Note that calling any of the generator methods below when the generator
is already executing raises a <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> exception.</p>
<span class="target" id="index-25"></span><dl class="method">
<dt id="generator.next">
<code class="descclassname">generator.</code><code class="descname">next</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#generator.next" title="Permalink to this definition">¶</a></dt>
<dd><p>Starts the execution of a generator function or resumes it at the last executed
<a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression.  When a generator function is resumed with a
<a class="reference internal" href="#generator.next" title="generator.next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a> method, the current <a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression
always evaluates to
<a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a>.  The execution then continues to the next <a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a>
expression, where the generator is suspended again, and the value of the
<a class="reference internal" href="#grammar-token-expression-list"><code class="xref std std-token docutils literal notranslate"><span class="pre">expression_list</span></code></a> is returned to <a class="reference internal" href="#generator.next" title="generator.next"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code></a>’s caller.
If the generator
exits without yielding another value, a <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> exception is
raised.</p>
</dd></dl>

<dl class="method">
<dt id="generator.send">
<code class="descclassname">generator.</code><code class="descname">send</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#generator.send" title="Permalink to this definition">¶</a></dt>
<dd><p>Resumes the execution and “sends” a value into the generator function.  The
<code class="docutils literal notranslate"><span class="pre">value</span></code> argument becomes the result of the current <a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a>
expression.  The <a class="reference internal" href="#generator.send" title="generator.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a> method returns the next value yielded by the
generator, or raises <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> if the generator exits without
yielding another value. When <a class="reference internal" href="#generator.send" title="generator.send"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send()</span></code></a> is called to start the generator, it
must be called with <a class="reference internal" href="../library/constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> as the argument, because there is no
<a class="reference internal" href="simple_stmts.html#yield"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">yield</span></code></a> expression that could receive the value.</p>
</dd></dl>

<dl class="method">
<dt id="generator.throw">
<code class="descclassname">generator.</code><code class="descname">throw</code><span class="sig-paren">(</span><em>type</em><span class="optional">[</span>, <em>value</em><span class="optional">[</span>, <em>traceback</em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#generator.throw" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises an exception of type <code class="docutils literal notranslate"><span class="pre">type</span></code> at the point where generator was paused,
and returns the next value yielded by the generator function.  If the generator
exits without yielding another value, a <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> exception is
raised.  If the generator function does not catch the passed-in exception, or
raises a different exception, then that exception propagates to the caller.</p>
</dd></dl>

<span class="target" id="index-26"></span><dl class="method">
<dt id="generator.close">
<code class="descclassname">generator.</code><code class="descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#generator.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Raises a <a class="reference internal" href="../library/exceptions.html#exceptions.GeneratorExit" title="exceptions.GeneratorExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">GeneratorExit</span></code></a> at the point where the generator function was
paused.  If the generator function then raises <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><code class="xref py py-exc docutils literal notranslate"><span class="pre">StopIteration</span></code></a> (by exiting
normally, or due to already being closed) or <a class="reference internal" href="../library/exceptions.html#exceptions.GeneratorExit" title="exceptions.GeneratorExit"><code class="xref py py-exc docutils literal notranslate"><span class="pre">GeneratorExit</span></code></a> (by not
catching the exception), close returns to its caller.  If the generator yields a
value, a <a class="reference internal" href="../library/exceptions.html#exceptions.RuntimeError" title="exceptions.RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.  If the generator raises any other
exception, it is propagated to the caller.  <a class="reference internal" href="#generator.close" title="generator.close"><code class="xref py py-meth docutils literal notranslate"><span class="pre">close()</span></code></a> does nothing if the
generator has already exited due to an exception or normal exit.</p>
</dd></dl>

<p>Here is a simple example that demonstrates the behavior of generators and
generator functions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">echo</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nb">print</span> <span class="s2">&quot;Execution starts when &#39;next()&#39; is called for the first time.&quot;</span>
<span class="gp">... </span>    <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
<span class="gp">... </span>            <span class="k">try</span><span class="p">:</span>
<span class="gp">... </span>                <span class="n">value</span> <span class="o">=</span> <span class="p">(</span><span class="k">yield</span> <span class="n">value</span><span class="p">)</span>
<span class="gp">... </span>            <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
<span class="gp">... </span>                <span class="n">value</span> <span class="o">=</span> <span class="n">e</span>
<span class="gp">... </span>    <span class="k">finally</span><span class="p">:</span>
<span class="gp">... </span>        <span class="nb">print</span> <span class="s2">&quot;Don&#39;t forget to clean up when &#39;close()&#39; is called.&quot;</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generator</span> <span class="o">=</span> <span class="n">echo</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">generator</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="go">Execution starts when &#39;next()&#39; is called for the first time.</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">generator</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="go">None</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span> <span class="n">generator</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generator</span><span class="o">.</span><span class="n">throw</span><span class="p">(</span><span class="ne">TypeError</span><span class="p">,</span> <span class="s2">&quot;spam&quot;</span><span class="p">)</span>
<span class="go">TypeError(&#39;spam&#39;,)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">generator</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="go">Don&#39;t forget to clean up when &#39;close()&#39; is called.</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><span class="target" id="index-27"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0342"><strong>PEP 342</strong></a> - Coroutines via Enhanced Generators</dt><dd><p>The proposal to enhance the API and syntax of generators, making them usable as
simple coroutines.</p>
</dd>
</dl>
</div>
</div>
</div>
</div>
<div class="section" id="primaries">
<span id="id5"></span><h2>5.3. Primaries<a class="headerlink" href="#primaries" title="Permalink to this headline">¶</a></h2>
<p id="index-28">Primaries represent the most tightly bound operations of the language. Their
syntax is:</p>
<pre>
<strong id="grammar-token-primary">primary</strong> ::=  <a class="reference internal" href="#grammar-token-atom"><code class="xref docutils literal notranslate"><span class="pre">atom</span></code></a> | <a class="reference internal" href="#grammar-token-attributeref"><code class="xref docutils literal notranslate"><span class="pre">attributeref</span></code></a> | <a class="reference internal" href="#grammar-token-subscription"><code class="xref docutils literal notranslate"><span class="pre">subscription</span></code></a> | <a class="reference internal" href="#grammar-token-slicing"><code class="xref docutils literal notranslate"><span class="pre">slicing</span></code></a> | <a class="reference internal" href="#grammar-token-call"><code class="xref docutils literal notranslate"><span class="pre">call</span></code></a>
</pre>
<div class="section" id="attribute-references">
<span id="id6"></span><h3>5.3.1. Attribute references<a class="headerlink" href="#attribute-references" title="Permalink to this headline">¶</a></h3>
<p id="index-29">An attribute reference is a primary followed by a period and a name:</p>
<pre>
<strong id="grammar-token-attributeref">attributeref</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><code class="xref docutils literal notranslate"><span class="pre">primary</span></code></a> &quot;.&quot; <a class="reference internal" href="lexical_analysis.html#grammar-token-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a>
</pre>
<p id="index-30">The primary must evaluate to an object of a type that supports attribute
references, e.g., a module, list, or an instance.  This object is then asked to
produce the attribute whose name is the identifier.  If this attribute is not
available, the exception <a class="reference internal" href="../library/exceptions.html#exceptions.AttributeError" title="exceptions.AttributeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">AttributeError</span></code></a> is raised. Otherwise, the type
and value of the object produced is determined by the object.  Multiple
evaluations of the same attribute reference may yield different objects.</p>
</div>
<div class="section" id="subscriptions">
<span id="id7"></span><h3>5.3.2. Subscriptions<a class="headerlink" href="#subscriptions" title="Permalink to this headline">¶</a></h3>
<span class="target" id="index-31"></span><p id="index-32">A subscription selects an item of a sequence (string, tuple or list) or mapping
(dictionary) object:</p>
<pre>
<strong id="grammar-token-subscription">subscription</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><code class="xref docutils literal notranslate"><span class="pre">primary</span></code></a> &quot;[&quot; <a class="reference internal" href="#grammar-token-expression-list"><code class="xref docutils literal notranslate"><span class="pre">expression_list</span></code></a> &quot;]&quot;
</pre>
<p>The primary must evaluate to an object of a sequence or mapping type.</p>
<p>If the primary is a mapping, the expression list must evaluate to an object
whose value is one of the keys of the mapping, and the subscription selects the
value in the mapping that corresponds to that key.  (The expression list is a
tuple except if it has exactly one item.)</p>
<p>If the primary is a sequence, the expression list must evaluate to a plain
integer.  If this value is negative, the length of the sequence is added to it
(so that, e.g., <code class="docutils literal notranslate"><span class="pre">x[-1]</span></code> selects the last item of <code class="docutils literal notranslate"><span class="pre">x</span></code>.)  The resulting value
must be a nonnegative integer less than the number of items in the sequence, and
the subscription selects the item whose index is that value (counting from
zero).</p>
<p id="index-33">A string’s items are characters.  A character is not a separate data type but a
string of exactly one character.</p>
</div>
<div class="section" id="slicings">
<span id="id8"></span><h3>5.3.3. Slicings<a class="headerlink" href="#slicings" title="Permalink to this headline">¶</a></h3>
<span class="target" id="index-34"></span><p id="index-35">A slicing selects a range of items in a sequence object (e.g., a string, tuple
or list).  Slicings may be used as expressions or as targets in assignment or
<a class="reference internal" href="simple_stmts.html#del"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">del</span></code></a> statements.  The syntax for a slicing:</p>
<pre>
<strong id="grammar-token-slicing">slicing         </strong> ::=  <a class="reference internal" href="#grammar-token-simple-slicing"><code class="xref docutils literal notranslate"><span class="pre">simple_slicing</span></code></a> | <a class="reference internal" href="#grammar-token-extended-slicing"><code class="xref docutils literal notranslate"><span class="pre">extended_slicing</span></code></a>
<strong id="grammar-token-simple-slicing">simple_slicing  </strong> ::=  <a class="reference internal" href="#grammar-token-primary"><code class="xref docutils literal notranslate"><span class="pre">primary</span></code></a> &quot;[&quot; <a class="reference internal" href="#grammar-token-short-slice"><code class="xref docutils literal notranslate"><span class="pre">short_slice</span></code></a> &quot;]&quot;
<strong id="grammar-token-extended-slicing">extended_slicing</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><code class="xref docutils literal notranslate"><span class="pre">primary</span></code></a> &quot;[&quot; <a class="reference internal" href="#grammar-token-slice-list"><code class="xref docutils literal notranslate"><span class="pre">slice_list</span></code></a> &quot;]&quot;
<strong id="grammar-token-slice-list">slice_list      </strong> ::=  <a class="reference internal" href="#grammar-token-slice-item"><code class="xref docutils literal notranslate"><span class="pre">slice_item</span></code></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-slice-item"><code class="xref docutils literal notranslate"><span class="pre">slice_item</span></code></a>)* [&quot;,&quot;]
<strong id="grammar-token-slice-item">slice_item      </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> | <a class="reference internal" href="#grammar-token-proper-slice"><code class="xref docutils literal notranslate"><span class="pre">proper_slice</span></code></a> | <a class="reference internal" href="#grammar-token-ellipsis"><code class="xref docutils literal notranslate"><span class="pre">ellipsis</span></code></a>
<strong id="grammar-token-proper-slice">proper_slice    </strong> ::=  <a class="reference internal" href="#grammar-token-short-slice"><code class="xref docutils literal notranslate"><span class="pre">short_slice</span></code></a> | <a class="reference internal" href="#grammar-token-long-slice"><code class="xref docutils literal notranslate"><span class="pre">long_slice</span></code></a>
<strong id="grammar-token-short-slice">short_slice     </strong> ::=  [<a class="reference internal" href="#grammar-token-lower-bound"><code class="xref docutils literal notranslate"><span class="pre">lower_bound</span></code></a>] &quot;:&quot; [<a class="reference internal" href="#grammar-token-upper-bound"><code class="xref docutils literal notranslate"><span class="pre">upper_bound</span></code></a>]
<strong id="grammar-token-long-slice">long_slice      </strong> ::=  <a class="reference internal" href="#grammar-token-short-slice"><code class="xref docutils literal notranslate"><span class="pre">short_slice</span></code></a> &quot;:&quot; [<a class="reference internal" href="#grammar-token-stride"><code class="xref docutils literal notranslate"><span class="pre">stride</span></code></a>]
<strong id="grammar-token-lower-bound">lower_bound     </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
<strong id="grammar-token-upper-bound">upper_bound     </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
<strong id="grammar-token-stride">stride          </strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
<strong id="grammar-token-ellipsis">ellipsis        </strong> ::=  &quot;...&quot;
</pre>
<p id="index-36">There is ambiguity in the formal syntax here: anything that looks like an
expression list also looks like a slice list, so any subscription can be
interpreted as a slicing.  Rather than further complicating the syntax, this is
disambiguated by defining that in this case the interpretation as a subscription
takes priority over the interpretation as a slicing (this is the case if the
slice list contains no proper slice nor ellipses).  Similarly, when the slice
list has exactly one short slice and no trailing comma, the interpretation as a
simple slicing takes priority over that as an extended slicing.</p>
<p>The semantics for a simple slicing are as follows.  The primary must evaluate to
a sequence object.  The lower and upper bound expressions, if present, must
evaluate to plain integers; defaults are zero and the <code class="docutils literal notranslate"><span class="pre">sys.maxint</span></code>,
respectively.  If either bound is negative, the sequence’s length is added to
it.  The slicing now selects all items with index <em>k</em> such that <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">&lt;=</span> <span class="pre">k</span> <span class="pre">&lt;</span> <span class="pre">j</span></code>
where <em>i</em> and <em>j</em> are the specified lower and upper bounds.  This may be an
empty sequence.  It is not an error if <em>i</em> or <em>j</em> lie outside the range of valid
indexes (such items don’t exist so they aren’t selected).</p>
<p id="index-37">The semantics for an extended slicing are as follows.  The primary must evaluate
to a mapping object, and it is indexed with a key that is constructed from the
slice list, as follows.  If the slice list contains at least one comma, the key
is a tuple containing the conversion of the slice items; otherwise, the
conversion of the lone slice item is the key.  The conversion of a slice item
that is an expression is that expression.  The conversion of an ellipsis slice
item is the built-in <code class="docutils literal notranslate"><span class="pre">Ellipsis</span></code> object.  The conversion of a proper slice is a
slice object (see section <a class="reference internal" href="datamodel.html#types"><span class="std std-ref">The standard type hierarchy</span></a>) whose <code class="xref py py-attr docutils literal notranslate"><span class="pre">start</span></code>,
<code class="xref py py-attr docutils literal notranslate"><span class="pre">stop</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">step</span></code> attributes are the values of the
expressions given as lower bound, upper bound and stride, respectively,
substituting <code class="docutils literal notranslate"><span class="pre">None</span></code> for missing expressions.</p>
</div>
<div class="section" id="calls">
<span id="index-38"></span><span id="id9"></span><h3>5.3.4. Calls<a class="headerlink" href="#calls" title="Permalink to this headline">¶</a></h3>
<p>A call calls a callable object (e.g., a <a class="reference internal" href="../glossary.html#term-function"><span class="xref std std-term">function</span></a>) with a possibly empty
series of <a class="reference internal" href="../glossary.html#term-argument"><span class="xref std std-term">arguments</span></a>:</p>
<pre>
<strong id="grammar-token-call">call                </strong> ::=  <a class="reference internal" href="#grammar-token-primary"><code class="xref docutils literal notranslate"><span class="pre">primary</span></code></a> &quot;(&quot; [<a class="reference internal" href="#grammar-token-argument-list"><code class="xref docutils literal notranslate"><span class="pre">argument_list</span></code></a> [&quot;,&quot;]
                          | <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> <code class="xref docutils literal notranslate"><span class="pre">genexpr_for</span></code>] &quot;)&quot;
<strong id="grammar-token-argument-list">argument_list       </strong> ::=  <a class="reference internal" href="#grammar-token-positional-arguments"><code class="xref docutils literal notranslate"><span class="pre">positional_arguments</span></code></a> [&quot;,&quot; <a class="reference internal" href="#grammar-token-keyword-arguments"><code class="xref docutils literal notranslate"><span class="pre">keyword_arguments</span></code></a>]
                            [&quot;,&quot; &quot;*&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>] [&quot;,&quot; <a class="reference internal" href="#grammar-token-keyword-arguments"><code class="xref docutils literal notranslate"><span class="pre">keyword_arguments</span></code></a>]
                            [&quot;,&quot; &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>]
                          | <a class="reference internal" href="#grammar-token-keyword-arguments"><code class="xref docutils literal notranslate"><span class="pre">keyword_arguments</span></code></a> [&quot;,&quot; &quot;*&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>]
                            [&quot;,&quot; &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>]
                          | &quot;*&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> [&quot;,&quot; <a class="reference internal" href="#grammar-token-keyword-arguments"><code class="xref docutils literal notranslate"><span class="pre">keyword_arguments</span></code></a>] [&quot;,&quot; &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>]
                          | &quot;**&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
<strong id="grammar-token-positional-arguments">positional_arguments</strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>)*
<strong id="grammar-token-keyword-arguments">keyword_arguments   </strong> ::=  <a class="reference internal" href="#grammar-token-keyword-item"><code class="xref docutils literal notranslate"><span class="pre">keyword_item</span></code></a> (&quot;,&quot; <a class="reference internal" href="#grammar-token-keyword-item"><code class="xref docutils literal notranslate"><span class="pre">keyword_item</span></code></a>)*
<strong id="grammar-token-keyword-item">keyword_item        </strong> ::=  <a class="reference internal" href="lexical_analysis.html#grammar-token-identifier"><code class="xref docutils literal notranslate"><span class="pre">identifier</span></code></a> &quot;=&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
</pre>
<p>A trailing comma may be present after the positional and keyword arguments but
does not affect the semantics.</p>
<p id="index-39">The primary must evaluate to a callable object (user-defined functions, built-in
functions, methods of built-in objects, class objects, methods of class
instances, and certain class instances themselves are callable; extensions may
define additional callable object types).  All argument expressions are
evaluated before the call is attempted.  Please refer to section <a class="reference internal" href="compound_stmts.html#function"><span class="std std-ref">Function definitions</span></a>
for the syntax of formal <a class="reference internal" href="../glossary.html#term-parameter"><span class="xref std std-term">parameter</span></a> lists.</p>
<p>If keyword arguments are present, they are first converted to positional
arguments, as follows.  First, a list of unfilled slots is created for the
formal parameters.  If there are N positional arguments, they are placed in the
first N slots.  Next, for each keyword argument, the identifier is used to
determine the corresponding slot (if the identifier is the same as the first
formal parameter name, the first slot is used, and so on).  If the slot is
already filled, a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised. Otherwise, the value of
the argument is placed in the slot, filling it (even if the expression is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, it fills the slot).  When all arguments have been processed, the slots
that are still unfilled are filled with the corresponding default value from the
function definition.  (Default values are calculated, once, when the function is
defined; thus, a mutable object such as a list or dictionary used as default
value will be shared by all calls that don’t specify an argument value for the
corresponding slot; this should usually be avoided.)  If there are any unfilled
slots for which no default value is specified, a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is
raised.  Otherwise, the list of filled slots is used as the argument list for
the call.</p>
<div class="impl-detail compound">
<p><strong>CPython implementation detail:</strong> An implementation may provide built-in functions whose positional parameters
do not have names, even if they are ‘named’ for the purpose of documentation,
and which therefore cannot be supplied by keyword.  In CPython, this is the
case for functions implemented in C that use <a class="reference internal" href="../c-api/arg.html#c.PyArg_ParseTuple" title="PyArg_ParseTuple"><code class="xref c c-func docutils literal notranslate"><span class="pre">PyArg_ParseTuple()</span></code></a> to
parse their arguments.</p>
</div>
<p>If there are more positional arguments than there are formal parameter slots, a
<a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised, unless a formal parameter using the syntax
<code class="docutils literal notranslate"><span class="pre">*identifier</span></code> is present; in this case, that formal parameter receives a tuple
containing the excess positional arguments (or an empty tuple if there were no
excess positional arguments).</p>
<p>If any keyword argument does not correspond to a formal parameter name, a
<a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised, unless a formal parameter using the syntax
<code class="docutils literal notranslate"><span class="pre">**identifier</span></code> is present; in this case, that formal parameter receives a
dictionary containing the excess keyword arguments (using the keywords as keys
and the argument values as corresponding values), or a (new) empty dictionary if
there were no excess keyword arguments.</p>
<p id="index-40">If the syntax <code class="docutils literal notranslate"><span class="pre">*expression</span></code> appears in the function call, <code class="docutils literal notranslate"><span class="pre">expression</span></code> must
evaluate to an iterable.  Elements from this iterable are treated as if they
were additional positional arguments; if there are positional arguments
<em>x1</em>, …, <em>xN</em>, and <code class="docutils literal notranslate"><span class="pre">expression</span></code> evaluates to a sequence <em>y1</em>, …, <em>yM</em>, this
is equivalent to a call with M+N positional arguments <em>x1</em>, …, <em>xN</em>, <em>y1</em>,
…, <em>yM</em>.</p>
<p>A consequence of this is that although the <code class="docutils literal notranslate"><span class="pre">*expression</span></code> syntax may appear
<em>after</em> some keyword arguments, it is processed <em>before</em> the keyword arguments
(and the <code class="docutils literal notranslate"><span class="pre">**expression</span></code> argument, if any – see below).  So:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nb">print</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mi">2</span><span class="p">,))</span>
<span class="go">2 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mi">2</span><span class="p">,))</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n">&lt;module&gt;</span>
<span class="gr">TypeError</span>: <span class="n">f() got multiple values for keyword argument &#39;a&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mi">2</span><span class="p">,))</span>
<span class="go">1 2</span>
</pre></div>
</div>
<p>It is unusual for both keyword arguments and the <code class="docutils literal notranslate"><span class="pre">*expression</span></code> syntax to be
used in the same call, so in practice this confusion does not arise.</p>
<p id="index-41">If the syntax <code class="docutils literal notranslate"><span class="pre">**expression</span></code> appears in the function call, <code class="docutils literal notranslate"><span class="pre">expression</span></code> must
evaluate to a mapping, the contents of which are treated as additional keyword
arguments.  In the case of a keyword appearing in both <code class="docutils literal notranslate"><span class="pre">expression</span></code> and as an
explicit keyword argument, a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised.</p>
<p>Formal parameters using the syntax <code class="docutils literal notranslate"><span class="pre">*identifier</span></code> or <code class="docutils literal notranslate"><span class="pre">**identifier</span></code> cannot be
used as positional argument slots or as keyword argument names.  Formal
parameters using the syntax <code class="docutils literal notranslate"><span class="pre">(sublist)</span></code> cannot be used as keyword argument
names; the outermost sublist corresponds to a single unnamed argument slot, and
the argument value is assigned to the sublist using the usual tuple assignment
rules after all other parameter processing is done.</p>
<p>A call always returns some value, possibly <code class="docutils literal notranslate"><span class="pre">None</span></code>, unless it raises an
exception.  How this value is computed depends on the type of the callable
object.</p>
<p>If it is—</p>
<dl>
<dt>a user-defined function:</dt><dd><p id="index-42">The code block for the function is executed, passing it the argument list.  The
first thing the code block will do is bind the formal parameters to the
arguments; this is described in section <a class="reference internal" href="compound_stmts.html#function"><span class="std std-ref">Function definitions</span></a>.  When the code block
executes a <a class="reference internal" href="simple_stmts.html#return"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">return</span></code></a> statement, this specifies the return value of the
function call.</p>
</dd>
<dt>a built-in function or method:</dt><dd><p id="index-43">The result is up to the interpreter; see <a class="reference internal" href="../library/functions.html#built-in-funcs"><span class="std std-ref">Built-in Functions</span></a> for the
descriptions of built-in functions and methods.</p>
</dd>
<dt>a class object:</dt><dd><p id="index-44">A new instance of that class is returned.</p>
</dd>
<dt>a class instance method:</dt><dd><p id="index-45">The corresponding user-defined function is called, with an argument list that is
one longer than the argument list of the call: the instance becomes the first
argument.</p>
</dd>
<dt>a class instance:</dt><dd><p id="index-46">The class must define a <a class="reference internal" href="datamodel.html#object.__call__" title="object.__call__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__call__()</span></code></a> method; the effect is then the same as
if that method was called.</p>
</dd>
</dl>
</div>
</div>
<div class="section" id="the-power-operator">
<span id="power"></span><h2>5.4. The power operator<a class="headerlink" href="#the-power-operator" title="Permalink to this headline">¶</a></h2>
<p>The power operator binds more tightly than unary operators on its left; it binds
less tightly than unary operators on its right.  The syntax is:</p>
<pre>
<strong id="grammar-token-power">power</strong> ::=  <a class="reference internal" href="#grammar-token-primary"><code class="xref docutils literal notranslate"><span class="pre">primary</span></code></a> [&quot;**&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a>]
</pre>
<p>Thus, in an unparenthesized sequence of power and unary operators, the operators
are evaluated from right to left (this does not constrain the evaluation order
for the operands): <code class="docutils literal notranslate"><span class="pre">-1**2</span></code> results in <code class="docutils literal notranslate"><span class="pre">-1</span></code>.</p>
<p>The power operator has the same semantics as the built-in <a class="reference internal" href="../library/functions.html#pow" title="pow"><code class="xref py py-func docutils literal notranslate"><span class="pre">pow()</span></code></a> function,
when called with two arguments: it yields its left argument raised to the power
of its right argument.  The numeric arguments are first converted to a common
type.  The result type is that of the arguments after coercion.</p>
<p>With mixed operand types, the coercion rules for binary arithmetic operators
apply. For int and long int operands, the result has the same type as the
operands (after coercion) unless the second argument is negative; in that case,
all arguments are converted to float and a float result is delivered. For
example, <code class="docutils literal notranslate"><span class="pre">10**2</span></code> returns <code class="docutils literal notranslate"><span class="pre">100</span></code>, but <code class="docutils literal notranslate"><span class="pre">10**-2</span></code> returns <code class="docutils literal notranslate"><span class="pre">0.01</span></code>. (This last
feature was added in Python 2.2. In Python 2.1 and before, if both arguments
were of integer types and the second argument was negative, an exception was
raised).</p>
<p>Raising <code class="docutils literal notranslate"><span class="pre">0.0</span></code> to a negative power results in a <a class="reference internal" href="../library/exceptions.html#exceptions.ZeroDivisionError" title="exceptions.ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a>.
Raising a negative number to a fractional power results in a <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>.</p>
</div>
<div class="section" id="unary-arithmetic-and-bitwise-operations">
<span id="unary"></span><h2>5.5. Unary arithmetic and bitwise operations<a class="headerlink" href="#unary-arithmetic-and-bitwise-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-47">All unary arithmetic and bitwise operations have the same priority:</p>
<pre>
<strong id="grammar-token-u-expr">u_expr</strong> ::=  <a class="reference internal" href="#grammar-token-power"><code class="xref docutils literal notranslate"><span class="pre">power</span></code></a> | &quot;-&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a> | &quot;+&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a> | &quot;~&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a>
</pre>
<p id="index-48">The unary <code class="docutils literal notranslate"><span class="pre">-</span></code> (minus) operator yields the negation of its numeric argument.</p>
<p id="index-49">The unary <code class="docutils literal notranslate"><span class="pre">+</span></code> (plus) operator yields its numeric argument unchanged.</p>
<p id="index-50">The unary <code class="docutils literal notranslate"><span class="pre">~</span></code> (invert) operator yields the bitwise inversion of its plain or
long integer argument.  The bitwise inversion of <code class="docutils literal notranslate"><span class="pre">x</span></code> is defined as
<code class="docutils literal notranslate"><span class="pre">-(x+1)</span></code>.  It only applies to integral numbers.</p>
<p id="index-51">In all three cases, if the argument does not have the proper type, a
<a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> exception is raised.</p>
</div>
<div class="section" id="binary-arithmetic-operations">
<span id="binary"></span><h2>5.6. Binary arithmetic operations<a class="headerlink" href="#binary-arithmetic-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-52">The binary arithmetic operations have the conventional priority levels.  Note
that some of these operations also apply to certain non-numeric types.  Apart
from the power operator, there are only two levels, one for multiplicative
operators and one for additive operators:</p>
<pre>
<strong id="grammar-token-m-expr">m_expr</strong> ::=  <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a> | <a class="reference internal" href="#grammar-token-m-expr"><code class="xref docutils literal notranslate"><span class="pre">m_expr</span></code></a> &quot;*&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a> | <a class="reference internal" href="#grammar-token-m-expr"><code class="xref docutils literal notranslate"><span class="pre">m_expr</span></code></a> &quot;//&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a> | <a class="reference internal" href="#grammar-token-m-expr"><code class="xref docutils literal notranslate"><span class="pre">m_expr</span></code></a> &quot;/&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a>
            | <a class="reference internal" href="#grammar-token-m-expr"><code class="xref docutils literal notranslate"><span class="pre">m_expr</span></code></a> &quot;%&quot; <a class="reference internal" href="#grammar-token-u-expr"><code class="xref docutils literal notranslate"><span class="pre">u_expr</span></code></a>
<strong id="grammar-token-a-expr">a_expr</strong> ::=  <a class="reference internal" href="#grammar-token-m-expr"><code class="xref docutils literal notranslate"><span class="pre">m_expr</span></code></a> | <a class="reference internal" href="#grammar-token-a-expr"><code class="xref docutils literal notranslate"><span class="pre">a_expr</span></code></a> &quot;+&quot; <a class="reference internal" href="#grammar-token-m-expr"><code class="xref docutils literal notranslate"><span class="pre">m_expr</span></code></a> | <a class="reference internal" href="#grammar-token-a-expr"><code class="xref docutils literal notranslate"><span class="pre">a_expr</span></code></a> &quot;-&quot; <a class="reference internal" href="#grammar-token-m-expr"><code class="xref docutils literal notranslate"><span class="pre">m_expr</span></code></a>
</pre>
<p id="index-53">The <code class="docutils literal notranslate"><span class="pre">*</span></code> (multiplication) operator yields the product of its arguments.  The
arguments must either both be numbers, or one argument must be an integer (plain
or long) and the other must be a sequence. In the former case, the numbers are
converted to a common type and then multiplied together.  In the latter case,
sequence repetition is performed; a negative repetition factor yields an empty
sequence.</p>
<p id="index-54">The <code class="docutils literal notranslate"><span class="pre">/</span></code> (division) and <code class="docutils literal notranslate"><span class="pre">//</span></code> (floor division) operators yield the quotient of
their arguments.  The numeric arguments are first converted to a common type.
Plain or long integer division yields an integer of the same type; the result is
that of mathematical division with the ‘floor’ function applied to the result.
Division by zero raises the <a class="reference internal" href="../library/exceptions.html#exceptions.ZeroDivisionError" title="exceptions.ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> exception.</p>
<p id="index-55">The <code class="docutils literal notranslate"><span class="pre">%</span></code> (modulo) operator yields the remainder from the division of the first
argument by the second.  The numeric arguments are first converted to a common
type.  A zero right argument raises the <a class="reference internal" href="../library/exceptions.html#exceptions.ZeroDivisionError" title="exceptions.ZeroDivisionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ZeroDivisionError</span></code></a> exception.  The
arguments may be floating point numbers, e.g., <code class="docutils literal notranslate"><span class="pre">3.14%0.7</span></code> equals <code class="docutils literal notranslate"><span class="pre">0.34</span></code>
(since <code class="docutils literal notranslate"><span class="pre">3.14</span></code> equals <code class="docutils literal notranslate"><span class="pre">4*0.7</span> <span class="pre">+</span> <span class="pre">0.34</span></code>.)  The modulo operator always yields a
result with the same sign as its second operand (or zero); the absolute value of
the result is strictly smaller than the absolute value of the second operand
<a class="footnote-reference brackets" href="#id20" id="id10">2</a>.</p>
<p>The integer division and modulo operators are connected by the following
identity: <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">(x/y)*y</span> <span class="pre">+</span> <span class="pre">(x%y)</span></code>.  Integer division and modulo are also
connected with the built-in function <a class="reference internal" href="../library/functions.html#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a>: <code class="docutils literal notranslate"><span class="pre">divmod(x,</span> <span class="pre">y)</span> <span class="pre">==</span> <span class="pre">(x/y,</span>
<span class="pre">x%y)</span></code>.  These identities don’t hold for floating point numbers; there similar
identities hold approximately where <code class="docutils literal notranslate"><span class="pre">x/y</span></code> is replaced by <code class="docutils literal notranslate"><span class="pre">floor(x/y)</span></code> or
<code class="docutils literal notranslate"><span class="pre">floor(x/y)</span> <span class="pre">-</span> <span class="pre">1</span></code> <a class="footnote-reference brackets" href="#id21" id="id11">3</a>.</p>
<p>In addition to performing the modulo operation on numbers, the <code class="docutils literal notranslate"><span class="pre">%</span></code> operator is
also overloaded by string and unicode objects to perform string formatting (also
known as interpolation). The syntax for string formatting is described in the
Python Library Reference, section <a class="reference internal" href="../library/stdtypes.html#string-formatting"><span class="std std-ref">String Formatting Operations</span></a>.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 2.3: </span>The floor division operator, the modulo operator, and the <a class="reference internal" href="../library/functions.html#divmod" title="divmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">divmod()</span></code></a>
function are no longer defined for complex numbers.  Instead, convert to a
floating point number using the <a class="reference internal" href="../library/functions.html#abs" title="abs"><code class="xref py py-func docutils literal notranslate"><span class="pre">abs()</span></code></a> function if appropriate.</p>
</div>
<p id="index-56">The <code class="docutils literal notranslate"><span class="pre">+</span></code> (addition) operator yields the sum of its arguments. The arguments
must either both be numbers or both sequences of the same type.  In the former
case, the numbers are converted to a common type and then added together.  In
the latter case, the sequences are concatenated.</p>
<p id="index-57">The <code class="docutils literal notranslate"><span class="pre">-</span></code> (subtraction) operator yields the difference of its arguments.  The
numeric arguments are first converted to a common type.</p>
</div>
<div class="section" id="shifting-operations">
<span id="shifting"></span><h2>5.7. Shifting operations<a class="headerlink" href="#shifting-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-58">The shifting operations have lower priority than the arithmetic operations:</p>
<pre>
<strong id="grammar-token-shift-expr">shift_expr</strong> ::=  <a class="reference internal" href="#grammar-token-a-expr"><code class="xref docutils literal notranslate"><span class="pre">a_expr</span></code></a> | <a class="reference internal" href="#grammar-token-shift-expr"><code class="xref docutils literal notranslate"><span class="pre">shift_expr</span></code></a> ( &quot;&lt;&lt;&quot; | &quot;&gt;&gt;&quot; ) <a class="reference internal" href="#grammar-token-a-expr"><code class="xref docutils literal notranslate"><span class="pre">a_expr</span></code></a>
</pre>
<p>These operators accept plain or long integers as arguments.  The arguments are
converted to a common type.  They shift the first argument to the left or right
by the number of bits given by the second argument.</p>
<p id="index-59">A right shift by <em>n</em> bits is defined as division by <code class="docutils literal notranslate"><span class="pre">pow(2,</span> <span class="pre">n)</span></code>.  A left shift
by <em>n</em> bits is defined as multiplication with <code class="docutils literal notranslate"><span class="pre">pow(2,</span> <span class="pre">n)</span></code>.  Negative shift
counts raise a <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> exception.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>In the current implementation, the right-hand operand is required
to be at most <a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a>.  If the right-hand operand is larger than
<a class="reference internal" href="../library/sys.html#sys.maxsize" title="sys.maxsize"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys.maxsize</span></code></a> an <a class="reference internal" href="../library/exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OverflowError</span></code></a> exception is raised.</p>
</div>
</div>
<div class="section" id="binary-bitwise-operations">
<span id="bitwise"></span><h2>5.8. Binary bitwise operations<a class="headerlink" href="#binary-bitwise-operations" title="Permalink to this headline">¶</a></h2>
<p id="index-60">Each of the three bitwise operations has a different priority level:</p>
<pre>
<strong id="grammar-token-and-expr">and_expr</strong> ::=  <a class="reference internal" href="#grammar-token-shift-expr"><code class="xref docutils literal notranslate"><span class="pre">shift_expr</span></code></a> | <a class="reference internal" href="#grammar-token-and-expr"><code class="xref docutils literal notranslate"><span class="pre">and_expr</span></code></a> &quot;&amp;&quot; <a class="reference internal" href="#grammar-token-shift-expr"><code class="xref docutils literal notranslate"><span class="pre">shift_expr</span></code></a>
<strong id="grammar-token-xor-expr">xor_expr</strong> ::=  <a class="reference internal" href="#grammar-token-and-expr"><code class="xref docutils literal notranslate"><span class="pre">and_expr</span></code></a> | <a class="reference internal" href="#grammar-token-xor-expr"><code class="xref docutils literal notranslate"><span class="pre">xor_expr</span></code></a> &quot;^&quot; <a class="reference internal" href="#grammar-token-and-expr"><code class="xref docutils literal notranslate"><span class="pre">and_expr</span></code></a>
<strong id="grammar-token-or-expr">or_expr </strong> ::=  <a class="reference internal" href="#grammar-token-xor-expr"><code class="xref docutils literal notranslate"><span class="pre">xor_expr</span></code></a> | <a class="reference internal" href="#grammar-token-or-expr"><code class="xref docutils literal notranslate"><span class="pre">or_expr</span></code></a> &quot;|&quot; <a class="reference internal" href="#grammar-token-xor-expr"><code class="xref docutils literal notranslate"><span class="pre">xor_expr</span></code></a>
</pre>
<p id="index-61">The <code class="docutils literal notranslate"><span class="pre">&amp;</span></code> operator yields the bitwise AND of its arguments, which must be plain
or long integers.  The arguments are converted to a common type.</p>
<p id="index-62">The <code class="docutils literal notranslate"><span class="pre">^</span></code> operator yields the bitwise XOR (exclusive OR) of its arguments, which
must be plain or long integers.  The arguments are converted to a common type.</p>
<p id="index-63">The <code class="docutils literal notranslate"><span class="pre">|</span></code> operator yields the bitwise (inclusive) OR of its arguments, which
must be plain or long integers.  The arguments are converted to a common type.</p>
</div>
<div class="section" id="comparisons">
<span id="id12"></span><h2>5.9. Comparisons<a class="headerlink" href="#comparisons" title="Permalink to this headline">¶</a></h2>
<span class="target" id="index-64"></span><p id="index-65">Unlike C, all comparison operations in Python have the same priority, which is
lower than that of any arithmetic, shifting or bitwise operation.  Also unlike
C, expressions like <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">&lt;</span> <span class="pre">b</span> <span class="pre">&lt;</span> <span class="pre">c</span></code> have the interpretation that is conventional
in mathematics:</p>
<pre>
<strong id="grammar-token-comparison">comparison   </strong> ::=  <a class="reference internal" href="#grammar-token-or-expr"><code class="xref docutils literal notranslate"><span class="pre">or_expr</span></code></a> ( <a class="reference internal" href="#grammar-token-comp-operator"><code class="xref docutils literal notranslate"><span class="pre">comp_operator</span></code></a> <a class="reference internal" href="#grammar-token-or-expr"><code class="xref docutils literal notranslate"><span class="pre">or_expr</span></code></a> )*
<strong id="grammar-token-comp-operator">comp_operator</strong> ::=  &quot;&lt;&quot; | &quot;&gt;&quot; | &quot;==&quot; | &quot;&gt;=&quot; | &quot;&lt;=&quot; | &quot;&lt;&gt;&quot; | &quot;!=&quot;
                   | &quot;is&quot; [&quot;not&quot;] | [&quot;not&quot;] &quot;in&quot;
</pre>
<p>Comparisons yield boolean values: <code class="docutils literal notranslate"><span class="pre">True</span></code> or <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
<p id="index-66">Comparisons can be chained arbitrarily, e.g., <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">&lt;=</span> <span class="pre">z</span></code> is equivalent to
<code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">and</span> <span class="pre">y</span> <span class="pre">&lt;=</span> <span class="pre">z</span></code>, except that <code class="docutils literal notranslate"><span class="pre">y</span></code> is evaluated only once (but in both
cases <code class="docutils literal notranslate"><span class="pre">z</span></code> is not evaluated at all when <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span></code> is found to be false).</p>
<p>Formally, if <em>a</em>, <em>b</em>, <em>c</em>, …, <em>y</em>, <em>z</em> are expressions and <em>op1</em>, <em>op2</em>, …,
<em>opN</em> are comparison operators, then <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span> <span class="pre">...</span> <span class="pre">y</span> <span class="pre">opN</span> <span class="pre">z</span></code> is equivalent
to <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">and</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span> <span class="pre">and</span> <span class="pre">...</span> <span class="pre">y</span> <span class="pre">opN</span> <span class="pre">z</span></code>, except that each expression is
evaluated at most once.</p>
<p>Note that <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">op1</span> <span class="pre">b</span> <span class="pre">op2</span> <span class="pre">c</span></code> doesn’t imply any kind of comparison between <em>a</em> and
<em>c</em>, so that, e.g., <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">&gt;</span> <span class="pre">z</span></code> is perfectly legal (though perhaps not
pretty).</p>
<p>The forms <code class="docutils literal notranslate"><span class="pre">&lt;&gt;</span></code> and <code class="docutils literal notranslate"><span class="pre">!=</span></code> are equivalent; for consistency with C, <code class="docutils literal notranslate"><span class="pre">!=</span></code> is
preferred; where <code class="docutils literal notranslate"><span class="pre">!=</span></code> is mentioned below <code class="docutils literal notranslate"><span class="pre">&lt;&gt;</span></code> is also accepted.  The <code class="docutils literal notranslate"><span class="pre">&lt;&gt;</span></code>
spelling is considered obsolescent.</p>
<div class="section" id="value-comparisons">
<h3>5.9.1. Value comparisons<a class="headerlink" href="#value-comparisons" title="Permalink to this headline">¶</a></h3>
<p>The operators <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">==</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, and <code class="docutils literal notranslate"><span class="pre">!=</span></code> compare the
values of two objects.  The objects do not need to have the same type.</p>
<p>Chapter <a class="reference internal" href="datamodel.html#objects"><span class="std std-ref">Objects, values and types</span></a> states that objects have a value (in addition to type
and identity).  The value of an object is a rather abstract notion in Python:
For example, there is no canonical access method for an object’s value.  Also,
there is no requirement that the value of an object should be constructed in a
particular way, e.g. comprised of all its data attributes. Comparison operators
implement a particular notion of what the value of an object is.  One can think
of them as defining the value of an object indirectly, by means of their
comparison implementation.</p>
<p>Types can customize their comparison behavior by implementing
a <a class="reference internal" href="datamodel.html#object.__cmp__" title="object.__cmp__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__cmp__()</span></code></a> method or
<em class="dfn">rich comparison methods</em> like <a class="reference internal" href="datamodel.html#object.__lt__" title="object.__lt__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__lt__()</span></code></a>, described in
<a class="reference internal" href="datamodel.html#customization"><span class="std std-ref">Basic customization</span></a>.</p>
<p>The default behavior for equality comparison (<code class="docutils literal notranslate"><span class="pre">==</span></code> and <code class="docutils literal notranslate"><span class="pre">!=</span></code>) is based on
the identity of the objects.  Hence, equality comparison of instances with the
same identity results in equality, and equality comparison of instances with
different identities results in inequality.  A motivation for this default
behavior is the desire that all objects should be reflexive (i.e. <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">y</span></code>
implies <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></code>).</p>
<p>The default order comparison (<code class="docutils literal notranslate"><span class="pre">&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, and <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>) gives a
consistent but arbitrary order.</p>
<p>(This unusual definition of comparison was used to simplify the definition of
operations like sorting and the <a class="reference internal" href="#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> and <a class="reference internal" href="#not-in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code></a> operators.
In the future, the comparison rules for objects of different types are likely to
change.)</p>
<p>The behavior of the default equality comparison, that instances with different
identities are always unequal, may be in contrast to what types will need that
have a sensible definition of object value and value-based equality.  Such
types will need to customize their comparison behavior, and in fact, a number
of built-in types have done that.</p>
<p>The following list describes the comparison behavior of the most important
built-in types.</p>
<ul>
<li><p>Numbers of built-in numeric types (<a class="reference internal" href="../library/stdtypes.html#typesnumeric"><span class="std std-ref">Numeric Types — int, float, long, complex</span></a>) and of the standard
library types <a class="reference internal" href="../library/fractions.html#fractions.Fraction" title="fractions.Fraction"><code class="xref py py-class docutils literal notranslate"><span class="pre">fractions.Fraction</span></code></a> and <a class="reference internal" href="../library/decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a> can be
compared within and across their types, with the restriction that complex
numbers do not support order comparison.  Within the limits of the types
involved, they compare mathematically (algorithmically) correct without loss
of precision.</p></li>
<li><p>Strings (instances of <a class="reference internal" href="../library/functions.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> or <a class="reference internal" href="../library/functions.html#unicode" title="unicode"><code class="xref py py-class docutils literal notranslate"><span class="pre">unicode</span></code></a>)
compare lexicographically using the numeric equivalents (the
result of the built-in function <a class="reference internal" href="../library/functions.html#ord" title="ord"><code class="xref py py-func docutils literal notranslate"><span class="pre">ord()</span></code></a>) of their characters. <a class="footnote-reference brackets" href="#id22" id="id13">4</a>
When comparing an 8-bit string and a Unicode string, the 8-bit string
is converted to Unicode.  If the conversion fails, the strings
are considered unequal.</p></li>
<li><p>Instances of <a class="reference internal" href="../library/functions.html#tuple" title="tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">tuple</span></code></a> or <code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code> can be compared only
within each of their types.  Equality comparison across these types
results in unequality, and ordering comparison across these types
gives an arbitrary order.</p>
<p>These sequences compare lexicographically using comparison of corresponding
elements, whereby reflexivity of the elements is enforced.</p>
<p>In enforcing reflexivity of elements, the comparison of collections assumes
that for a collection element <code class="docutils literal notranslate"><span class="pre">x</span></code>, <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">x</span></code> is always true.  Based on
that assumption, element identity is compared first, and element comparison
is performed only for distinct elements.  This approach yields the same
result as a strict element comparison would, if the compared elements are
reflexive.  For non-reflexive elements, the result is different than for
strict element comparison.</p>
<p>Lexicographical comparison between built-in collections works as follows:</p>
<ul class="simple">
<li><p>For two collections to compare equal, they must be of the same type, have
the same length, and each pair of corresponding elements must compare
equal (for example, <code class="docutils literal notranslate"><span class="pre">[1,2]</span> <span class="pre">==</span> <span class="pre">(1,2)</span></code> is false because the type is not the
same).</p></li>
<li><p>Collections are ordered the same as their
first unequal elements (for example, <code class="docutils literal notranslate"><span class="pre">cmp([1,2,x],</span> <span class="pre">[1,2,y])</span></code> returns the
same as <code class="docutils literal notranslate"><span class="pre">cmp(x,y)</span></code>).  If a corresponding element does not exist, the
shorter collection is ordered first (for example, <code class="docutils literal notranslate"><span class="pre">[1,2]</span> <span class="pre">&lt;</span> <span class="pre">[1,2,3]</span></code> is
true).</p></li>
</ul>
</li>
<li><p>Mappings (instances of <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>) compare equal if and only if they have
equal <cite>(key, value)</cite> pairs. Equality comparison of the keys and values
enforces reflexivity.</p>
<p>Outcomes other than equality are resolved
consistently, but are not otherwise defined. <a class="footnote-reference brackets" href="#id23" id="id14">5</a></p>
</li>
<li><p>Most other objects of built-in types compare unequal unless they are the same
object; the choice whether one object is considered smaller or larger than
another one is made arbitrarily but consistently within one execution of a
program.</p></li>
</ul>
<p>User-defined classes that customize their comparison behavior should follow
some consistency rules, if possible:</p>
<ul>
<li><p>Equality comparison should be reflexive.
In other words, identical objects should compare equal:</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">y</span></code> implies <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></code></p>
</div></blockquote>
</li>
<li><p>Comparison should be symmetric.
In other words, the following expressions should have the same result:</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span> <span class="pre">==</span> <span class="pre">x</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">!=</span> <span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span> <span class="pre">!=</span> <span class="pre">x</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span> <span class="pre">&gt;</span> <span class="pre">x</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;=</span> <span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">y</span> <span class="pre">&gt;=</span> <span class="pre">x</span></code></p>
</div></blockquote>
</li>
<li><p>Comparison should be transitive.
The following (non-exhaustive) examples illustrate that:</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&gt;</span> <span class="pre">y</span> <span class="pre">and</span> <span class="pre">y</span> <span class="pre">&gt;</span> <span class="pre">z</span></code> implies <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&gt;</span> <span class="pre">z</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span> <span class="pre">and</span> <span class="pre">y</span> <span class="pre">&lt;=</span> <span class="pre">z</span></code> implies <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">z</span></code></p>
</div></blockquote>
</li>
<li><p>Inverse comparison should result in the boolean negation.
In other words, the following expressions should have the same result:</p>
<blockquote>
<div><p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">x</span> <span class="pre">!=</span> <span class="pre">y</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">x</span> <span class="pre">&gt;=</span> <span class="pre">y</span></code> (for total ordering)</p>
<p><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">&gt;</span> <span class="pre">y</span></code> and <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">x</span> <span class="pre">&lt;=</span> <span class="pre">y</span></code> (for total ordering)</p>
</div></blockquote>
<p>The last two expressions apply to totally ordered collections (e.g. to
sequences, but not to sets or mappings). See also the
<a class="reference internal" href="../library/functools.html#functools.total_ordering" title="functools.total_ordering"><code class="xref py py-func docutils literal notranslate"><span class="pre">total_ordering()</span></code></a> decorator.</p>
</li>
<li><p>The <a class="reference internal" href="../library/functions.html#hash" title="hash"><code class="xref py py-func docutils literal notranslate"><span class="pre">hash()</span></code></a> result should be consistent with equality.
Objects that are equal should either have the same hash value,
or be marked as unhashable.</p></li>
</ul>
<p>Python does not enforce these consistency rules.</p>
</div>
<div class="section" id="membership-test-operations">
<span id="membership-test-details"></span><span id="not-in"></span><span id="in"></span><h3>5.9.2. Membership test operations<a class="headerlink" href="#membership-test-operations" title="Permalink to this headline">¶</a></h3>
<p>The operators <a class="reference internal" href="#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> and <a class="reference internal" href="#not-in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code></a> test for membership.  <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span>
<span class="pre">s</span></code> evaluates to <code class="docutils literal notranslate"><span class="pre">True</span></code> if <em>x</em> is a member of <em>s</em>, and <code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.
<code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">s</span></code> returns the negation of <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></code>.  All built-in sequences and
set types support this as well as dictionary, for which <a class="reference internal" href="#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> tests
whether the dictionary has a given key. For container types such as list, tuple,
set, frozenset, dict, or collections.deque, the expression <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></code> is equivalent
to <code class="docutils literal notranslate"><span class="pre">any(x</span> <span class="pre">is</span> <span class="pre">e</span> <span class="pre">or</span> <span class="pre">x</span> <span class="pre">==</span> <span class="pre">e</span> <span class="pre">for</span> <span class="pre">e</span> <span class="pre">in</span> <span class="pre">y)</span></code>.</p>
<p>For the string and bytes types, <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> if and only if <em>x</em> is a
substring of <em>y</em>.  An equivalent test is <code class="docutils literal notranslate"><span class="pre">y.find(x)</span> <span class="pre">!=</span> <span class="pre">-1</span></code>.  Empty strings are
always considered to be a substring of any other string, so <code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span> <span class="pre">in</span> <span class="pre">&quot;abc&quot;</span></code> will
return <code class="docutils literal notranslate"><span class="pre">True</span></code>.</p>
<p>For user-defined classes which define the <a class="reference internal" href="datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code></a> method, <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span>
<span class="pre">y</span></code> returns <code class="docutils literal notranslate"><span class="pre">True</span></code> if <code class="docutils literal notranslate"><span class="pre">y.__contains__(x)</span></code> returns a true value, and
<code class="docutils literal notranslate"><span class="pre">False</span></code> otherwise.</p>
<p>For user-defined classes which do not define <a class="reference internal" href="datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__contains__()</span></code></a> but do define
<a class="reference internal" href="datamodel.html#object.__iter__" title="object.__iter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__iter__()</span></code></a>, <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> if some value <code class="docutils literal notranslate"><span class="pre">z</span></code> with <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">z</span></code> is
produced while iterating over <code class="docutils literal notranslate"><span class="pre">y</span></code>.  If an exception is raised during the
iteration, it is as if <a class="reference internal" href="#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> raised that exception.</p>
<p>Lastly, the old-style iteration protocol is tried: if a class defines
<a class="reference internal" href="datamodel.html#object.__getitem__" title="object.__getitem__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__getitem__()</span></code></a>, <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></code> is <code class="docutils literal notranslate"><span class="pre">True</span></code> if and only if there is a non-negative
integer index <em>i</em> such that <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">==</span> <span class="pre">y[i]</span></code>, and all lower integer indices do not
raise <a class="reference internal" href="../library/exceptions.html#exceptions.IndexError" title="exceptions.IndexError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IndexError</span></code></a> exception. (If any other exception is raised, it is as
if <a class="reference internal" href="#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a> raised that exception).</p>
<p id="index-67">The operator <a class="reference internal" href="#not-in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code></a> is defined to have the inverse true value of
<a class="reference internal" href="#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a>.</p>
</div>
<div class="section" id="is-not">
<span id="is"></span><span id="index-68"></span><span id="identity-comparisons"></span><h3>5.9.3. Identity comparisons<a class="headerlink" href="#is-not" title="Permalink to this headline">¶</a></h3>
<p>The operators <a class="reference internal" href="#is"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">is</span></code></a> and <a class="reference internal" href="#is-not"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code></a> test for object identity: <code class="docutils literal notranslate"><span class="pre">x</span>
<span class="pre">is</span> <span class="pre">y</span></code> is true if and only if <em>x</em> and <em>y</em> are the same object.  <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">y</span></code>
yields the inverse truth value. <a class="footnote-reference brackets" href="#id24" id="id15">6</a></p>
</div>
</div>
<div class="section" id="boolean-operations">
<span id="not"></span><span id="or"></span><span id="and"></span><span id="booleans"></span><h2>5.10. Boolean operations<a class="headerlink" href="#boolean-operations" title="Permalink to this headline">¶</a></h2>
<pre id="index-69">
<strong id="grammar-token-or-test">or_test </strong> ::=  <a class="reference internal" href="#grammar-token-and-test"><code class="xref docutils literal notranslate"><span class="pre">and_test</span></code></a> | <a class="reference internal" href="#grammar-token-or-test"><code class="xref docutils literal notranslate"><span class="pre">or_test</span></code></a> &quot;or&quot; <a class="reference internal" href="#grammar-token-and-test"><code class="xref docutils literal notranslate"><span class="pre">and_test</span></code></a>
<strong id="grammar-token-and-test">and_test</strong> ::=  <a class="reference internal" href="#grammar-token-not-test"><code class="xref docutils literal notranslate"><span class="pre">not_test</span></code></a> | <a class="reference internal" href="#grammar-token-and-test"><code class="xref docutils literal notranslate"><span class="pre">and_test</span></code></a> &quot;and&quot; <a class="reference internal" href="#grammar-token-not-test"><code class="xref docutils literal notranslate"><span class="pre">not_test</span></code></a>
<strong id="grammar-token-not-test">not_test</strong> ::=  <a class="reference internal" href="#grammar-token-comparison"><code class="xref docutils literal notranslate"><span class="pre">comparison</span></code></a> | &quot;not&quot; <a class="reference internal" href="#grammar-token-not-test"><code class="xref docutils literal notranslate"><span class="pre">not_test</span></code></a>
</pre>
<p>In the context of Boolean operations, and also when expressions are used by
control flow statements, the following values are interpreted as false:
<code class="docutils literal notranslate"><span class="pre">False</span></code>, <code class="docutils literal notranslate"><span class="pre">None</span></code>, numeric zero of all types, and empty strings and containers
(including strings, tuples, lists, dictionaries, sets and frozensets).  All
other values are interpreted as true.  (See the <a class="reference internal" href="datamodel.html#object.__nonzero__" title="object.__nonzero__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__nonzero__()</span></code></a>
special method for a way to change this.)</p>
<p id="index-70">The operator <a class="reference internal" href="#not"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">not</span></code></a> yields <code class="docutils literal notranslate"><span class="pre">True</span></code> if its argument is false, <code class="docutils literal notranslate"><span class="pre">False</span></code>
otherwise.</p>
<p id="index-71">The expression <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">and</span> <span class="pre">y</span></code> first evaluates <em>x</em>; if <em>x</em> is false, its value is
returned; otherwise, <em>y</em> is evaluated and the resulting value is returned.</p>
<p id="index-72">The expression <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">or</span> <span class="pre">y</span></code> first evaluates <em>x</em>; if <em>x</em> is true, its value is
returned; otherwise, <em>y</em> is evaluated and the resulting value is returned.</p>
<p>(Note that neither <a class="reference internal" href="#and"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">and</span></code></a> nor <a class="reference internal" href="#or"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">or</span></code></a> restrict the value and type
they return to <code class="docutils literal notranslate"><span class="pre">False</span></code> and <code class="docutils literal notranslate"><span class="pre">True</span></code>, but rather return the last evaluated
argument. This is sometimes useful, e.g., if <code class="docutils literal notranslate"><span class="pre">s</span></code> is a string that should be
replaced by a default value if it is empty, the expression <code class="docutils literal notranslate"><span class="pre">s</span> <span class="pre">or</span> <span class="pre">'foo'</span></code> yields
the desired value.  Because <a class="reference internal" href="#not"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">not</span></code></a> has to invent a value anyway, it does
not bother to return a value of the same type as its argument, so e.g., <code class="docutils literal notranslate"><span class="pre">not</span>
<span class="pre">'foo'</span></code> yields <code class="docutils literal notranslate"><span class="pre">False</span></code>, not <code class="docutils literal notranslate"><span class="pre">''</span></code>.)</p>
</div>
<div class="section" id="conditional-expressions">
<h2>5.11. Conditional Expressions<a class="headerlink" href="#conditional-expressions" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 2.5.</span></p>
</div>
<pre id="index-73">
<strong id="grammar-token-conditional-expression">conditional_expression</strong> ::=  <a class="reference internal" href="#grammar-token-or-test"><code class="xref docutils literal notranslate"><span class="pre">or_test</span></code></a> [&quot;if&quot; <a class="reference internal" href="#grammar-token-or-test"><code class="xref docutils literal notranslate"><span class="pre">or_test</span></code></a> &quot;else&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>]
<strong id="grammar-token-expression">expression            </strong> ::=  <a class="reference internal" href="#grammar-token-conditional-expression"><code class="xref docutils literal notranslate"><span class="pre">conditional_expression</span></code></a> | <a class="reference internal" href="#grammar-token-lambda-expr"><code class="xref docutils literal notranslate"><span class="pre">lambda_expr</span></code></a>
</pre>
<p>Conditional expressions (sometimes called a “ternary operator”) have the lowest
priority of all Python operations.</p>
<p>The expression <code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">if</span> <span class="pre">C</span> <span class="pre">else</span> <span class="pre">y</span></code> first evaluates the condition, <em>C</em> (<em>not</em> <em>x</em>);
if <em>C</em> is true, <em>x</em> is evaluated and its value is returned; otherwise, <em>y</em> is
evaluated and its value is returned.</p>
<p>See <span class="target" id="index-74"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0308"><strong>PEP 308</strong></a> for more details about conditional expressions.</p>
</div>
<div class="section" id="lambda">
<span id="lambdas"></span><span id="id16"></span><h2>5.12. Lambdas<a class="headerlink" href="#lambda" title="Permalink to this headline">¶</a></h2>
<pre id="index-75">
<strong id="grammar-token-lambda-expr">lambda_expr    </strong> ::=  &quot;lambda&quot; [<a class="reference internal" href="compound_stmts.html#grammar-token-parameter-list"><code class="xref docutils literal notranslate"><span class="pre">parameter_list</span></code></a>]: <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a>
<strong id="grammar-token-old-lambda-expr">old_lambda_expr</strong> ::=  &quot;lambda&quot; [<a class="reference internal" href="compound_stmts.html#grammar-token-parameter-list"><code class="xref docutils literal notranslate"><span class="pre">parameter_list</span></code></a>]: <a class="reference internal" href="#grammar-token-old-expression"><code class="xref docutils literal notranslate"><span class="pre">old_expression</span></code></a>
</pre>
<p>Lambda expressions (sometimes called lambda forms) have the same syntactic position as
expressions.  They are a shorthand to create anonymous functions; the expression
<code class="docutils literal notranslate"><span class="pre">lambda</span> <span class="pre">parameters:</span> <span class="pre">expression</span></code> yields a function object.  The unnamed object
behaves like a function object defined with</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>def &lt;lambda&gt;(parameters):
    return expression
</pre></div>
</div>
<p>See section <a class="reference internal" href="compound_stmts.html#function"><span class="std std-ref">Function definitions</span></a> for the syntax of parameter lists.  Note that
functions created with lambda expressions cannot contain statements.</p>
</div>
<div class="section" id="expression-lists">
<span id="exprlists"></span><h2>5.13. Expression lists<a class="headerlink" href="#expression-lists" title="Permalink to this headline">¶</a></h2>
<pre id="index-76">
<strong id="grammar-token-expression-list">expression_list</strong> ::=  <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> ( &quot;,&quot; <a class="reference internal" href="#grammar-token-expression"><code class="xref docutils literal notranslate"><span class="pre">expression</span></code></a> )* [&quot;,&quot;]
</pre>
<p id="index-77">An expression list containing at least one comma yields a tuple.  The length of
the tuple is the number of expressions in the list.  The expressions are
evaluated from left to right.</p>
<p id="index-78">The trailing comma is required only to create a single tuple (a.k.a. a
<em>singleton</em>); it is optional in all other cases.  A single expression without a
trailing comma doesn’t create a tuple, but rather yields the value of that
expression. (To create an empty tuple, use an empty pair of parentheses:
<code class="docutils literal notranslate"><span class="pre">()</span></code>.)</p>
</div>
<div class="section" id="evaluation-order">
<span id="evalorder"></span><h2>5.14. Evaluation order<a class="headerlink" href="#evaluation-order" title="Permalink to this headline">¶</a></h2>
<p id="index-79">Python evaluates expressions from left to right. Notice that while evaluating an
assignment, the right-hand side is evaluated before the left-hand side.</p>
<p>In the following lines, expressions will be evaluated in the arithmetic order of
their suffixes:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">expr1</span><span class="p">,</span> <span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">,</span> <span class="n">expr4</span>
<span class="p">(</span><span class="n">expr1</span><span class="p">,</span> <span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">,</span> <span class="n">expr4</span><span class="p">)</span>
<span class="p">{</span><span class="n">expr1</span><span class="p">:</span> <span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">:</span> <span class="n">expr4</span><span class="p">}</span>
<span class="n">expr1</span> <span class="o">+</span> <span class="n">expr2</span> <span class="o">*</span> <span class="p">(</span><span class="n">expr3</span> <span class="o">-</span> <span class="n">expr4</span><span class="p">)</span>
<span class="n">expr1</span><span class="p">(</span><span class="n">expr2</span><span class="p">,</span> <span class="n">expr3</span><span class="p">,</span> <span class="o">*</span><span class="n">expr4</span><span class="p">,</span> <span class="o">**</span><span class="n">expr5</span><span class="p">)</span>
<span class="n">expr3</span><span class="p">,</span> <span class="n">expr4</span> <span class="o">=</span> <span class="n">expr1</span><span class="p">,</span> <span class="n">expr2</span>
</pre></div>
</div>
</div>
<div class="section" id="operator-precedence">
<span id="operator-summary"></span><h2>5.15. Operator precedence<a class="headerlink" href="#operator-precedence" title="Permalink to this headline">¶</a></h2>
<p id="index-80">The following table summarizes the operator precedences in Python, from lowest
precedence (least binding) to highest precedence (most binding). Operators in
the same box have the same precedence.  Unless the syntax is explicitly given,
operators are binary.  Operators in the same box group left to right (except for
comparisons, including tests, which all have the same precedence and chain from
left to right — see section <a class="reference internal" href="#comparisons"><span class="std std-ref">Comparisons</span></a> — and exponentiation, which
groups from right to left).</p>
<table class="docutils align-center">
<colgroup>
<col style="width: 56%" />
<col style="width: 44%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Operator</p></th>
<th class="head"><p>Description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><a class="reference internal" href="#lambda"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">lambda</span></code></a></p></td>
<td><p>Lambda expression</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="compound_stmts.html#if"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">if</span></code></a> – <a class="reference internal" href="compound_stmts.html#else"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">else</span></code></a></p></td>
<td><p>Conditional expression</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#or"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">or</span></code></a></p></td>
<td><p>Boolean OR</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#and"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">and</span></code></a></p></td>
<td><p>Boolean AND</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="#not"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">not</span></code></a> <code class="docutils literal notranslate"><span class="pre">x</span></code></p></td>
<td><p>Boolean NOT</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="#in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">in</span></code></a>, <a class="reference internal" href="#not-in"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">not</span> <span class="pre">in</span></code></a>,
<a class="reference internal" href="#is"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">is</span></code></a>, <a class="reference internal" href="#is-not"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">is</span> <span class="pre">not</span></code></a>, <code class="docutils literal notranslate"><span class="pre">&lt;</span></code>,
<code class="docutils literal notranslate"><span class="pre">&lt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;=</span></code>, <code class="docutils literal notranslate"><span class="pre">&lt;&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">!=</span></code>, <code class="docutils literal notranslate"><span class="pre">==</span></code></p></td>
<td><p>Comparisons, including membership
tests and identity tests</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">|</span></code></p></td>
<td><p>Bitwise OR</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">^</span></code></p></td>
<td><p>Bitwise XOR</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">&amp;</span></code></p></td>
<td><p>Bitwise AND</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">&lt;&lt;</span></code>, <code class="docutils literal notranslate"><span class="pre">&gt;&gt;</span></code></p></td>
<td><p>Shifts</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">+</span></code>, <code class="docutils literal notranslate"><span class="pre">-</span></code></p></td>
<td><p>Addition and subtraction</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">*</span></code>, <code class="docutils literal notranslate"><span class="pre">/</span></code>, <code class="docutils literal notranslate"><span class="pre">//</span></code>, <code class="docutils literal notranslate"><span class="pre">%</span></code></p></td>
<td><p>Multiplication, division, remainder
<a class="footnote-reference brackets" href="#id25" id="id17">7</a></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">+x</span></code>, <code class="docutils literal notranslate"><span class="pre">-x</span></code>, <code class="docutils literal notranslate"><span class="pre">~x</span></code></p></td>
<td><p>Positive, negative, bitwise NOT</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">**</span></code></p></td>
<td><p>Exponentiation <a class="footnote-reference brackets" href="#id26" id="id18">8</a></p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">x[index]</span></code>, <code class="docutils literal notranslate"><span class="pre">x[index:index]</span></code>,
<code class="docutils literal notranslate"><span class="pre">x(arguments...)</span></code>, <code class="docutils literal notranslate"><span class="pre">x.attribute</span></code></p></td>
<td><p>Subscription, slicing,
call, attribute reference</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">(expressions...)</span></code>,
<code class="docutils literal notranslate"><span class="pre">[expressions...]</span></code>,
<code class="docutils literal notranslate"><span class="pre">{key:</span> <span class="pre">value...}</span></code>,
<code class="docutils literal notranslate"><span class="pre">`expressions...`</span></code></p></td>
<td><p>Binding or tuple display,
list display,
dictionary display,
string conversion</p></td>
</tr>
</tbody>
</table>
<p class="rubric">Footnotes</p>
<dl class="footnote brackets">
<dt class="label" id="id19"><span class="brackets"><a class="fn-backref" href="#id3">1</a></span></dt>
<dd><p>In Python 2.3 and later releases, a list comprehension “leaks” the control
variables of each <code class="docutils literal notranslate"><span class="pre">for</span></code> it contains into the containing scope.  However, this
behavior is deprecated, and relying on it will not work in Python 3.</p>
</dd>
<dt class="label" id="id20"><span class="brackets"><a class="fn-backref" href="#id10">2</a></span></dt>
<dd><p>While <code class="docutils literal notranslate"><span class="pre">abs(x%y)</span> <span class="pre">&lt;</span> <span class="pre">abs(y)</span></code> is true mathematically, for floats it may not be
true numerically due to roundoff.  For example, and assuming a platform on which
a Python float is an IEEE 754 double-precision number, in order that <code class="docutils literal notranslate"><span class="pre">-1e-100</span> <span class="pre">%</span>
<span class="pre">1e100</span></code> have the same sign as <code class="docutils literal notranslate"><span class="pre">1e100</span></code>, the computed result is <code class="docutils literal notranslate"><span class="pre">-1e-100</span> <span class="pre">+</span>
<span class="pre">1e100</span></code>, which is numerically exactly equal to <code class="docutils literal notranslate"><span class="pre">1e100</span></code>.  The function
<a class="reference internal" href="../library/math.html#math.fmod" title="math.fmod"><code class="xref py py-func docutils literal notranslate"><span class="pre">math.fmod()</span></code></a> returns a result whose sign matches the sign of the
first argument instead, and so returns <code class="docutils literal notranslate"><span class="pre">-1e-100</span></code> in this case. Which approach
is more appropriate depends on the application.</p>
</dd>
<dt class="label" id="id21"><span class="brackets"><a class="fn-backref" href="#id11">3</a></span></dt>
<dd><p>If x is very close to an exact integer multiple of y, it’s possible for
<code class="docutils literal notranslate"><span class="pre">floor(x/y)</span></code> to be one larger than <code class="docutils literal notranslate"><span class="pre">(x-x%y)/y</span></code> due to rounding.  In such
cases, Python returns the latter result, in order to preserve that
<code class="docutils literal notranslate"><span class="pre">divmod(x,y)[0]</span> <span class="pre">*</span> <span class="pre">y</span> <span class="pre">+</span> <span class="pre">x</span> <span class="pre">%</span> <span class="pre">y</span></code> be very close to <code class="docutils literal notranslate"><span class="pre">x</span></code>.</p>
</dd>
<dt class="label" id="id22"><span class="brackets"><a class="fn-backref" href="#id13">4</a></span></dt>
<dd><p>The Unicode standard distinguishes between <em class="dfn">code points</em>
(e.g. U+0041) and <em class="dfn">abstract characters</em> (e.g. “LATIN CAPITAL LETTER A”).
While most abstract characters in Unicode are only represented using one
code point, there is a number of abstract characters that can in addition be
represented using a sequence of more than one code point.  For example, the
abstract character “LATIN CAPITAL LETTER C WITH CEDILLA” can be represented
as a single <em class="dfn">precomposed character</em> at code position U+00C7, or as a
sequence of a <em class="dfn">base character</em> at code position U+0043 (LATIN CAPITAL
LETTER C), followed by a <em class="dfn">combining character</em> at code position U+0327
(COMBINING CEDILLA).</p>
<p>The comparison operators on unicode strings compare at the level of Unicode code
points. This may be counter-intuitive to humans.  For example,
<code class="docutils literal notranslate"><span class="pre">u&quot;\u00C7&quot;</span> <span class="pre">==</span> <span class="pre">u&quot;\u0043\u0327&quot;</span></code> is <code class="docutils literal notranslate"><span class="pre">False</span></code>, even though both strings
represent the same abstract character “LATIN CAPITAL LETTER C WITH CEDILLA”.</p>
<p>To compare strings at the level of abstract characters (that is, in a way
intuitive to humans), use <a class="reference internal" href="../library/unicodedata.html#unicodedata.normalize" title="unicodedata.normalize"><code class="xref py py-func docutils literal notranslate"><span class="pre">unicodedata.normalize()</span></code></a>.</p>
</dd>
<dt class="label" id="id23"><span class="brackets"><a class="fn-backref" href="#id14">5</a></span></dt>
<dd><p>Earlier versions of Python used lexicographic comparison of the sorted (key,
value) lists, but this was very expensive for the common case of comparing for
equality.  An even earlier version of Python compared dictionaries by identity
only, but this caused surprises because people expected to be able to test a
dictionary for emptiness by comparing it to <code class="docutils literal notranslate"><span class="pre">{}</span></code>.</p>
</dd>
<dt class="label" id="id24"><span class="brackets"><a class="fn-backref" href="#id15">6</a></span></dt>
<dd><p>Due to automatic garbage-collection, free lists, and the dynamic nature of
descriptors, you may notice seemingly unusual behaviour in certain uses of
the <a class="reference internal" href="#is"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">is</span></code></a> operator, like those involving comparisons between instance
methods, or constants.  Check their documentation for more info.</p>
</dd>
<dt class="label" id="id25"><span class="brackets"><a class="fn-backref" href="#id17">7</a></span></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">%</span></code> operator is also used for string formatting; the same
precedence applies.</p>
</dd>
<dt class="label" id="id26"><span class="brackets"><a class="fn-backref" href="#id18">8</a></span></dt>
<dd><p>The power operator <code class="docutils literal notranslate"><span class="pre">**</span></code> binds less tightly than an arithmetic or
bitwise unary operator on its right, that is, <code class="docutils literal notranslate"><span class="pre">2**-1</span></code> is <code class="docutils literal notranslate"><span class="pre">0.5</span></code>.</p>
</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">5. Expressions</a><ul>
<li><a class="reference internal" href="#arithmetic-conversions">5.1. Arithmetic conversions</a></li>
<li><a class="reference internal" href="#atoms">5.2. Atoms</a><ul>
<li><a class="reference internal" href="#atom-identifiers">5.2.1. Identifiers (Names)</a></li>
<li><a class="reference internal" href="#literals">5.2.2. Literals</a></li>
<li><a class="reference internal" href="#parenthesized-forms">5.2.3. Parenthesized forms</a></li>
<li><a class="reference internal" href="#list-displays">5.2.4. List displays</a></li>
<li><a class="reference internal" href="#displays-for-sets-and-dictionaries">5.2.5. Displays for sets and dictionaries</a></li>
<li><a class="reference internal" href="#generator-expressions">5.2.6. Generator expressions</a></li>
<li><a class="reference internal" href="#dictionary-displays">5.2.7. Dictionary displays</a></li>
<li><a class="reference internal" href="#set-displays">5.2.8. Set displays</a></li>
<li><a class="reference internal" href="#string-conversions">5.2.9. String conversions</a></li>
<li><a class="reference internal" href="#yield-expressions">5.2.10. Yield expressions</a><ul>
<li><a class="reference internal" href="#generator-iterator-methods">5.2.10.1. Generator-iterator methods</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#primaries">5.3. Primaries</a><ul>
<li><a class="reference internal" href="#attribute-references">5.3.1. Attribute references</a></li>
<li><a class="reference internal" href="#subscriptions">5.3.2. Subscriptions</a></li>
<li><a class="reference internal" href="#slicings">5.3.3. Slicings</a></li>
<li><a class="reference internal" href="#calls">5.3.4. Calls</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-power-operator">5.4. The power operator</a></li>
<li><a class="reference internal" href="#unary-arithmetic-and-bitwise-operations">5.5. Unary arithmetic and bitwise operations</a></li>
<li><a class="reference internal" href="#binary-arithmetic-operations">5.6. Binary arithmetic operations</a></li>
<li><a class="reference internal" href="#shifting-operations">5.7. Shifting operations</a></li>
<li><a class="reference internal" href="#binary-bitwise-operations">5.8. Binary bitwise operations</a></li>
<li><a class="reference internal" href="#comparisons">5.9. Comparisons</a><ul>
<li><a class="reference internal" href="#value-comparisons">5.9.1. Value comparisons</a></li>
<li><a class="reference internal" href="#membership-test-operations">5.9.2. Membership test operations</a></li>
<li><a class="reference internal" href="#is-not">5.9.3. Identity comparisons</a></li>
</ul>
</li>
<li><a class="reference internal" href="#boolean-operations">5.10. Boolean operations</a></li>
<li><a class="reference internal" href="#conditional-expressions">5.11. Conditional Expressions</a></li>
<li><a class="reference internal" href="#lambda">5.12. Lambdas</a></li>
<li><a class="reference internal" href="#expression-lists">5.13. Expression lists</a></li>
<li><a class="reference internal" href="#evaluation-order">5.14. Evaluation order</a></li>
<li><a class="reference internal" href="#operator-precedence">5.15. Operator precedence</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="executionmodel.html"
                        title="previous chapter">4. Execution model</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="simple_stmts.html"
                        title="next chapter">6. Simple statements</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/reference/expressions.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <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="simple_stmts.html" title="6. Simple statements"
             >next</a> |</li>
        <li class="right" >
          <a href="executionmodel.html" title="4. Execution model"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Language Reference</a> &#187;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Oct 19, 2019.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
    </div>

  </body>
</html>