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