|
1 #+LaTeX_CLASS: beamer |
|
2 #+LaTeX_CLASS_OPTIONS: [presentation] |
|
3 #+BEAMER_FRAME_LEVEL: 1 |
|
4 |
|
5 #+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} |
|
6 #+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) |
|
7 #+PROPERTY: BEAMER_col_ALL 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 :ETC |
|
8 |
|
9 #+LaTeX_CLASS: beamer |
|
10 #+LaTeX_CLASS_OPTIONS: [presentation] |
|
11 |
|
12 #+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl} |
|
13 #+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} |
|
14 |
|
15 #+LaTeX_HEADER: \usepackage{listings} |
|
16 |
|
17 #+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, |
|
18 #+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, |
|
19 #+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} |
|
20 |
|
21 #+TITLE: Getting started with functions |
|
22 #+AUTHOR: FOSSEE |
|
23 #+EMAIL: info@fossee.in |
|
24 #+DATE: |
|
25 |
|
26 #+DESCRIPTION: |
|
27 #+KEYWORDS: |
|
28 #+LANGUAGE: en |
|
29 #+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t |
|
30 #+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc |
|
31 |
|
32 * Outline |
|
33 - Define functions |
|
34 - Pass arguments to functions |
|
35 - Learn about docstrings |
|
36 - Return values from functions |
|
37 |
|
38 * Function |
|
39 - Eliminate code redundancy |
|
40 - Help in code reuse |
|
41 - Subroutine |
|
42 - relatively independent of remaining code |
|
43 |
|
44 * ~f(x)~ a mathematical function |
|
45 |
|
46 $f(x) = x^{2}$ |
|
47 |
|
48 : f(1) -> 1 |
|
49 : f(2) -> 4 |
|
50 |
|
51 * Define ~f(x)~ in Python |
|
52 #+begin_src python |
|
53 def f(x): |
|
54 return x*x |
|
55 #+end_src |
|
56 |
|
57 - ~def~ - keyword |
|
58 - ~f~ - function name |
|
59 - ~x~ - parameter / argument to function ~f~ |
|
60 |
|
61 * Exercise 1 |
|
62 |
|
63 Write a python function named ~cube~ which computes the cube of a given |
|
64 number ~n~. |
|
65 |
|
66 - Pause here and try to solve the problem yourself. |
|
67 |
|
68 * Solution |
|
69 #+begin_src python |
|
70 def cube(n): |
|
71 return n**3 |
|
72 #+end_src |
|
73 |
|
74 * ~greet~ function |
|
75 |
|
76 Function ~greet~ which will print ~Hello World!~. |
|
77 #+begin_src python |
|
78 def greet(): |
|
79 print "Hello World!" |
|
80 #+end_src |
|
81 - Call the function ~greet~ |
|
82 : In []: greet() |
|
83 : Hello World! |
|
84 |
|
85 * Exercise 2 |
|
86 |
|
87 Write a python function named ~avg~ which computes the average of |
|
88 ~a~ and ~b~. |
|
89 |
|
90 - Pause here and try to solve the problem yourself. |
|
91 |
|
92 * Solution 2 |
|
93 #+begin_src python |
|
94 def avg(a,b): |
|
95 return (a + b)/2 |
|
96 #+end_src |
|
97 |
|
98 - ~a~ and ~b~ are parameters |
|
99 - ~def f(p1, p2, p3, ... , pn)~ |
|
100 |
|
101 * Docstring |
|
102 |
|
103 - Documenting/commenting code is a good practice |
|
104 #+begin_src python |
|
105 def avg(a,b): |
|
106 """ avg takes two numbers as input |
|
107 (a & b), and returns the average |
|
108 of a and b""" |
|
109 return (a+b)/2 |
|
110 #+end_src |
|
111 - Docstring |
|
112 - written in the line after the ~def~ line. |
|
113 - Inside triple quote. |
|
114 - Documentation |
|
115 : avg? |
|
116 * Exercise 3 |
|
117 Add docstring to the function f. |
|
118 |
|
119 * Solution 3 |
|
120 |
|
121 #+begin_src python |
|
122 def f(x): |
|
123 """Accepts a number x as argument and, |
|
124 returns the square of the number x.""" |
|
125 return x*x |
|
126 #+end_src |
|
127 |
|
128 * Exercise 4 |
|
129 Write a python function named ~circle~ which returns the area and |
|
130 perimeter of a circle given radius ~r~. |
|
131 |
|
132 * Solution 4 |
|
133 #+begin_src python |
|
134 def circle(r): |
|
135 """returns area and perimeter of a circle given |
|
136 radius r""" |
|
137 pi = 3.14 |
|
138 area = pi * r * r |
|
139 perimeter = 2 * pi * r |
|
140 return area, perimeter |
|
141 #+end_src |
|
142 |
|
143 * ~what~ |
|
144 #+begin_src python |
|
145 |
|
146 def what( n ): |
|
147 if n < 0: n = -n |
|
148 while n > 0: |
|
149 if n % 2 == 1: |
|
150 return False |
|
151 n /= 10 |
|
152 return True |
|
153 #+end_src |
|
154 |
|
155 * ~even_digits~ |
|
156 #+begin_src python |
|
157 def even_digits( n ): |
|
158 """returns True if all the digits of number |
|
159 n is even returns False if all the digits |
|
160 of number n is not even""" |
|
161 if n < 0: n = -n |
|
162 while n > 0: |
|
163 if n % 2 == 1: |
|
164 return False |
|
165 n /= 10 |
|
166 return True |
|
167 #+end_src |
|
168 |
|
169 * ~what~ |
|
170 #+begin_src python |
|
171 def what( n ): |
|
172 i = 1 |
|
173 while i * i < n: |
|
174 i += 1 |
|
175 return i * i == n, i |
|
176 #+end_src |
|
177 |
|
178 * ~is_perfect_square~ |
|
179 #+begin_src python |
|
180 def is_perfect_square( n ): |
|
181 """returns True and square root of n, |
|
182 if n is a perfect square, otherwise |
|
183 returns False and the square root |
|
184 of the next perfect square""" |
|
185 i = 1 |
|
186 while i * i < n: |
|
187 i += 1 |
|
188 return i * i == n, i |
|
189 #+end_src |
|
190 |
|
191 * Summary |
|
192 - Functions in Python |
|
193 - Passing parameters to a function |
|
194 - Returning values from a function |
|
195 |
|
196 - We also did few code reading exercises. |
|
197 |
|
198 * Thank you! |
|
199 #+begin_latex |
|
200 \begin{block}{} |
|
201 \begin{center} |
|
202 This spoken tutorial has been produced by the |
|
203 \textcolor{blue}{FOSSEE} team, which is funded by the |
|
204 \end{center} |
|
205 \begin{center} |
|
206 \textcolor{blue}{National Mission on Education through \\ |
|
207 Information \& Communication Technology \\ |
|
208 MHRD, Govt. of India}. |
|
209 \end{center} |
|
210 \end{block} |
|
211 #+end_latex |