day2/cheatsheet2.tex
author Shantanu <shantanu@fossee.in>
Wed, 25 Nov 2009 12:11:03 +0530
changeset 327 c78cad28c2f7
parent 326 f57f42861770
child 329 0a6ab1d81491
permissions -rw-r--r--
modifeid cheat sheets for session 3, 4 day 2.
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}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    29
\subsection{\typ{while}}
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
    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
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    32
In []: while b < 10:  
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
    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
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    35
  ...:
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
    36
\end{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    37
Basic syntax of \typ{while} loop is:
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    38
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    39
while condition:
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    40
    statement1
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    41
    statement2
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    42
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    43
All the statements are executed, till the condition statement evaluates to True.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    44
\subsection{\typ{for} and \typ{range}}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    45
\typ{range(start, stop, step)}\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    46
returns a list containing an arithmetic progression of integers.\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    47
Of the arguments mentioned above, both start and step are optional.\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    48
For example, if we skip third argument, i.e \typ{step}, then default value is 1. So:
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    49
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    50
In []: range(1,10)
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    51
Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    52
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    53
\textbf{Note:} stop value is not included in the list.\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    54
Similarly if we don't pass \typ{start} argument, default is taken to be 0.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    55
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    56
In []: range(10)
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    57
Out[]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    58
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    59
In case \typ{step} is mentioned, the jump between consecutive members of the list would be equal to that.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    60
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    61
In []: range(1,10,2)
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    62
Out[]: [1, 3, 5, 7, 9]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    63
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    64
%Notice the jump between two consecutive elements is 2, i.e step.\\
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
    65
\typ{for} and \typ{range}\\
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    66
As mentioned previously \typ{for} in Python is used to iterate through the list members. So \typ{for} and \typ{range} can be used together to iterate through required series. For example to get square of all numbers less then 5 and greater then equal to 0, code can be written as:
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
    67
\begin{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    68
In []: for i in range(5):
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
    69
 ....:     print i, i * i
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    70
 ....:
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    71
 ....:
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    72
0 0
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    73
1 1
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    74
2 4
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
    75
3 9
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    76
4 16
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    77
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    78
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    79
\section{list}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    80
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    81
In []: num = [1, 2, 3, 4] # Initializing a list
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    82
In []: num
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    83
Out[]: [1, 2, 3, 4]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    84
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    85
\subsection{Accessing individual elements}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    86
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    87
In []: num[1]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    88
Out[]: 2  
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    89
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    90
\textbf{Note:} Index of list starts from 0.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    91
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    92
In []: num[5]  # ERROR: throws a index error
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    93
IndexError: list index out of range
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    94
In []: num[-1]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    95
Out[]: 4
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
    96
\end{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    97
\textbf{Note: }\typ{-1} points the last element in a list. Similarly to access third last element of a list one can use: 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    98
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    99
In []: num[-3]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   100
Out[]: 2  
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   101
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   102
\subsection{Adding lists and elements}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   103
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   104
In []: num += [9, 10, 11] # Adding two lists
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   105
In []: num
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   106
Out[]: [1, 2, 3, 4, 9, 10, 11]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   107
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   108
\typ{list} provides \typ{append} function to append objects at the end. 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   109
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   110
In []: num = [1, 2, 3, 4] 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   111
In []: num.append(-2) 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   112
In []: num
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   113
Out[]: [1, 2, 3, 4, -2]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   114
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   115
%% In []: num += [-5] 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   116
%% In []: num
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   117
%% Out[]: [1, 2, 3, 4, -2, -5]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   118
Working of \typ{append} is different from \typ{+} operator on list. Till here both will behave as same. But in following case:
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
   119
\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
   120
In []: num = [1, 2, 3, 4]
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   121
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   122
In []: num + [9, 10, 11]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   123
Out[]: [1, 2, 3, 4, 9, 10, 11]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   124
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   125
In []: num.append([9, 10, 11]) # appending a list to a list
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   126
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
   127
In []: num
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   128
Out[]: [1, 2, 3, 4, [9, 10, 11]] # last element is a list
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   129
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   130
when one attempts to append a list(in above case [9, 10, 11]) to a list(num) it adds list as a single element. So the resulting list will have a element which itself is a list. But \typ{+} operator would simply add the elements of second list.\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   131
\subsection{Miscellaneous}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   132
\begin{lstlisting}
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
   133
In []: num = [1, 2, 3, 4]
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   134
In []: num.extend([5, 6, 7])  # extend list by adding elements
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
   135
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
   136
Out[]: [1, 2, 3, 4, 5, 6, 7]
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   137
In []: num.reverse()  # reverse the current list
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
   138
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
   139
Out[]: [7, 6, 5, 4, 3, 2, 1]
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   140
In []: num.remove(6) # removing first occurrence of 6
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
   141
In []: num
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   142
Out[]: [7, 5, 4, 3, 2, 1]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   143
In []: len(num) # returns the length of list
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   144
Out[]: 6
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   145
In []: a = [1, 5, 3, 7, -2, 4]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   146
In []: min(a) # returns smallest item in a list.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   147
Out[]: -2
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   148
In []: max(a) # returns largest item in a list.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   149
Out[]: 7
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
   150
\end{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   151
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   152
\subsection{Slicing} 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   153
General syntax for getting slice out of a list is \\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   154
\typ{list[initial:final:step]}
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
   155
\begin{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   156
In []: a = [1, 2, 3, 4, 5]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   157
In []: a[1:-1:2] 
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
   158
Out[]: [2, 4]
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   159
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   160
Start slice from second element(1), till the last element(-1) with step size of 2.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   161
\begin{lstlisting}
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
   162
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
   163
Out[]: [1, 3, 5]
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   164
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   165
Start from beginning(since \typ{initial} is blank), till last of list(this time last element is included, as \typ{final} is blank), with step size of 2.\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   166
Apart from using \typ{reverse} command on list, one can also use slicing in special way to get reverse of a list.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   167
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   168
In []: a[-1:-4:-1]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   169
Out[]: [5, 4, 3]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   170
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   171
Above syntax of slice can be expressed as, ``start from last element(\typ{-1}), go till fourth last element(\typ{-4}), with step size \typ{-1}, which implies, go in reverse direction. That is, first element would be \typ{a[-1]}, second element would be \typ{a[-2]} and so on and so forth.''\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   172
So to get reverse of whole list one can write following slice syntax:
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   173
\begin{lstlisting}
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
   174
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
   175
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
   176
\end{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   177
Since \typ{final} is left blank, it will traverse through whole list in reverse manner.\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   178
\textbf{Note:} While \typ{reverse} reverses the original list, slicing will just result in a instance list with reverse of original, which can be used and worked upon independently.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   179
%%Should we include  list copy concept here?
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   180
\subsection{Containership}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   181
\typ{in} keyword is used to check for containership of any element in a given list.
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
   182
\begin{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   183
In []: a = [2, 5, 4, 6, 9]
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   184
In []: 4 in a
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   185
Out[]: True
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   186
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   187
In []: b = 15
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   188
In []: b in a
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   189
Out[]: False
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   190
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   191
\section{Tuples}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   192
Tuples are sequences just like Lists, but they are \textbf{immutable}, or items/elements cannot be changed in any way.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   193
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   194
In []: t = (1, 2, 3, 4, 5, 6, 7, 8) 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   195
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   196
\textbf{Note:} For tupels we use parantheses in place of square brackets, rest is same as lists.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   197
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   198
In []: t[0] + t[3] + t[-1] # elements are accessed via indices
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
   199
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
   200
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
   201
\end{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   202
\textbf{Note:} elements cant be changed!
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   203
\section{Dictionaries}
327
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   204
Dictionaries are data structures that provide key-value mappings. They are similar to lists except that instead of the values having integer indexes, they have keys or strings as indexes.\\
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   205
A simple dictionary can be created by:
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   206
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   207
In []: player = {'Mat': 134,'Inn': 233,
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   208
          'Runs': 10823, 'Avg': 52.53}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   209
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   210
For above case, value on left of ':' is key and value on right is corresponding value. To retrieve value related to key 'Avg'
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   211
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   212
In []: player['Avg']
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   213
Out[]: 52.530000000000001
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   214
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   215
\subsection{Element operations}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   216
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   217
In []: player['Name'] = 'Rahul Dravid' #Adds new key-value pair.
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   218
In []: player
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   219
Out[]: 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   220
{'Avg': 52.530000000000001,
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   221
 'Inn': 233,
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   222
 'Mat': 134,
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   223
 'Name': 'Rahul Dravid',
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   224
 'Runs': 10823}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   225
In []: player.pop('Mat') # removing particular key-value pair
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   226
Out[]: 134
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   227
In [21]: player
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   228
Out[21]: {'Avg': 52.530000000000001, 'Inn': 233, 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   229
          'Name': 'Rahul Dravid', 'Runs': 10823}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   230
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   231
\textbf{Note:} Duplicate keys are overwritten!\\
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   232
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   233
In []: player['Name'] = 'Dravid'
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   234
In []: player
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   235
Out[23]: {'Avg': 52.530000000000001, 'Inn': 233, 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   236
          'Name': 'Dravid', 'Runs': 10823}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   237
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   238
\subsection{containership}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   239
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   240
In []: 'Inn' in player
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   241
Out[]: True
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   242
In []: 'Econ' in player
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   243
Out[]: False
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   244
\end{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   245
\textbf{Note:} Containership is always checked on 'keys' of dictionary but not 'values'.\\
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   246
\subsection{Methods}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   247
\begin{lstlisting}
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   248
In []: player.keys() # returns list of all keys
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   249
Out[]: ['Runs', 'Inn', 'Avg', 'Mat']
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   250
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   251
In []: player.values() # returns list of all values.
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   252
Out[]: [10823, 233, 
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   253
        52.530000000000001, 134]  
c78cad28c2f7 modifeid cheat sheets for session 3, 4 day 2.
Shantanu <shantanu@fossee.in>
parents: 326
diff changeset
   254
\end{lstlisting}
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   255
\section{Sets}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   256
are an unordered collection of unique elements.\\
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   257
Creation:
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   258
\begin{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   259
In []: s = set([2,4,7,8,5]) # creating a basic set
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   260
In []: s
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   261
Out[]: set([2, 4, 5, 7, 8])
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   262
In []: g = set([2, 4, 5, 7, 4, 0, 5])
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   263
In []: g
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   264
Out[]: set([0, 2, 4, 5, 7]) # No repetition allowed.
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   265
\end{lstlisting}
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   266
Some other operations which can be performed on sets are:
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
   267
\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
   268
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
   269
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
   270
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
   271
Out[]: set([1, 2, 3, 5, 7, 8])
326
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   272
In []: f & p # Intersection of two sets
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   273
Out[]: set([2, 3, 5])
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   274
In []: f - p # Elements in f not is p
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   275
Out[]: set([1, 8])
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   276
In []: f ^ p # (f - p) | (p - f)
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   277
Out[]: set([1, 7, 8])) 
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   278
In []: set([2,3]) < p # Test for subset
f57f42861770 Cheat sheet for session 2 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   279
Out[]: True
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
   280
\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
   281
\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
   282