# [Python-checkins] CVS: python/dist/src/Doc/tut tut.tex,1.100.2.6,1.100.2.7

Fred L. Drake python-dev@python.org
Mon, 20 Mar 2000 09:49:27 -0500 (EST)

Update of /projects/cvsroot/python/dist/src/Doc/tut
In directory weyr:/home/fdrake/projects/python/Doc-152p2/tut

Modified Files:
Tag: release152p1-patches
tut.tex
Log Message:

Made changes based on Ka-Ping Yee's <ping@lfw.org> suggested changes
from his message to the doc-sig regarding list methods.  Mostly
accepted verbatim, but point out that pop() actually removes the
returned item from the list!  ;)

Index: tut.tex
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Doc/tut/tut.tex,v
retrieving revision 1.100.2.6
retrieving revision 1.100.2.7
diff -C2 -r1.100.2.6 -r1.100.2.7
*** tut.tex	2000/02/22 17:26:02	1.100.2.6
--- tut.tex	2000/03/20 14:49:23	1.100.2.7
***************
*** 4,8 ****
% Add a section on file I/O
% Write a chapter entitled Some Useful Modules''
! %  --regex, math+cmath
% Should really move the Python startup file info to an appendix

--- 4,8 ----
% Add a section on file I/O
% Write a chapter entitled Some Useful Modules''
! %  --re, math+cmath
% Should really move the Python startup file info to an appendix

***************
*** 1441,1444 ****
--- 1441,1445 ----
more detail, and adds some new things as well.

+
\section{More on Lists \label{moreLists}}

***************
*** 1448,1451 ****
--- 1449,1460 ----
\begin{description}

+ \item[\code{append(x)}]
+ Add an item to the end of the list;
+ equivalent to \code{a[len(a):] = [x]}.
+
+ \item[\code{extend(L)}]
+ Extend the list by appending all the items in the given list;
+ equivalent to \code{a[len(a):] = L}.
+
\item[\code{insert(i, x)}]
Insert an item at a given position.  The first argument is the index of
***************
*** 1454,1460 ****
\code{a.append(x)}.

! \item[\code{append(x)}]
! Append an item to the list;
! equivalent to \code{a.insert(len(a), x)}.

\item[\code{index(x)}]
--- 1463,1474 ----
\code{a.append(x)}.

! \item[\code{remove(x)}]
! Remove the first item from the list whose value is \code{x}.
! It is an error if there is no such item.
!
! \item[\code{pop(\optional{i})}]
! Remove the item at the given position in the list, and return it.  If
! no index is specified, \code{a.pop()} returns the last item in the
! list.  The item is also removed from the list.

\item[\code{index(x)}]
***************
*** 1462,1468 ****
It is an error if there is no such item.

! \item[\code{remove(x)}]
! Remove the first item from the list whose value is \code{x}.
! It is an error if there is no such item.

\item[\code{sort()}]
--- 1476,1481 ----
It is an error if there is no such item.

! \item[\code{count(x)}]
! Return the number of times \code{x} appears in the list.

\item[\code{sort()}]
***************
*** 1472,1481 ****
Reverse the elements of the list, in place.

- \item[\code{count(x)}]
- Return the number of times \code{x} appears in the list.
-
\end{description}

! An example that uses all list methods:

\begin{verbatim}
--- 1485,1491 ----
Reverse the elements of the list, in place.

\end{description}

! An example that uses most of the list methods:

\begin{verbatim}
***************
*** 1499,1502 ****
--- 1509,1563 ----
[-1, 1, 66.6, 333, 333, 1234.5]
\end{verbatim}
+
+
+ \subsection{Using Lists as Stacks \label{lists-as-stacks}}
+ \sectionauthor{Ka-Ping Yee}{ping@lfs.org}
+
+ The list methods make it very easy to use a list as a stack, where the
+ last element added is the first element retrieved (last-in,
+ first-out'').  To add an item to the top of the stack, use
+ \method{append()}.  To retrieve an item from the top of the stack, use
+ \method{pop()} without an explicit index.  For example:
+
+ \begin{verbatim}
+ >>> stack = [3, 4, 5]
+ >>> stack.append(6)
+ >>> stack.append(7)
+ >>> stack
+ [3, 4, 5, 6, 7]
+ >>> stack.pop()
+ 7
+ >>> stack
+ [3, 4, 5, 6]
+ >>> stack.pop()
+ 6
+ >>> stack.pop()
+ 5
+ >>> stack
+ [3, 4]
+ \end{verbatim}
+
+
+ \subsection{Using Lists as Queues \label{lists-as-queues}}
+ \sectionauthor{Ka-Ping Yee}{ping@lfs.org}
+
+ You can also use a list conveniently as a queue, where the first
+ element added is the first element retrieved (first-in,
+ first-out'').  To add an item to the back of the queue, use
+ \method{append()}.  To retrieve an item from the front of the queue,
+ use \method{pop()} with \code{0} as the index.  For example:
+
+ \begin{verbatim}
+ >>> queue = ["Eric", "John", "Michael"]
+ >>> queue.append("Terry")           # Terry arrives
+ >>> queue.append("Graham")          # Graham arrives
+ >>> queue.pop(0)
+ 'Eric'
+ >>> queue.pop(0)
+ 'John'
+ >>> queue
+ ['Michael', 'Terry', 'Graham']
+ \end{verbatim}
+

\subsection{Functional Programming Tools \label{functional}}