day2/cheatsheet2.tex
author Shantanu <shantanu@fossee.in>
Thu, 19 Nov 2009 22:26:00 +0530
changeset 317 0eca6c542fce
parent 301 49bdffe4dca5
child 326 f57f42861770
permissions -rw-r--r--
Modified cheat sheet of session 1 and session 6 day 1.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
301
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     1
\documentclass[12pt]{article}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     2
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     3
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     4
\title{Python: Data Structures}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     5
\author{FOSSEE}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     6
\usepackage{listings}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     7
\lstset{language=Python,
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     8
    basicstyle=\ttfamily,
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
     9
commentstyle=\itshape\bfseries, 
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    10
showstringspaces=false
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    11
}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    12
\newcommand{\typ}[1]{\lstinline{#1}}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    13
\usepackage[english]{babel}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    14
\usepackage[latin1]{inputenc}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    15
\usepackage{times}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    16
\usepackage[T1]{fontenc}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    17
\usepackage{ae,aecompl}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    18
\usepackage{mathpazo,courier,euler}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    19
\usepackage[scaled=.95]{helvet}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    20
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    21
\begin{document}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    22
\date{}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    23
\vspace{-1in}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    24
\begin{center}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    25
\LARGE{Python: Data Structures}\\
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    26
\large{FOSSEE}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    27
\end{center}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    28
\section{Basic Looping}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    29
\typ{while}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    30
  \begin{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    31
In []: a, b = 0, 1
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    32
In []: while b < 10:
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    33
  ...:     print b,
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    34
  ...:     a, b = b, a + b # Fibonacci Sequence
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    35
\end{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    36
\typ{for} and \typ{range}\\
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    37
\typ{range([start,] stop[, step])}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    38
\begin{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    39
In []: for i in range(3, 10, 2):
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    40
 ....:     print i, i * i
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    41
3 9
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    42
5 25
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    43
7 49
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    44
9 81
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    45
\end{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    46
List methods (Contd.)
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    47
\begin{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    48
In []: num = [1, 2, 3, 4]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    49
In []: num.append([9, 10, 11])
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    50
In []: num
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    51
Out[]: [1, 2, 3, 4, [9, 10, 11]]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    52
In []: num = [1, 2, 3, 4]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    53
In []: num.extend([5, 6, 7])
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    54
In []: num
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    55
Out[]: [1, 2, 3, 4, 5, 6, 7]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    56
In []: num.reverse()
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    57
In []: num
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    58
Out[]: [7, 6, 5, 4, 3, 2, 1]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    59
In []: num.remove(6)
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    60
In []: num
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    61
\end{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    62
Slicing: \typ{list[initial:final:step]}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    63
\begin{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    64
In []: a[1:-1:2]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    65
Out[]: [2, 4]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    66
In []: a[::2]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    67
Out[]: [1, 3, 5]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    68
In []: a[-1::-1]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    69
Out[]: [5, 4, 3, 2, 1]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    70
\end{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    71
Tuples(Immutable lists)
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    72
\begin{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    73
In []: t = (1, 2, 3, 4, 5, 6, 7, 8)
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    74
In []: t[0] + t[3] + t[-1]
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    75
Out[]: 13
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    76
In []: t[4] = 7 # ERROR: tuples are immutable
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    77
\end{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    78
Sets
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    79
\begin{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    80
In []: f = set([1,2,3,5,8])
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    81
In []: p = set([2,3,5,7])
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    82
In []: f | p # Union of two sets
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    83
Out[]: set([1, 2, 3, 5, 7, 8])
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    84
In []: g = set([2, 4, 5, 7, 4, 0, 5])
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    85
In []: g
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    86
Out[]: set([2, 4, 5, 7, 0]) # No repetition allowed.
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    87
\end{lstlisting}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    88
\end{document}
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    89