\documentclass[12pt,letterpaper]{article}

\bibliographystyle{plain}
\usepackage{graphicx}

\setlength\textwidth{6in}
\setlength\textheight{8in}                                                  
\setlength\oddsidemargin{0.25in} % LaTeX adds a default 1in to this!        
\setlength\evensidemargin{0.25in}                                            
\setlength\topmargin{-0.0in}     % LaTeX adds a default 1in to this!        
\setlength\headsep{0in}                                                     
\setlength\headheight{0in}                                                  
\setlength\footskip{1in}   

\begin{document}

  \title{Shell, Emacs, \& Python (oh my!) Cheat Sheet}
  \author{Lawrence David \\ 
  }
  \date{09.07.06}
  \maketitle
  
  % The Shell
  \section{Shell}

  To run these commands, only type in the part after the dollar sign.
  Replace both arguments \emph{and} square brackets (don't leave
  square brackets in).

  \begin{center}
    $\begin{array}{l|l}
      \hline
      \mbox{\$ cp [oldfile] [newfile]} & \mbox{copies} \\
      \mbox{\$ mv [oldfile] [newfile]} & \mbox{moves} \\
      \mbox{\$ rm [oldfile]} & \mbox{deletes (no undo!)} \\
      \mbox{\$ mkdir [newdir]} & \mbox{new directory} \\
      \mbox{\$ rm -r [olddir]} & \mbox{remove directory} \\
      \mbox{\$ cd [destination]} & \mbox{change directory} \\
      \mbox{\$ cd .. } & \mbox{up one directory} \\
      \mbox{\$ ls } & \mbox{list contents of directory} \\
      \mbox{\$ cat [file] } & \mbox{print every line of file} \\
      \mbox{\$ more [file]} & \mbox{print one screen of file} \\
      \mbox{\$ emacs [file]} & \mbox{edit file} \\
      \mbox{\$ python [file]} & \mbox{execute python file} \\
      \hline
    \end{array}$
    \end{center}

    \clearpage

    \section{Emacs}
    
    Below, 'C' is short for the \texttt{control} key and 'M' denotes
    the \texttt{meta} key.  On most operating systems, the meta key
    defaults to the \texttt{Esc} button; however, you can usually futz
    with your terminal application to map \texttt{meta} to something
    more ergonomically reasonable, like the \texttt{alt} button.

    To execute one of these commands (e.g. C-d), hold down
    \texttt{control}, press d with a free finger, and then release
    \texttt{control}.  To execute something like C-x,C-s, hold down
    \texttt{control}, press x, then press s, and finally release
    \texttt{control}.  

  \begin{center}
    $\begin{array}{l|l}
      \hline
      \mbox{C-d} & \mbox{delete one character} \\
      \mbox{M-d} & \mbox{delete one word} \\
      \mbox{C-k} & \mbox{delete one line} \\
      \mbox{M-k} & \mbox{delete one paragraph} \\
      \mbox{C-\_} & \mbox{undo} \\
      \mbox{C-x,C-c} & \mbox{save and quit} \\
      \mbox{C-x,C-w} & \mbox{save as} \\
      \mbox{C-x,C-s} & \mbox{save} \\
      \mbox{C-s} & \mbox{find as you type} \\
      \mbox{M-\%} & \mbox{find and replace} \\
      \mbox{C-space} & \mbox{set mark} \\
      \mbox{M-w} & \mbox{copy from mark to current position} \\
      \mbox{C-w} & \mbox{cut from mark to current position} \\
      \mbox{C-y} & \mbox{paste} \\
      \mbox{C-a} & \mbox{jump to beginning of line} \\
      \mbox{C-e} & \mbox{jump to end of line} \\
      \mbox{M-<} & \mbox{jump to beginning of document} \\
      \mbox{M->} & \mbox{jump to end of document} \\
      \mbox{C-v} & \mbox{page down} \\
      \mbox{M-v} & \mbox{page up} \\
      \hline
    \end{array}$
    \end{center}

    \clearpage
    
    \section{Python}

    \subsection{Comments}
    Comment out lines with the pound-sign: \#.

    \subsection{Lists}
    List is python-speak for array or ordered collection of items.
    \begin{center}
      $\begin{array}{l|l}
        \hline
        \mbox{L = []} & \mbox{initiate a list} \\
        \mbox{L = [expr1,expr2,...]} & \mbox{list creation} \\
        \mbox{L.append(x)} & \mbox{add an item x} \\
        \mbox{L.remove(x)} & \mbox{remove first occurence of item x} \\
        \mbox{L[i]} & \mbox{return i'th item of list} \\
        \mbox{L[0]} & \mbox{return first item of list} \\
        \mbox{L[-1]} & \mbox{return last item of list} \\
        \mbox{L[0:2]} & \mbox{return first 3 items of list} \\
        \mbox{L.extend(M)} & \mbox{merge list M into L} \\
        \mbox{L.reverse()} & \mbox{flip ordering of list} \\
        \mbox{L.sort()} & \mbox{sort items of L} \\
        \mbox{len(L)} & \mbox{return length of list} \\
        \hline
      \end{array}$
    \end{center}

    \subsection{Dictionaries}
    Dictionary is python-speak for hash table or data structure where
    \emph{values} are stored according to \emph{keys}

    \begin{center}
      $\begin{array}{l|l}
        \hline
        \mbox{D = {}} & \mbox{initiate a dict} \\
        \mbox{D[k] = i} & \mbox{store item i in dict with key k} \\
        \mbox{D = {k1:v1, k2:v2, ...}} & \mbox{another way to populate
            dict} \\
        \mbox{D.has\_key(k)} & \mbox{return true if k is a key in D} \\
        \mbox{D.keys()} & \mbox{return a list of keys} \\
        \mbox{D.values()} & \mbox{return a list of values} \\
        \hline
      \end{array}$
    \end{center}

    \subsection{Logic}
    \begin{center}
      $\begin{array}{l|l}
        \hline
        \mbox{a == b} & \mbox{return true if a equals b} \\
        \mbox{a } > \mbox{ b} & \mbox{return true if a greater than b} \\
        \mbox{a } < \mbox{ b} & \mbox{return true if a greater than b} \\
        \mbox{a and b} & \mbox{return true if both a and b are true} \\
        \mbox{a or b} & \mbox{return true if either a or b are true} \\
        \mbox{not a} & \mbox{return true if a is false} \\
        \hline
      \end{array}$
    \end{center}

    \subsection{Loops}
    Be sure to indent all lines of code to be executed in a loop!
    Note that you don't need any brackets or end statements.
    \begin{center}
      $\begin{array}{l}
        \hline
        \mbox{for i in L:} \\
        \mbox{  print i} \\
        \mbox{for i in range(0,len(L)):} \\
        \mbox{  print L[i]} \\
        \hline
        \mbox{while i == True:} \\
        \mbox{  print i} \\
        \hline
      \end{array}$
    \end{center}

    \subsection{Conditionals}
    Like loops, code executed within conditions must be indented!
    \begin{center}
      $\begin{array}{l}
      \hline
        \mbox{if i == 0:} \\
        \mbox{  print "i equals 0"} \\
        \mbox{elif i == 1:}\\
        \mbox{  print "i equals 1"} \\
        \mbox{else:} \\
        \mbox{  print "who knows what i equals"} \\
        \hline
      \end{array}$
    \end{center}

    \begin{center}
      $\begin{array}{l|l}
        \hline
        \mbox{break} & \mbox{will terminate the loop
          encompassing the conditional} \\
        \mbox{continue} & \mbox{will skip encompassing loop to next
          iteration} \\
        \hline
      \end{array}$
    \end{center}

    \subsection{Reading and Writing to Files}
    \begin{center}
      $\begin{array}{l|l}
        \hline
        \mbox{f = open('filename','r')} & \mbox{open file for reading}
        \\
        \mbox{f = open('filename','w')} & \mbox{open file for writing}
        \\
        \mbox{f.close()} & \mbox{close a file}
        \\
        \mbox{f.read()} & \mbox{return the file's entire contents in a
          string} \\
        \mbox{f.readlines()} & \mbox{return a list containing each
          line of the file} \\
        \mbox{f.write(s)} & \mbox{write string s to file f} \\
        \hline
      \end{array}$
    \end{center}    

    \clearpage

    \subsection{Math}
      For commands that are preceded by 'math', you will need to
      import the math module.
    \begin{center}
      $\begin{array}{l|l}
        \hline
        \mbox{import math} & \mbox{imports math module} \\
        \mbox{a*b} & \mbox{get product of a and b} \\
        \mbox{a/b} & \mbox{divide a by b} \\
        \mbox{a+b} & \mbox{sum a and b} \\
        \mbox{a-b} & \mbox{difference of a and b} \\
        \mbox{a += b} & \mbox{same as a = a + b} \\
        \mbox{math.log(n)} & \mbox{take log of n} \\
        \mbox{math.exp(n)} & \mbox{e raised to n} \\
        \hline
      \end{array}$
    \end{center}    
     
    \subsection{Defining Functions}
    Again, you'll need to indent all the function's code.  
    \begin{center}
      $\begin{array}{l}
      \hline
        \mbox{def myfunction(arg1,arg2):}\\
        \mbox{  myprod = arg1*arg2} \\
        \mbox{  return myprod} \\
        \hline
      \end{array}$
    \end{center}

    \subsection{Modules}
    Note that imported modules need to live in Python's path.  If that
    doesn't make any sense to you, it means that modules you write or
    download from online should be placed in the same directory as
    your code, to prevent import errors.

    \begin{center}
      $\begin{array}{l|l}
      \hline
        \mbox{import m} & \mbox{import module m} \\
        \mbox{m.myfun()} & \mbox{call function 'my fun' in m} \\
        \hline
      \end{array}$
    \end{center}
    
    
    \subsection{Miscellany}
    \begin{center}
      $\begin{array}{l|l}
        \hline
        \mbox{print string} & \mbox{display string on terminal output}
        \\
        \mbox{import sys; sys.argv[1]} & \mbox{first command-line
          argument} \\
        \hline
      \end{array}$
    \end{center}    
    

\end{document}
