day1/cheatsheet2.tex
author Puneeth Chaganti <punchagan@fossee.in>
Mon, 25 Jan 2010 12:06:18 +0530
changeset 367 3c911c958bcf
parent 341 7ae88b9da553
permissions -rwxr-xr-x
Added tot.pdf to Makefile.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
284
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
\documentclass[12pt]{article}
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     2
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     3
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     4
\title{Plotting Points}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     5
\author{FOSSEE}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     6
\usepackage{listings}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     7
\lstset{language=Python,
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     8
    basicstyle=\ttfamily,
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
     9
  commentstyle=\itshape\bfseries,
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    10
  showstringspaces=false,
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    11
}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    12
\newcommand{\typ}[1]{\lstinline{#1}}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    13
\usepackage[english]{babel}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    14
\usepackage[latin1]{inputenc}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    15
\usepackage{times}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    16
\usepackage[T1]{fontenc}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    17
\usepackage{ae,aecompl}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    18
\usepackage{mathpazo,courier,euler}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    19
\usepackage[scaled=.95]{helvet}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    20
284
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
\begin{document}
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
\date{}
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
\vspace{-1in}
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
\begin{center}
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    25
\LARGE{Plotting Points}\\
284
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
\large{FOSSEE}
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
\end{center}
309
0b1f2c378d84 Minor changes on cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 300
diff changeset
    28
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    29
\section{Plotting Points with Lists}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    30
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    31
\begin{lstlisting}
330
46533051b9d3 commited changes made for scipy and cheatsheats.
Shantanu <shantanu@fossee.in>
parents: 314
diff changeset
    32
In []: x = [0, 1, 2, 3] # Creating a list
311
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    33
In []: y = [7, 11, 15, 19]
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    34
In []: plot(x, y)
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    35
In []: clf()
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    36
In []: plot(x, y, 'o')  # Plotting Circles
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    37
\end{lstlisting}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    38
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    39
\subsection{Line style/marker}
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    40
\begin{lstlisting}
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    41
The following format string characters are accepted 
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    42
to control the line style or marker:
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    43
    
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    44
    ================    ===============================
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    45
    character           description
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    46
    ================    ===============================
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    47
    '-'                 solid line style
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    48
    '--'                dashed line style
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    49
    '-.'                dash-dot line style
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    50
    ':'                 dotted line style
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    51
    '.'                 point marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    52
    ','                 pixel marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    53
    'o'                 circle marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    54
    'v'                 triangle_down marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    55
    '^'                 triangle_up marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    56
    '<'                 triangle_left marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    57
    '>'                 triangle_right marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    58
    '1'                 tri_down marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    59
    '2'                 tri_up marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    60
    '3'                 tri_left marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    61
    '4'                 tri_right marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    62
    's'                 square marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    63
    'p'                 pentagon marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    64
    '*'                 star marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    65
    'h'                 hexagon1 marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    66
    'H'                 hexagon2 marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    67
    '+'                 plus marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    68
    'x'                 x marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    69
    'D'                 diamond marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    70
    'd'                 thin_diamond marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    71
    '|'                 vline marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    72
    '_'                 hline marker
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    73
    ================    ===============================
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    74
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    75
\end{lstlisting}
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    76
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    77
\subsection{Marker combinations}
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    78
\typ{In []: plot(x, y, 'ro')} \\
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    79
This plots figure with red colored filled circles.\\
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    80
Similarly other combination of colors and marker can be used.
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    81
\section{Lists}
284
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    83
Initializing
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    84
  \begin{lstlisting}
311
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    85
In []: mtlist = []      # Empty List
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    86
In []: lst = [ 1, 2, 3, 4, 5] 
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    87
  \end{lstlisting}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    88
Slicing
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    89
\begin{lstlisting}
311
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    90
In []: lst[1:3]         # A slice.
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    91
Out[]: [2, 3]
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    92
311
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    93
In []: lst[1:-1]
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    94
Out[]: [2, 3, 4]
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    95
\end{lstlisting}
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
    96
\subsection{Appending to lists}
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
    97
\begin{lstlisting}
311
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    98
In []: a = [ 6, 7, 8, 9]
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
    99
In []: b = lst + a
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
   100
In []: b
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
   101
Out[]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
284
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
311
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
   103
In []: lst.append(6)
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
   104
In []: lst
5303c34a3180 Minor edits to cheatsheet session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 309
diff changeset
   105
Out[]: [ 1, 2, 3, 4, 5, 6]
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   106
\end{lstlisting}
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   107
\subsection{Iterating over a List}
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   108
\begin{lstlisting}
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   109
In []: for element in b:  # Iterating over the list, element-wise
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   110
 ....:     print element       # Print each element
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   111
 ....:
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   112
\end{lstlisting}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   113
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   114
\section{Strings}
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   115
\subsection{Splitting Strings}
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   116
\begin{lstlisting}
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   117
In []: greet = ``hello world''
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   118
In []: print greet.split()
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   119
Out[]: ['hello', 'world']
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   120
In []: greet = ``hello, world''
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   121
In []: print greet.split(',')
340
347ff2714deb Minor edits to correct spellings.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 314
diff changeset
   122
Out[]: ['hello', ' world'] # Note the white space before 'world'
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   123
\end{lstlisting}
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   124
A string can be split based on the delimiter specified within quotes. A combination of more than one delimiter can also be used.\\
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   125
\typ{In []: greet.split(', ')}\\
340
347ff2714deb Minor edits to correct spellings.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 314
diff changeset
   126
\typ{Out[]: ['hello', 'world']}\\Note the white space is not there anymore.
314
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   127
\newpage
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   128
\section{Plotting from Files}
314
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   129
\subsection{Opening files}
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   130
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   131
\typ{In []: f = open('datafile.txt')}\\By default opens in read mode. \\If file does not exist then it throws an exception\\
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   132
\typ{In []: f = open('datafile.txt','r')}\\Specifying the read mode\\
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   133
\typ{In []: f = open('datafile.txt', 'w')}\\Opens the file in write mode. \\If the file already exists, then it deletes all the previous content and opens.
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   134
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   135
\subsection{Reading from files}
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   136
Just like lists files are iterable as well.
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   137
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   138
\begin{lstlisting}
314
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   139
  In []: for line in f:
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   140
    ...:     print line
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   141
    ...: 
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   142
    ...:
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   143
\end{lstlisting}
314
c9f05808e1c4 Updated cheatsheet of session 2 day 1.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents: 313
diff changeset
   144
313
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   145
\subsection{Plotting}
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   146
\begin{lstlisting}
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   147
l = []
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   148
t = []
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   149
for line in open('pendulum.txt'):
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   150
    point = line.split()
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   151
    l.append(float(point[0]))
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   152
    t.append(float(point[1]))
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   153
tsq = []
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   154
for time in t:
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   155
    tsq.append(time*time)
f5bcb974a665 Initial changes to cheat sheet for session 2.
Shantanu <shantanu@fossee.in>
parents: 311
diff changeset
   156
plot(l, tsq, '.')
295
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   157
\end{lstlisting}
39d7c4e14585 Added all day 1 cheatsheets.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 284
diff changeset
   158
284
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
\end{document}
3c191accbb32 Added Cheatsheets for day1.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160