| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="ref.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="ref.html" title='Python Reference Manual' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="calls.html" /> |
| 13 | <link rel="prev" href="subscriptions.html" /> |
| 14 | <link rel="parent" href="primaries.html" /> |
| 15 | <link rel="next" href="calls.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.3.3 Slicings</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="5.3.2 Subscriptions" |
| 25 | href="subscriptions.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="5.3 Primaries" |
| 28 | href="primaries.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="5.3.4 Calls" |
| 31 | href="calls.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Reference Manual</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><img src='../icons/blank.png' |
| 38 | border='0' height='32' alt='' width='32' /></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="subscriptions.html">5.3.2 Subscriptions</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="primaries.html">5.3 Primaries</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="calls.html">5.3.4 Calls</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION007330000000000000000"></A><A NAME="slicings"></A> |
| 56 | <a id='l2h-378' xml:id='l2h-378'></a> |
| 57 | <BR> |
| 58 | 5.3.3 Slicings |
| 59 | </H2> |
| 60 | |
| 61 | <P> |
| 62 | A slicing selects a range of items in a sequence object (e.g., a |
| 63 | string, tuple or list). Slicings may be used as expressions or as |
| 64 | targets in assignment or del statements. The syntax for a slicing: |
| 65 | <a id='l2h-379' xml:id='l2h-379'></a><a id='l2h-380' xml:id='l2h-380'></a><a id='l2h-381' xml:id='l2h-381'></a><a id='l2h-382' xml:id='l2h-382'></a> |
| 66 | <P> |
| 67 | <dl><dd class="grammar"> |
| 68 | <div class="productions"> |
| 69 | <table> |
| 70 | <tr> |
| 71 | <td><a id='tok-slicing' xml:id='tok-slicing'>slicing</a></td> |
| 72 | <td>::=</td> |
| 73 | <td><a class='grammartoken' href="slicings.html#tok-simple_slicing">simple_slicing</a> | <a class='grammartoken' href="slicings.html#tok-extended_slicing">extended_slicing</a></td></tr> |
| 74 | <tr> |
| 75 | <td><a id='tok-simple_slicing' xml:id='tok-simple_slicing'>simple_slicing</a></td> |
| 76 | <td>::=</td> |
| 77 | <td><a class='grammartoken' href="primaries.html#tok-primary">primary</a> "[" <a class='grammartoken' href="slicings.html#tok-short_slice">short_slice</a> "]"</td></tr> |
| 78 | <tr> |
| 79 | <td><a id='tok-extended_slicing' xml:id='tok-extended_slicing'>extended_slicing</a></td> |
| 80 | <td>::=</td> |
| 81 | <td><a class='grammartoken' href="primaries.html#tok-primary">primary</a> "[" <a class='grammartoken' href="slicings.html#tok-slice_list">slice_list</a> "]" </td></tr> |
| 82 | <tr> |
| 83 | <td><a id='tok-slice_list' xml:id='tok-slice_list'>slice_list</a></td> |
| 84 | <td>::=</td> |
| 85 | <td><a class='grammartoken' href="slicings.html#tok-slice_item">slice_item</a> ("," <a class='grammartoken' href="slicings.html#tok-slice_item">slice_item</a>)* [","]</td></tr> |
| 86 | <tr> |
| 87 | <td><a id='tok-slice_item' xml:id='tok-slice_item'>slice_item</a></td> |
| 88 | <td>::=</td> |
| 89 | <td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a> | <a class='grammartoken' href="slicings.html#tok-proper_slice">proper_slice</a> | <a class='grammartoken' href="slicings.html#tok-ellipsis">ellipsis</a></td></tr> |
| 90 | <tr> |
| 91 | <td><a id='tok-proper_slice' xml:id='tok-proper_slice'>proper_slice</a></td> |
| 92 | <td>::=</td> |
| 93 | <td><a class='grammartoken' href="slicings.html#tok-short_slice">short_slice</a> | <a class='grammartoken' href="slicings.html#tok-long_slice">long_slice</a></td></tr> |
| 94 | <tr> |
| 95 | <td><a id='tok-short_slice' xml:id='tok-short_slice'>short_slice</a></td> |
| 96 | <td>::=</td> |
| 97 | <td>[<a class='grammartoken' href="slicings.html#tok-lower_bound">lower_bound</a>] ":" [<a class='grammartoken' href="slicings.html#tok-upper_bound">upper_bound</a>]</td></tr> |
| 98 | <tr> |
| 99 | <td><a id='tok-long_slice' xml:id='tok-long_slice'>long_slice</a></td> |
| 100 | <td>::=</td> |
| 101 | <td><a class='grammartoken' href="slicings.html#tok-short_slice">short_slice</a> ":" [<a class='grammartoken' href="slicings.html#tok-stride">stride</a>]</td></tr> |
| 102 | <tr> |
| 103 | <td><a id='tok-lower_bound' xml:id='tok-lower_bound'>lower_bound</a></td> |
| 104 | <td>::=</td> |
| 105 | <td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a></td></tr> |
| 106 | <tr> |
| 107 | <td><a id='tok-upper_bound' xml:id='tok-upper_bound'>upper_bound</a></td> |
| 108 | <td>::=</td> |
| 109 | <td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a></td></tr> |
| 110 | <tr> |
| 111 | <td><a id='tok-stride' xml:id='tok-stride'>stride</a></td> |
| 112 | <td>::=</td> |
| 113 | <td><a class='grammartoken' href="Booleans.html#tok-expression">expression</a></td></tr> |
| 114 | <tr> |
| 115 | <td><a id='tok-ellipsis' xml:id='tok-ellipsis'>ellipsis</a></td> |
| 116 | <td>::=</td> |
| 117 | <td>"..."</td></tr> |
| 118 | </table> |
| 119 | </div> |
| 120 | <a class="grammar-footer" |
| 121 | href="grammar.txt" type="text/plain" |
| 122 | >Download entire grammar as text.</a> |
| 123 | </dd></dl> |
| 124 | |
| 125 | <P> |
| 126 | There is ambiguity in the formal syntax here: anything that looks like |
| 127 | an expression list also looks like a slice list, so any subscription |
| 128 | can be interpreted as a slicing. Rather than further complicating the |
| 129 | syntax, this is disambiguated by defining that in this case the |
| 130 | interpretation as a subscription takes priority over the |
| 131 | interpretation as a slicing (this is the case if the slice list |
| 132 | contains no proper slice nor ellipses). Similarly, when the slice |
| 133 | list has exactly one short slice and no trailing comma, the |
| 134 | interpretation as a simple slicing takes priority over that as an |
| 135 | extended slicing.<a id='l2h-383' xml:id='l2h-383'></a> |
| 136 | <P> |
| 137 | The semantics for a simple slicing are as follows. The primary must |
| 138 | evaluate to a sequence object. The lower and upper bound expressions, |
| 139 | if present, must evaluate to plain integers; defaults are zero and the |
| 140 | <code>sys.maxint</code>, respectively. If either bound is negative, the |
| 141 | sequence's length is added to it. The slicing now selects all items |
| 142 | with index <var>k</var> such that |
| 143 | <code><var>i</var> <= <var>k</var> < <var>j</var></code> where <var>i</var> |
| 144 | and <var>j</var> are the specified lower and upper bounds. This may be an |
| 145 | empty sequence. It is not an error if <var>i</var> or <var>j</var> lie outside the |
| 146 | range of valid indexes (such items don't exist so they aren't |
| 147 | selected). |
| 148 | |
| 149 | <P> |
| 150 | The semantics for an extended slicing are as follows. The primary |
| 151 | must evaluate to a mapping object, and it is indexed with a key that |
| 152 | is constructed from the slice list, as follows. If the slice list |
| 153 | contains at least one comma, the key is a tuple containing the |
| 154 | conversion of the slice items; otherwise, the conversion of the lone |
| 155 | slice item is the key. The conversion of a slice item that is an |
| 156 | expression is that expression. The conversion of an ellipsis slice |
| 157 | item is the built-in <code>Ellipsis</code> object. The conversion of a |
| 158 | proper slice is a slice object (see section <A href="types.html#types">3.2</A>) whose |
| 159 | <tt class="member">start</tt>, <tt class="member">stop</tt> and <tt class="member">step</tt> attributes are the |
| 160 | values of the expressions given as lower bound, upper bound and |
| 161 | stride, respectively, substituting <code>None</code> for missing |
| 162 | expressions. |
| 163 | <a id='l2h-385' xml:id='l2h-385'></a> |
| 164 | <P> |
| 165 | |
| 166 | <DIV CLASS="navigation"> |
| 167 | <div class='online-navigation'> |
| 168 | <p></p><hr /> |
| 169 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 170 | <tr> |
| 171 | <td class='online-navigation'><a rel="prev" title="5.3.2 Subscriptions" |
| 172 | href="subscriptions.html"><img src='../icons/previous.png' |
| 173 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 174 | <td class='online-navigation'><a rel="parent" title="5.3 Primaries" |
| 175 | href="primaries.html"><img src='../icons/up.png' |
| 176 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 177 | <td class='online-navigation'><a rel="next" title="5.3.4 Calls" |
| 178 | href="calls.html"><img src='../icons/next.png' |
| 179 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 180 | <td align="center" width="100%">Python Reference Manual</td> |
| 181 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 182 | href="contents.html"><img src='../icons/contents.png' |
| 183 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 184 | <td class='online-navigation'><img src='../icons/blank.png' |
| 185 | border='0' height='32' alt='' width='32' /></td> |
| 186 | <td class='online-navigation'><a rel="index" title="Index" |
| 187 | href="genindex.html"><img src='../icons/index.png' |
| 188 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 189 | </tr></table> |
| 190 | <div class='online-navigation'> |
| 191 | <b class="navlabel">Previous:</b> |
| 192 | <a class="sectref" rel="prev" href="subscriptions.html">5.3.2 Subscriptions</A> |
| 193 | <b class="navlabel">Up:</b> |
| 194 | <a class="sectref" rel="parent" href="primaries.html">5.3 Primaries</A> |
| 195 | <b class="navlabel">Next:</b> |
| 196 | <a class="sectref" rel="next" href="calls.html">5.3.4 Calls</A> |
| 197 | </div> |
| 198 | </div> |
| 199 | <hr /> |
| 200 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 201 | </DIV> |
| 202 | <!--End of Navigation Panel--> |
| 203 | <ADDRESS> |
| 204 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 205 | </ADDRESS> |
| 206 | </BODY> |
| 207 | </HTML> |