|
1 % Created 2010-11-10 Wed 18: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 functions} |
|
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 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 \begin{frame} |
|
50 \frametitle{Outline} |
|
51 \label{sec-1} |
|
52 |
|
53 \begin{itemize} |
|
54 \item Define functions |
|
55 \item Pass arguments to functions |
|
56 \item Learn about docstrings |
|
57 \item Return values from functions |
|
58 \end{itemize} |
|
59 \end{frame} |
|
60 \begin{frame} |
|
61 \frametitle{Function} |
|
62 \label{sec-2} |
|
63 |
|
64 \begin{itemize} |
|
65 \item Eliminate code redundancy |
|
66 \item Help in code reuse |
|
67 \item Subroutine |
|
68 |
|
69 \begin{itemize} |
|
70 \item relatively independent of remaining code |
|
71 \end{itemize} |
|
72 |
|
73 \end{itemize} |
|
74 \end{frame} |
|
75 \begin{frame}[fragile] |
|
76 \frametitle{\texttt{f(x)} a mathematical function} |
|
77 \label{sec-3} |
|
78 |
|
79 |
|
80 $f(x) = x^{2}$ |
|
81 |
|
82 \begin{verbatim} |
|
83 f(1) -> 1 |
|
84 f(2) -> 4 |
|
85 \end{verbatim} |
|
86 \end{frame} |
|
87 \begin{frame}[fragile] |
|
88 \frametitle{Define \texttt{f(x)} in Python} |
|
89 \label{sec-4} |
|
90 |
|
91 \begin{verbatim} |
|
92 def f(x): |
|
93 return x*x |
|
94 \end{verbatim} |
|
95 |
|
96 \begin{itemize} |
|
97 \item \texttt{def} - keyword |
|
98 \item \texttt{f} - function name |
|
99 \item \texttt{x} - parameter / argument to function \texttt{f} |
|
100 \end{itemize} |
|
101 \end{frame} |
|
102 \begin{frame} |
|
103 \frametitle{Exercise 1} |
|
104 \label{sec-5} |
|
105 |
|
106 |
|
107 Write a python function named \texttt{cube} which computes the cube of a given |
|
108 number \texttt{n}. |
|
109 |
|
110 \begin{itemize} |
|
111 \item Pause here and try to solve the problem yourself. |
|
112 \end{itemize} |
|
113 \end{frame} |
|
114 \begin{frame}[fragile] |
|
115 \frametitle{Solution} |
|
116 \label{sec-6} |
|
117 |
|
118 \begin{verbatim} |
|
119 def cube(n): |
|
120 return n**3 |
|
121 \end{verbatim} |
|
122 \end{frame} |
|
123 \begin{frame}[fragile] |
|
124 \frametitle{\texttt{greet} function} |
|
125 \label{sec-7} |
|
126 |
|
127 |
|
128 Function \texttt{greet} which will print \texttt{Hello World!}. |
|
129 \begin{verbatim} |
|
130 def greet(): |
|
131 print "Hello World!" |
|
132 \end{verbatim} |
|
133 \begin{itemize} |
|
134 \item Call the function \texttt{greet} |
|
135 \begin{verbatim} |
|
136 In []: greet() |
|
137 Hello World! |
|
138 \end{verbatim} |
|
139 |
|
140 \end{itemize} |
|
141 \end{frame} |
|
142 \begin{frame} |
|
143 \frametitle{Exercise 2} |
|
144 \label{sec-8} |
|
145 |
|
146 |
|
147 Write a python function named \texttt{avg} which computes the average of |
|
148 \texttt{a} and \texttt{b}. |
|
149 |
|
150 \begin{itemize} |
|
151 \item Pause here and try to solve the problem yourself. |
|
152 \end{itemize} |
|
153 \end{frame} |
|
154 \begin{frame}[fragile] |
|
155 \frametitle{Solution 2} |
|
156 \label{sec-9} |
|
157 |
|
158 \begin{verbatim} |
|
159 def avg(a,b): |
|
160 return (a + b)/2 |
|
161 \end{verbatim} |
|
162 |
|
163 \begin{itemize} |
|
164 \item \texttt{a} and \texttt{b} are parameters |
|
165 \item \texttt{def f(p1, p2, p3, ... , pn)} |
|
166 \end{itemize} |
|
167 \end{frame} |
|
168 \begin{frame}[fragile] |
|
169 \frametitle{Docstring} |
|
170 \label{sec-10} |
|
171 |
|
172 |
|
173 \begin{itemize} |
|
174 \item Documenting/commenting code is a good practice |
|
175 \begin{verbatim} |
|
176 def avg(a,b): |
|
177 """ avg takes two numbers as input |
|
178 (a & b), and returns the average |
|
179 of a and b""" |
|
180 return (a+b)/2 |
|
181 \end{verbatim} |
|
182 \item Docstring |
|
183 |
|
184 \begin{itemize} |
|
185 \item written in the line after the \texttt{def} line. |
|
186 \item Inside triple quote. |
|
187 \end{itemize} |
|
188 |
|
189 \item Documentation |
|
190 \begin{verbatim} |
|
191 avg? |
|
192 \end{verbatim} |
|
193 |
|
194 \end{itemize} |
|
195 \end{frame} |
|
196 \begin{frame} |
|
197 \frametitle{Exercise 3} |
|
198 \label{sec-11} |
|
199 |
|
200 Add docstring to the function f. |
|
201 \end{frame} |
|
202 \begin{frame}[fragile] |
|
203 \frametitle{Solution 3} |
|
204 \label{sec-12} |
|
205 |
|
206 |
|
207 \begin{verbatim} |
|
208 def f(x): |
|
209 """Accepts a number x as argument and, |
|
210 returns the square of the number x.""" |
|
211 return x*x |
|
212 \end{verbatim} |
|
213 \end{frame} |
|
214 \begin{frame} |
|
215 \frametitle{Exercise 4} |
|
216 \label{sec-13} |
|
217 |
|
218 Write a python function named \texttt{circle} which returns the area and |
|
219 perimeter of a circle given radius \texttt{r}. |
|
220 \end{frame} |
|
221 \begin{frame}[fragile] |
|
222 \frametitle{Solution 4} |
|
223 \label{sec-14} |
|
224 |
|
225 \begin{verbatim} |
|
226 def circle(r): |
|
227 """returns area and perimeter of a circle given |
|
228 radius r""" |
|
229 pi = 3.14 |
|
230 area = pi * r * r |
|
231 perimeter = 2 * pi * r |
|
232 return area, perimeter |
|
233 \end{verbatim} |
|
234 \end{frame} |
|
235 \begin{frame}[fragile] |
|
236 \frametitle{\texttt{what}} |
|
237 \label{sec-15} |
|
238 |
|
239 \begin{verbatim} |
|
240 |
|
241 def what( n ): |
|
242 if n < 0: n = -n |
|
243 while n > 0: |
|
244 if n % 2 == 1: |
|
245 return False |
|
246 n /= 10 |
|
247 return True |
|
248 \end{verbatim} |
|
249 \end{frame} |
|
250 \begin{frame}[fragile] |
|
251 \frametitle{\texttt{even\_digits}} |
|
252 \label{sec-16} |
|
253 |
|
254 \begin{verbatim} |
|
255 def even_digits( n ): |
|
256 """returns True if all the digits of number |
|
257 n is even returns False if all the digits |
|
258 of number n is not even""" |
|
259 if n < 0: n = -n |
|
260 while n > 0: |
|
261 if n % 2 == 1: |
|
262 return False |
|
263 n /= 10 |
|
264 return True |
|
265 \end{verbatim} |
|
266 \end{frame} |
|
267 \begin{frame}[fragile] |
|
268 \frametitle{\texttt{what}} |
|
269 \label{sec-17} |
|
270 |
|
271 \begin{verbatim} |
|
272 def what( n ): |
|
273 i = 1 |
|
274 while i * i < n: |
|
275 i += 1 |
|
276 return i * i == n, i |
|
277 \end{verbatim} |
|
278 \end{frame} |
|
279 \begin{frame}[fragile] |
|
280 \frametitle{\texttt{is\_perfect\_square}} |
|
281 \label{sec-18} |
|
282 |
|
283 \begin{verbatim} |
|
284 def is_perfect_square( n ): |
|
285 """returns True and square root of n, |
|
286 if n is a perfect square, otherwise |
|
287 returns False and the square root |
|
288 of the next perfect square""" |
|
289 i = 1 |
|
290 while i * i < n: |
|
291 i += 1 |
|
292 return i * i == n, i |
|
293 \end{verbatim} |
|
294 \end{frame} |
|
295 \begin{frame} |
|
296 \frametitle{Summary} |
|
297 \label{sec-19} |
|
298 |
|
299 \begin{itemize} |
|
300 \item Functions in Python |
|
301 \item Passing parameters to a function |
|
302 \item Returning values from a function |
|
303 \item We also did few code reading exercises. |
|
304 \end{itemize} |
|
305 \end{frame} |
|
306 \begin{frame} |
|
307 \frametitle{Thank you!} |
|
308 \label{sec-20} |
|
309 |
|
310 \begin{block}{} |
|
311 \begin{center} |
|
312 This spoken tutorial has been produced by the |
|
313 \textcolor{blue}{FOSSEE} team, which is funded by the |
|
314 \end{center} |
|
315 \begin{center} |
|
316 \textcolor{blue}{National Mission on Education through \\ |
|
317 Information \& Communication Technology \\ |
|
318 MHRD, Govt. of India}. |
|
319 \end{center} |
|
320 \end{block} |
|
321 \end{frame} |
|
322 |
|
323 \end{document} |