<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>5.3.3 Slicings</TITLE>
<META NAME="description" CONTENT="5.3.3 Slicings">
<META NAME="keywords" CONTENT="ref">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="ref.css">
<LINK REL="next" HREF="calls.html">
<LINK REL="previous" HREF="subscriptions.html">
<LINK REL="up" HREF="primaries.html">
<LINK REL="next" HREF="calls.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="calls.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="primaries.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="subscriptions.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="calls.html">5.3.4 Calls</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="primaries.html">5.3 Primaries</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="subscriptions.html">5.3.2 Subscriptions</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION007330000000000000000"></A>
<a name="l2h-484"></a>
<a name="l2h-485"></a>
<BR>
5.3.3 Slicings
</H2>

<P>
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 del statements.  The syntax for a slicing:
<a name="l2h-490"></a>
<a name="l2h-491"></a>
<a name="l2h-492"></a>
<a name="l2h-493"></a>

<P>
<dl><dd><pre class=verbatim>
slicing:          simple_slicing | extended_slicing
simple_slicing:   primary "[" short_slice "]"
extended_slicing: primary "[" slice_list "]" 
slice_list:       slice_item ("," slice_item)* [","]
slice_item:       expression | proper_slice | ellipsis
proper_slice:     short_slice | long_slice
short_slice:      [lower_bound] ":" [upper_bound]
long_slice:       short_slice ":" [stride]
lower_bound:      expression
upper_bound:      expression
stride:           expression
ellipsis:         "..."
</pre></dl>

<P>
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.<a name="l2h-494"></a>

<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
sequence's length, respectively.  If either bound is negative, the
sequence's length is added to it.  The slicing now selects all items
with index <var>k</var> such that
<tt><var>i</var> &lt;= <var>k</var> &lt; <var>j</var></tt> where <var>i</var>
and <var>j</var> are the specified lower and upper bounds.  This may be an
empty sequence.  It is not an error if <var>i</var> or <var>j</var> lie outside the
range of valid indexes (such items don't exist so they aren't
selected).

<P>
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 <tt>Ellipsis</tt> object.  The conversion of a
proper slice is a slice object (see section <A HREF="types.html#types">3.2</A>) whose
<tt class=member>start</tt>, <tt class=member>stop</tt> and <tt class=member>step</tt> attributes are the
values of the expressions given as lower bound, upper bound and
stride, respectively, substituting <tt>None</tt> for missing
expressions.
<a name="l2h-487"></a>
  <a name="l2h-488"></a><a name="l2h-489"></a>
<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="calls.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="primaries.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="subscriptions.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Reference Manual</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><img width=32 height=32 align=bottom border=0 alt=""
 src="../icons/blank.gif"></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="calls.html">5.3.4 Calls</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="primaries.html">5.3 Primaries</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="subscriptions.html">5.3.2 Subscriptions</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
