Modified cheat sheet of session 1 day 2.
authorShantanu <shantanu@fossee.in>
Fri, 20 Nov 2009 00:34:18 +0530
changeset 321 8bf99f747817
parent 317 0eca6c542fce
child 322 54fb746c1565
Modified cheat sheet of session 1 day 2.
day1/cheatsheet6.tex
day2/cheatsheet1.tex
--- a/day1/cheatsheet6.tex	Thu Nov 19 22:26:00 2009 +0530
+++ b/day1/cheatsheet6.tex	Fri Nov 20 00:34:18 2009 +0530
@@ -115,4 +115,9 @@
 
   In []: plot(t, y)
 \end{lstlisting}
+\section{Links and References}
+\begin{itemize}
+\item Documentation for Numpy and Scipy is available at: \url{http://docs.scipy.org/doc/}
+  \item For "recipes" or worked examples of commonly-done tasks in SciPy explore: \url{http://www.scipy.org/Cookbook/}
+\end{itemize}
 \end{document}
--- a/day2/cheatsheet1.tex	Thu Nov 19 22:26:00 2009 +0530
+++ b/day2/cheatsheet1.tex	Fri Nov 20 00:34:18 2009 +0530
@@ -26,95 +26,151 @@
 \large{FOSSEE}
 \end{center}
 \section{Data types}
-Complex Numbers
+\subsection{int and float}
+A whole number is a \typ{int} variable.
 \begin{lstlisting}
-In []: c = 3+4j
-In []: abs(c)
+In []: a = 13
+In []: type(a)
+Out[]: <type 'int'>
+In []: b = -2
+In []: type(b)
+Out[]: <type 'int'>
+In []: c = 500000000
+In []: type(c)
+Out[]: <type 'int'>
+\end{lstlisting}
+A number with decimal is a \typ{float}.
+\begin{lstlisting}
+In []: p = 3.141592
+In []: type(p)
+Out[]: <type 'float'>
+\end{lstlisting}
+\subsection{Complex Numbers}
+\begin{lstlisting}
+In []: c = 3+4j  #coeff of j specifies imaginary part
+In []: abs(c) #absolute value of complex number
 Out[]: 5.0
-In []: c.imag
+In []: c.imag #accessing imaginary part of c
 Out[]: 4.0
-In []: c.real
+In []: c.real #accessing real part of c
 Out[]: 3.0
 \end{lstlisting}
-Boolean
+\newpage
+\subsection{Boolean}
 \begin{lstlisting}     
 In []: a = False
 In []: b = True
 In []: c = True
-In []: (a and b) or c
+In []: (a and b) or c #Boolean operations
 Out[]: True
 \end{lstlisting}
-Strings
+\textbf{Note:} Python is case sensitive language, \typ{True} is \typ{bool} type, but \typ{true} would be a variable. and hence following assignment fails:\\
+\typ{In []: a = true}\\
+\subsection{Strings}
   \begin{lstlisting}
-In []: w = "hello"
-In []: print w[0] + w[2] + w[-1]
-Out[]: hlo
-In []: len(w)
+In []: w = "hello" #w is string variable
+In []: print w[1]
+Out[]: e
+In []: print w[-1] #last character of string
+Out[]: o
+  \end{lstlisting}
+\textbf{Note:} For a string variable, individual elements can be accessed using indices.
+  \begin{lstlisting}
+In []: len(w) #function to calculate length of string
 Out[]: 5
 In []: w[0] = 'H' # ERROR: Strings are immutable 
   \end{lstlisting}
-String methods
+\subsection{String methods}
   \begin{lstlisting}
 In []: a = 'Hello World' 
 In []: a.startswith('Hell') # 'a' starts with 'Hell'
+Out[]: True
 In []: a.endswith('ld') # 'a' ends with 'ld'
+Out[]: True
 In []: a.upper() # all characters to upper case
+Out[]: 'HELLO WORLD'
 In []: a.lower() # all characters to lower case
+Out[]: 'hello world'
 In []: ''.join(['a', 'b', 'c'])
 Out[]: 'abc'
   \end{lstlisting}
-String formatting
+\typ{join} function joins all the list member passed as argument with the string it is called upon. In above case it is \typ{empty string}.
+\begin{lstlisting}
+In []: ' '.join(['a','b','c'])
+Out[]: 'a b c'  
+In []: ','.join(['a','b','c'])
+Out[]: 'a,b,c'
+\end{lstlisting}
+\subsection{String formatting}
   \begin{lstlisting}
-In []: x, y = 1, 1.234
+In []: x, y = 1, 1.234 #initializing two variables
 In []: 'x is %s, y is %s' %(x, y)
 Out[]: 'x is 1, y is 1.234'
   \end{lstlisting}
-Arithmetic Operators
+\textbf{Note:} \typ{\%s} used in above fomatting specifies \typ{'str'} representation of variables. One can also try:\\
+\typ{\%d} for \typ{int} representation\\
+\typ{\%f} for \typ{float} representation
+\begin{lstlisting}
+In []: 'x is %f, y is %f' %(x, y)
+Out[]: 'x is 1.000000, y is 1.234000'
+
+In []: 'x is %d, y is %d' %(x, y)
+Out[]: 'x is 1, y is 1'
+\end{lstlisting}
+\subsection{Arithmetic Operators}
   \begin{lstlisting}
 In []: 45 % 2 # Modulo operator
 Out[]: 1
-In []: 1234567891234567890 ** 3 # Power
+In []: 5 ** 3 # Power
+Out[]: 125
 In []: a = 5
-In []: a += 1
+In []: a += 1 #increment by 1, translates to a = a + 1
 In []: a *= 2
   \end{lstlisting}
-String Operations
+\subsection{String Operations}
 \begin{lstlisting}
 In []: s = 'Hello'
 In []: p = 'World'
-In []: s + p 
+In []: s + p  #concatenating two strings
 Out[]: 'HelloWorld'
-In []: s * 4
+In []: s * 4  #repeating string for given num of times
 Out[]: 'HelloHelloHelloHello'
 \end{lstlisting}
-Relational and Logical Operators
+\subsection{Relational and Logical Operators}
 \begin{lstlisting}
-In []: p, z, n = 1, 0, -1
-In []: p == n
+In []: p, z, n = 1, 0, -1 #initializing three variables
+In []: p == n  #equivalency check
 Out[]: False
-In []: p >= n
+In []: p >= n 
 Out[]: True
-In []: n < z < p
+In []: n < z < p #finding largest number among three
 Out[]: True
 In []: p + n != z
 Out[]: False
 \end{lstlisting}
-Built-ins
+\subsection{Built-ins}
 \begin{lstlisting}
-In []: int(17 / 2.0)
+In []: int(17 / 2.0) #converts arguments to integer
 Out[]: 8
-In []: float(17 / 2)
+In []: float(17 / 2) #argument is already integer(17 / 2 = 8)
 Out[]: 8.0
-In []: str(17 / 2.0)
+In []: str(17 / 2.0) #converts to string
 Out[]: '8.5'
-In []: round( 7.5 )
+In []: round( 7.5 ) 
 Out[]: 8.0
 \end{lstlisting}
-Console Input
+\subsection{Console Input}
 \begin{lstlisting}
 In []: a = raw_input('Enter a value: ')
 Enter a value: 5
 \end{lstlisting}
+\textbf{Note:} \typ{raw_input} always returns string representation of user input and hence:
+\begin{lstlisting}
+In []: type(a)
+Out[]: <type 'str'>
+\end{lstlisting}
+To get integer or floating point of this input, one has to perform type conversion:\\
+\typ{In []: b = int(a)}
 \section{Conditionals}
 \typ{if}
 \begin{lstlisting}
@@ -133,4 +189,10 @@
 In []: a = raw_input('Enter number(Q to quit):')
 In []: num = int(a) if a != 'Q' else 0
 \end{lstlisting}
+Above statement can be read as ``num is int of a, if a is not equal to 'Q', otherwise 0 ``
+\section{Links and References}
+\begin{itemize}
+  \item Reference manual to describe the standard libraries  that are distributed with Python is available at \url{http://docs.python.org/library/} 
+  \item To read more on strings refer to: \\ \url{http://docs.python.org/library/stdtypes.html#string-methods}
+\end{itemize}
 \end{document}