day2/cheatsheet1.tex
author Shantanu <shantanu@fossee.in>
Fri, 20 Nov 2009 00:34:18 +0530
changeset 321 8bf99f747817
parent 301 49bdffe4dca5
child 327 c78cad28c2f7
permissions -rwxr-xr-x
Modified cheat sheet of session 1 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: Basics}
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: Basics}\\
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{Data types}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    29
\subsection{int and float}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    30
A whole number is a \typ{int} variable.
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
    31
\begin{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    32
In []: a = 13
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    33
In []: type(a)
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    34
Out[]: <type 'int'>
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    35
In []: b = -2
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    36
In []: type(b)
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    37
Out[]: <type 'int'>
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    38
In []: c = 500000000
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    39
In []: type(c)
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    40
Out[]: <type 'int'>
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    41
\end{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    42
A number with decimal is a \typ{float}.
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    43
\begin{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    44
In []: p = 3.141592
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    45
In []: type(p)
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    46
Out[]: <type 'float'>
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    47
\end{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    48
\subsection{Complex Numbers}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    49
\begin{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    50
In []: c = 3+4j  #coeff of j specifies imaginary part
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    51
In []: abs(c) #absolute value of complex number
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
    52
Out[]: 5.0
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    53
In []: c.imag #accessing imaginary part of c
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
    54
Out[]: 4.0
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    55
In []: c.real #accessing real part of c
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
    56
Out[]: 3.0
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
\end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    58
\newpage
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    59
\subsection{Boolean}
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
    60
\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
    61
In []: a = 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
    62
In []: b = True
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
In []: c = True
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    64
In []: (a and b) or c #Boolean operations
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
Out[]: True
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
\end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    67
\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:\\
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    68
\typ{In []: a = true}\\
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    69
\subsection{Strings}
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
    70
  \begin{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    71
In []: w = "hello" #w is string variable
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    72
In []: print w[1]
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    73
Out[]: e
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    74
In []: print w[-1] #last character of string
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    75
Out[]: o
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    76
  \end{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    77
\textbf{Note:} For a string variable, individual elements can be accessed using indices.
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    78
  \begin{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    79
In []: len(w) #function to calculate length of string
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
    80
Out[]: 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
    81
In []: w[0] = 'H' # ERROR: Strings 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
    82
  \end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    83
\subsection{String methods}
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
    84
  \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
    85
In []: a = 'Hello World' 
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
In []: a.startswith('Hell') # 'a' starts with 'Hell'
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    87
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
    88
In []: a.endswith('ld') # 'a' ends with 'ld'
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    89
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
    90
In []: a.upper() # all characters to upper case
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    91
Out[]: 'HELLO WORLD'
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
    92
In []: a.lower() # all characters to lower case
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    93
Out[]: 'hello world'
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
    94
In []: ''.join(['a', 'b', 'c'])
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
    95
Out[]: 'abc'
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}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    97
\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}.
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    98
\begin{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
    99
In []: ' '.join(['a','b','c'])
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   100
Out[]: 'a b c'  
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   101
In []: ','.join(['a','b','c'])
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   102
Out[]: 'a,b,c'
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   103
\end{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   104
\subsection{String formatting}
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
   105
  \begin{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   106
In []: x, y = 1, 1.234 #initializing two variables
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
   107
In []: 'x is %s, y is %s' %(x, y)
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   108
Out[]: 'x is 1, y is 1.234'
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   109
  \end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   110
\textbf{Note:} \typ{\%s} used in above fomatting specifies \typ{'str'} representation of variables. One can also try:\\
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   111
\typ{\%d} for \typ{int} representation\\
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   112
\typ{\%f} for \typ{float} representation
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   113
\begin{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   114
In []: 'x is %f, y is %f' %(x, y)
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   115
Out[]: 'x is 1.000000, y is 1.234000'
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   116
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   117
In []: 'x is %d, y is %d' %(x, y)
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   118
Out[]: 'x is 1, y is 1'
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   119
\end{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   120
\subsection{Arithmetic Operators}
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
   121
  \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
   122
In []: 45 % 2 # Modulo operator
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   123
Out[]: 1
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   124
In []: 5 ** 3 # Power
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   125
Out[]: 125
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
   126
In []: a = 5
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   127
In []: a += 1 #increment by 1, translates to a = a + 1
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
   128
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
   129
  \end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   130
\subsection{String Operations}
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
   131
\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
   132
In []: s = 'Hello'
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 []: p = 'World'
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   134
In []: s + p  #concatenating two strings
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
Out[]: 'HelloWorld'
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   136
In []: s * 4  #repeating string for given num of times
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
   137
Out[]: 'HelloHelloHelloHello'
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
\end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   139
\subsection{Relational and Logical Operators}
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
   140
\begin{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   141
In []: p, z, n = 1, 0, -1 #initializing three variables
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   142
In []: p == n  #equivalency check
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
   143
Out[]: False
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   144
In []: p >= n 
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
   145
Out[]: True
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   146
In []: n < z < p #finding largest number among three
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
   147
Out[]: True
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   148
In []: p + n != z
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   149
Out[]: 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
   150
\end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   151
\subsection{Built-ins}
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
   152
\begin{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   153
In []: int(17 / 2.0) #converts arguments to integer
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
   154
Out[]: 8
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   155
In []: float(17 / 2) #argument is already integer(17 / 2 = 8)
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
   156
Out[]: 8.0
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   157
In []: str(17 / 2.0) #converts to string
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[]: '8.5'
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   159
In []: round( 7.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
   160
Out[]: 8.0
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   161
\end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   162
\subsection{Console Input}
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
   163
\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
   164
In []: a = raw_input('Enter a value: ')
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   165
Enter a value: 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
   166
\end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   167
\textbf{Note:} \typ{raw_input} always returns string representation of user input and hence:
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   168
\begin{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   169
In []: type(a)
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   170
Out[]: <type 'str'>
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   171
\end{lstlisting}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   172
To get integer or floating point of this input, one has to perform type conversion:\\
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   173
\typ{In []: b = int(a)}
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
\section{Conditionals}
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
\typ{if}
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
\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
   177
In []: x = int(raw_input("Enter an integer:"))
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   178
In []: if x < 0:
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   179
  ...:     print 'Be positive!'
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   180
  ...: elif x == 0:
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   181
  ...:     print 'Zero'
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
  ...: elif x == 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
   183
  ...:     print 'Single'
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   184
  ...: else:
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   185
  ...:     print 'More'
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   186
\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
   187
Ternary Operator
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   188
\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
   189
In []: a = raw_input('Enter number(Q to quit):')
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   190
In []: num = int(a) if a != 'Q' else 0
49bdffe4dca5 Updated session 2 slides of day 1 and added cheatsheets of day 2.
Santosh G. Vattam <vattam.santosh@gmail.com>
parents:
diff changeset
   191
\end{lstlisting}
321
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   192
Above statement can be read as ``num is int of a, if a is not equal to 'Q', otherwise 0 ``
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   193
\section{Links and References}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   194
\begin{itemize}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   195
  \item Reference manual to describe the standard libraries  that are distributed with Python is available at \url{http://docs.python.org/library/} 
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   196
  \item To read more on strings refer to: \\ \url{http://docs.python.org/library/stdtypes.html#string-methods}
8bf99f747817 Modified cheat sheet of session 1 day 2.
Shantanu <shantanu@fossee.in>
parents: 301
diff changeset
   197
\end{itemize}
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
   198
\end{document}