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