|
1 % Created 2010-10-12 Tue 12:55 |
|
2 \documentclass[presentation]{beamer} |
|
3 \usepackage[latin1]{inputenc} |
|
4 \usepackage[T1]{fontenc} |
|
5 \usepackage{fixltx2e} |
|
6 \usepackage{graphicx} |
|
7 \usepackage{longtable} |
|
8 \usepackage{float} |
|
9 \usepackage{wrapfig} |
|
10 \usepackage{soul} |
|
11 \usepackage{t1enc} |
|
12 \usepackage{textcomp} |
|
13 \usepackage{marvosym} |
|
14 \usepackage{wasysym} |
|
15 \usepackage{latexsym} |
|
16 \usepackage{amssymb} |
|
17 \usepackage{hyperref} |
|
18 \tolerance=1000 |
|
19 \usepackage[english]{babel} \usepackage{ae,aecompl} |
|
20 \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} |
|
21 \usepackage{listings} |
|
22 \lstset{language=Python, basicstyle=\ttfamily\bfseries, |
|
23 commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, |
|
24 showstringspaces=false, keywordstyle=\color{blue}\bfseries} |
|
25 \providecommand{\alert}[1]{\textbf{#1}} |
|
26 |
|
27 \title{Getting started with for} |
|
28 \author{FOSSEE} |
|
29 \date{} |
|
30 |
|
31 \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} |
|
32 \begin{document} |
|
33 |
|
34 \maketitle |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 \begin{frame} |
|
45 \frametitle{Outline} |
|
46 \label{sec-1} |
|
47 |
|
48 \begin{itemize} |
|
49 \item \texttt{for} loop in Python. |
|
50 \item Blocks of code in Python. |
|
51 |
|
52 \begin{itemize} |
|
53 \item Indentation |
|
54 \end{itemize} |
|
55 |
|
56 \end{itemize} |
|
57 \end{frame} |
|
58 \begin{frame}[fragile] |
|
59 \frametitle{Whitespace in Python} |
|
60 \label{sec-2} |
|
61 |
|
62 \begin{itemize} |
|
63 \item Whitespace is significant |
|
64 |
|
65 \begin{itemize} |
|
66 \item blocks are visually separated |
|
67 \end{itemize} |
|
68 |
|
69 \item Blocks are indented using 4 spaces |
|
70 \begin{verbatim} |
|
71 Block A |
|
72 Block A |
|
73 Block B |
|
74 Block B |
|
75 Block A |
|
76 \end{verbatim} |
|
77 |
|
78 \texttt{Block B} is an inner block and is indented using 4 spaces |
|
79 \end{itemize} |
|
80 \end{frame} |
|
81 \begin{frame}[fragile] |
|
82 \frametitle{Exercise 1} |
|
83 \label{sec-3} |
|
84 |
|
85 Write a \texttt{for} loop which iterates through a list of numbers and find |
|
86 the square root of each number. |
|
87 \begin{verbatim} |
|
88 |
|
89 \end{verbatim} |
|
90 |
|
91 The numbers are, |
|
92 \begin{verbatim} |
|
93 1369, 7225, 3364, 7056, 5625, 729, 7056, |
|
94 576, 2916 |
|
95 \end{verbatim} |
|
96 \end{frame} |
|
97 \begin{frame}[fragile] |
|
98 \frametitle{Solution 1} |
|
99 \label{sec-4} |
|
100 |
|
101 \begin{itemize} |
|
102 \item Open text editor and type the following code |
|
103 \end{itemize} |
|
104 |
|
105 \begin{verbatim} |
|
106 numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, |
|
107 576, 2916] |
|
108 |
|
109 for each in numbers: |
|
110 print "Square root of", each, "is", sqrt(each) |
|
111 |
|
112 print "This is not in for loop!" |
|
113 \end{verbatim} |
|
114 \end{frame} |
|
115 \begin{frame}[fragile] |
|
116 \frametitle{Save \& run script} |
|
117 \label{sec-5} |
|
118 |
|
119 \begin{itemize} |
|
120 \item Save the script as \texttt{list\_roots.py} |
|
121 \item Run in \texttt{ipython} interpreter as, |
|
122 \begin{verbatim} |
|
123 In []: %run -i list_roots.py |
|
124 \end{verbatim} |
|
125 |
|
126 \end{itemize} |
|
127 \end{frame} |
|
128 \begin{frame}[fragile] |
|
129 \frametitle{Exercise 2} |
|
130 \label{sec-6} |
|
131 |
|
132 From the given numbers make a list of perfect squares and a list of those which are not. |
|
133 \begin{verbatim} |
|
134 |
|
135 \end{verbatim} |
|
136 |
|
137 The numbers are, |
|
138 \begin{verbatim} |
|
139 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, |
|
140 7056, 576, 2916 |
|
141 \end{verbatim} |
|
142 \end{frame} |
|
143 \begin{frame}[fragile] |
|
144 \frametitle{Exercise 3 (indentation in \texttt{ipython})} |
|
145 \label{sec-7} |
|
146 |
|
147 Print the square root of numbers in the list. |
|
148 \begin{verbatim} |
|
149 |
|
150 \end{verbatim} |
|
151 |
|
152 Numbers are, |
|
153 \begin{verbatim} |
|
154 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, |
|
155 7056, 576, 2916 |
|
156 \end{verbatim} |
|
157 \end{frame} |
|
158 \begin{frame}[fragile] |
|
159 \frametitle{Indentation in \texttt{ipython}} |
|
160 \label{sec-8} |
|
161 |
|
162 \begin{verbatim} |
|
163 In []: numbers = [1369, 7225, 3364, 7056, 5625, |
|
164 ...: 729, 7056, 576, 2916] |
|
165 \end{verbatim} |
|
166 |
|
167 |
|
168 \begin{verbatim} |
|
169 In []: for each in numbers: |
|
170 ...: |
|
171 \end{verbatim} |
|
172 |
|
173 Note the four spaces here |
|
174 \begin{verbatim} |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 \end{verbatim} |
|
182 \end{frame} |
|
183 \begin{frame}[fragile] |
|
184 \frametitle{Indentation in \texttt{ipython} (cont'd)} |
|
185 \label{sec-9} |
|
186 |
|
187 \begin{verbatim} |
|
188 In []: numbers = [1369, 7225, 3364, 7056, 5625, |
|
189 ...: 729, 7056, 576, 2916] |
|
190 In []: for each in numbers: |
|
191 ...: |
|
192 \end{verbatim} |
|
193 |
|
194 Note the four spaces here |
|
195 \begin{verbatim} |
|
196 |
|
197 \end{verbatim} |
|
198 |
|
199 Now type the rest of the code |
|
200 \begin{verbatim} |
|
201 ...: print "Square root of", each, |
|
202 ...: print "is", sqrt(each) |
|
203 ...: |
|
204 ...: |
|
205 \end{verbatim} |
|
206 \end{frame} |
|
207 \begin{frame}[fragile] |
|
208 \frametitle{Indentation in \texttt{python} interpreter} |
|
209 \label{sec-10} |
|
210 |
|
211 Find out the cube of all the numbers from 1 to 10. |
|
212 \begin{verbatim} |
|
213 |
|
214 \end{verbatim} |
|
215 |
|
216 \emph{do it in the python interpreter} |
|
217 \end{frame} |
|
218 \begin{frame}[fragile] |
|
219 \frametitle{Indentation in \texttt{python} interpreter (cont'd)} |
|
220 \label{sec-11} |
|
221 |
|
222 \begin{verbatim} |
|
223 >>> for i in range(1, 11): |
|
224 ... print i, "cube is", i**3 |
|
225 ... |
|
226 \end{verbatim} |
|
227 \end{frame} |
|
228 \begin{frame} |
|
229 \frametitle{\texttt{range()} function} |
|
230 \label{sec-12} |
|
231 |
|
232 \begin{itemize} |
|
233 \item in built function in Python |
|
234 \item generates a list of integers |
|
235 |
|
236 \begin{itemize} |
|
237 \item \emph{syntax:} range([start,] stop[, step]) |
|
238 \item \emph{example:} |
|
239 |
|
240 \begin{itemize} |
|
241 \item range(1, 20) - \emph{generates integers from 1 to 20} |
|
242 \item range(20) - \emph{generates integers from 0 to 20} |
|
243 \end{itemize} |
|
244 |
|
245 \end{itemize} |
|
246 |
|
247 \end{itemize} |
|
248 \end{frame} |
|
249 \begin{frame} |
|
250 \frametitle{Exercise 4} |
|
251 \label{sec-13} |
|
252 |
|
253 Print all the odd numbers from 1 to 50. |
|
254 \end{frame} |
|
255 \begin{frame} |
|
256 \frametitle{Summary} |
|
257 \label{sec-14} |
|
258 |
|
259 \begin{itemize} |
|
260 \item blocks in \texttt{python} |
|
261 \item indentation |
|
262 \item blocks in \texttt{ipython} interpreter |
|
263 \item \texttt{for} loop |
|
264 \item iterating over list using \texttt{for} loop |
|
265 \item \texttt{range()} function |
|
266 \end{itemize} |
|
267 \end{frame} |
|
268 \begin{frame} |
|
269 \frametitle{Thank you!} |
|
270 \label{sec-15} |
|
271 |
|
272 \begin{block}{} |
|
273 \begin{center} |
|
274 This spoken tutorial has been produced by the |
|
275 \textcolor{blue}{FOSSEE} team, which is funded by the |
|
276 \end{center} |
|
277 \begin{center} |
|
278 \textcolor{blue}{National Mission on Education through \\ |
|
279 Information \& Communication Technology \\ |
|
280 MHRD, Govt. of India}. |
|
281 \end{center} |
|
282 \end{block} |
|
283 \end{frame} |
|
284 |
|
285 \end{document} |