home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / python / pyhtmldoc / s / sequence_t next >
Encoding:
Text File  |  1996-11-14  |  3.7 KB  |  66 lines

  1. <TITLE>Sequence Types -- Python library reference</TITLE>
  2. Next: <A HREF="../m/mapping_types" TYPE="Next">Mapping Types</A>  
  3. Prev: <A HREF="../n/numeric_types" TYPE="Prev">Numeric Types</A>  
  4. Up: <A HREF="../t/types" TYPE="Up">Types</A>  
  5. Top: <A HREF="../t/top" TYPE="Top">Top</A>  
  6. <H2>2.1.5. Sequence Types</H2>
  7. There are three sequence types: strings, lists and tuples.
  8. <P>
  9. Strings literals are written in single or double quotes:
  10. <CODE>'xyzzy'</CODE>, <CODE>"frobozz"</CODE>.  See Chapter 2 of the Python
  11. Reference Manual for more about string literals.  Lists are
  12. constructed with square brackets, separating items with commas:
  13. <CODE>[a, b, c]</CODE>.  Tuples are constructed by the comma operator (not
  14. within square brackets), with or without enclosing parentheses, but an
  15. empty tuple must have the enclosing parentheses, e.g.,
  16. <CODE>a, b, c</CODE> or <CODE>()</CODE>.  A single item tuple must have a trailing
  17. comma, e.g., <CODE>(d,)</CODE>.
  18. Sequence types support the following operations.  The `<SAMP>in</SAMP>' and
  19. `<SAMP>not,in</SAMP>' operations have the same priorities as the comparison
  20. operations.  The `<SAMP>+</SAMP>' and `<SAMP>*</SAMP>' operations have the same
  21. priority as the corresponding numeric operations.<A NAME="footnoteref1" HREF="#footnotetext1">(1)</A>
  22. <P>
  23. This table lists the sequence operations sorted in ascending priority
  24. (operations in the same box have the same priority).  In the table,
  25. <VAR>s</VAR> and <VAR>t</VAR> are sequences of the same type; <VAR>n</VAR>, <VAR>i</VAR>
  26. and <VAR>j</VAR> are integers:
  27. <P>
  28. <DL>
  29. <DT><I>Operation</I><DD><I>Result</I>  ---  <I>Notes</I>
  30. <P>
  31. <DT><CODE><VAR>x</VAR> in <VAR>s</VAR></CODE><DD><CODE>1</CODE> if an item of <VAR>s</VAR> is equal to <VAR>x</VAR>, else <CODE>0</CODE>
  32. <DT><CODE><VAR>x</VAR> not in <VAR>s</VAR></CODE><DD><CODE>0</CODE> if an item of <VAR>s</VAR> is
  33. equal to <VAR>x</VAR>, else <CODE>1</CODE>
  34. <DT><CODE><VAR>s</VAR> + <VAR>t</VAR></CODE><DD>the concatenation of <VAR>s</VAR> and <VAR>t</VAR>
  35. <DT><CODE><VAR>s</VAR> * <VAR>n</VAR><R>,</R> <VAR>n</VAR> * <VAR>s</VAR></CODE><DD><VAR>n</VAR> copies of <VAR>s</VAR> concatenated
  36. <DT><CODE><VAR>s</VAR>[<VAR>i</VAR>]</CODE><DD><VAR>i</VAR>'th item of <VAR>s</VAR>, origin 0  ---  (1)
  37. <DT><CODE><VAR>s</VAR>[<VAR>i</VAR>:<VAR>j</VAR>]</CODE><DD>slice of <VAR>s</VAR> from <VAR>i</VAR> to <VAR>j</VAR>  ---  (1), (2)
  38. <DT><CODE>len(<VAR>s</VAR>)</CODE><DD>length of <VAR>s</VAR>
  39. <DT><CODE>min(<VAR>s</VAR>)</CODE><DD>smallest item of <VAR>s</VAR>
  40. <DT><CODE>max(<VAR>s</VAR>)</CODE><DD>largest item of <VAR>s</VAR>
  41. </DL>
  42.  Notes:
  43. <P>
  44. <DL>
  45. <DT><B>(1)</B><DD>If <VAR>i</VAR> or <VAR>j</VAR> is negative, the index is relative to
  46. the end of the string, i.e., <CODE>len(<VAR>s</VAR>) + <VAR>i</VAR></CODE> or
  47. <CODE>len(<VAR>s</VAR>) + <VAR>j</VAR></CODE> is substituted.  But note that <CODE>-0</CODE> is
  48. still <CODE>0</CODE>.
  49. <P>
  50. <DT><B>(2)</B><DD>The slice of <VAR>s</VAR> from <VAR>i</VAR> to <VAR>j</VAR> is defined as
  51. the sequence of items with index <VAR>k</VAR> such that <CODE><VAR>i</VAR> <=
  52. <VAR>k</VAR> < <VAR>j</VAR></CODE>.  If <VAR>i</VAR> or <VAR>j</VAR> is greater than
  53. <CODE>len(<VAR>s</VAR>)</CODE>, use <CODE>len(<VAR>s</VAR>)</CODE>.  If <VAR>i</VAR> is omitted,
  54. use <CODE>0</CODE>.  If <VAR>j</VAR> is omitted, use <CODE>len(<VAR>s</VAR>)</CODE>.  If
  55. <VAR>i</VAR> is greater than or equal to <VAR>j</VAR>, the slice is empty.
  56. <P>
  57. </DL>
  58. <H2>Menu</H2><DL COMPACT>
  59. <DT><A HREF="../m/more_string_operations" TYPE=Menu>More String Operations</A>
  60. <DD><DT><A HREF="../m/mutable_sequence_types" TYPE=Menu>Mutable Sequence Types</A>
  61. <DD></DL>
  62. <H2>---------- Footnotes ----------</H2>
  63. <A NAME="footnotetext1" HREF="#footnoteref1">(1)</A>
  64. They must
  65. have since the parser can't tell the type of the operands.<P>
  66.