getting-started-with-functions/slides.org
changeset 445 0c8ab2b5225b
child 446 2ce824b5adf4
equal deleted inserted replaced
444:1aa91f8d8d59 445:0c8ab2b5225b
       
     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 #   - Manipulating one and multi dimensional arrays
       
    34 #   - Access and change individual elements 
       
    35 #   - Access and change rows and columns 
       
    36 #   - Slicing and striding on arrays to access chunks 
       
    37 #   - Read images into arrays and manipulations
       
    38 # * Sample Arrays
       
    39 #   #+begin_src python
       
    40 #     In []: A = array([12, 23, 34, 45, 56])
       
    41     
       
    42 #     In []: C = array([[11, 12, 13, 14, 15],
       
    43 #                       [21, 22, 23, 24, 25],
       
    44 #                       [31, 32, 33, 34, 35],
       
    45 #                       [41, 42, 43, 44, 45],
       
    46 #                       [51, 52, 53, 54, 55]])
       
    47     
       
    48 #   #+end_src
       
    49 # * Question 1
       
    50 #   Change the last column of ~C~ to zeroes. 
       
    51 # * Solution 1
       
    52 #   #+begin_src python
       
    53 #     In []:  C[:, -1] = 0
       
    54 #   #+end_src
       
    55 # * Question 2
       
    56 #   Change ~A~ to ~[11, 12, 13, 14, 15]~. 
       
    57 # * Solution 2
       
    58 #   #+begin_src python
       
    59 #     In []:  A[:] = [11, 12, 13, 14, 15]
       
    60 #   #+end_src
       
    61 # * squares.png
       
    62 #   #+begin_latex
       
    63 #     \begin{center}
       
    64 #       \includegraphics[scale=0.6]{squares}    
       
    65 #     \end{center}
       
    66 #   #+end_latex
       
    67 # * Question 3
       
    68 #   - obtain ~[22, 23]~ from ~C~. 
       
    69 #   - obtain ~[11, 21, 31, 41]~ from ~C~. 
       
    70 #   - obtain ~[21, 31, 41, 0]~.   
       
    71 # * Solution 3
       
    72 #   #+begin_src python
       
    73 #     In []:  C[1, 1:3]
       
    74 #     In []:  C[0:4, 0]
       
    75 #     In []:  C[1:5, 0]
       
    76 #   #+end_src
       
    77 # * Question 4
       
    78 #   Obtain ~[[23, 24], [33, -34]]~ from ~C~
       
    79 # * Solution 4
       
    80 #   #+begin_src python
       
    81 #     In []:  C[1:3, 2:4]
       
    82 #   #+end_src
       
    83 # * Question 5
       
    84 #   Obtain the square in the center of the image
       
    85 # * Solution 5
       
    86 #   #+begin_src python
       
    87 #     In []: imshow(I[75:225, 75:225])
       
    88 #   #+end_src
       
    89 # * Question 6
       
    90 #   Obtain the following
       
    91 #   #+begin_src python
       
    92 #     [[12, 0], [42, 0]]
       
    93 #     [[12, 13, 14], [0, 0, 0]]
       
    94 #   #+end_src
       
    95 
       
    96 # * Solution 6
       
    97 #   #+begin_src python
       
    98 #     In []: C[::3, 1::3]
       
    99 #     In []: C[::4, 1:4]
       
   100 #   #+end_src
       
   101 # * Summary
       
   102 #   You should now be able to --
       
   103 #   - Manipulate 1D \& Multi dimensional arrays
       
   104 #       - Access and change individual elements 
       
   105 #       - Access and change rows and columns 
       
   106 #       - Slice and stride on arrays
       
   107 #   - Read images into arrays and manipulate them.
       
   108 
       
   109 
       
   110 * Outline
       
   111   - Define functions
       
   112   - Pass arguments to functions
       
   113   - Learn about docstrings
       
   114   - Return values from functions
       
   115 
       
   116 * Function
       
   117   - Eliminate code redundancy
       
   118   - Help in code reuse
       
   119   - Subroutine
       
   120     - relatively independent of remaining code
       
   121 
       
   122 * ~f(x)~ a mathematical function
       
   123 
       
   124   $f(x) = x^{2}$
       
   125 
       
   126   : f(1) -> 1
       
   127   : f(2) -> 4
       
   128 
       
   129 * Define ~f(x)~ in Python
       
   130   #+begin_src python
       
   131     def f(x):
       
   132         return x*x
       
   133   #+end_src
       
   134 
       
   135   - ~def~ - keyword
       
   136   - ~f~ - function name
       
   137   - ~x~ - parameter / argument to function ~f~
       
   138 
       
   139 * Exercise 1
       
   140 
       
   141   Write a python function named ~cube~ which computes the cube of a given
       
   142   number ~n~.
       
   143   
       
   144   - Pause here and try to solve the problem yourself.
       
   145 
       
   146 * Solution
       
   147   #+begin_src python
       
   148     def cube(n):
       
   149     	return n**3
       
   150   #+end_src
       
   151 
       
   152 * ~greet~ function
       
   153 
       
   154  Function ~greet~ which will print ~Hello World!~.
       
   155  #+begin_src python
       
   156     def greet():
       
   157     	print "Hello World!"
       
   158  #+end_src
       
   159   - Call the function ~greet~
       
   160     : In []: greet()
       
   161     : Hello World!
       
   162 
       
   163 * Exercise 2
       
   164 
       
   165   Write a python function named ~avg~ which computes the average of
       
   166   ~a~ and ~b~.
       
   167 
       
   168   - Pause here and try to solve the problem yourself.
       
   169 
       
   170 * Solution 2
       
   171  #+begin_src python
       
   172     def avg(a,b):
       
   173     	return (a + b)/2
       
   174  #+end_src
       
   175 
       
   176  - ~a~ and ~b~ are parameters
       
   177  - ~def f(p1, p2, p3, ... , pn)~
       
   178 
       
   179 * Docstring
       
   180 
       
   181   - Documenting/commenting code is a good practice
       
   182    #+begin_src python
       
   183      def avg(a,b):
       
   184          """ avg takes two numbers as input 
       
   185 	 (a & b), and returns the average 
       
   186 	 of a and b"""
       
   187 	 return (a+b)/2
       
   188    #+end_src
       
   189   - Docstring
       
   190     - written in the line after the ~def~ line.
       
   191     - Inside triple quote.
       
   192   - Documentation
       
   193     : avg?
       
   194 * Exercise 3
       
   195   Add docstring to the function f.
       
   196 
       
   197 * Solution 3
       
   198 
       
   199 #+begin_src python
       
   200   def f(x):
       
   201       """Accepts a number x as argument and,
       
   202       returns the square of the number x."""
       
   203       return x*x
       
   204 #+end_src
       
   205 
       
   206 * Exercise 4
       
   207   Write a python function named ~circle~ which returns the area and
       
   208   perimeter of a circle given radius ~r~.
       
   209 
       
   210 * Solution 4
       
   211 #+begin_src python
       
   212   def circle(r):
       
   213       """returns area and perimeter of a circle given 
       
   214       radius r"""
       
   215       pi = 3.14
       
   216       area = pi * r * r
       
   217       perimeter = 2 * pi * r
       
   218       return area, perimeter
       
   219 #+end_src
       
   220 
       
   221 * ~what~
       
   222 #+begin_src python
       
   223 
       
   224  def what( n ):
       
   225      if n < 0: n = -n
       
   226      while n > 0:
       
   227          if n % 2 == 1:
       
   228              return False
       
   229          n /= 10
       
   230      return True
       
   231 #+end_src
       
   232 
       
   233 * ~even_digits~
       
   234 #+begin_src python
       
   235  def even_digits( n ):
       
   236     """returns True if all the digits of number 
       
   237     n is even returns False if all the digits 
       
   238     of number n is not even"""
       
   239      if n < 0: n = -n
       
   240      while n > 0:
       
   241          if n % 2 == 1:
       
   242              return False
       
   243          n /= 10
       
   244      return True
       
   245 #+end_src
       
   246 
       
   247 * ~what~
       
   248 #+begin_src python
       
   249  def what( n ):
       
   250      i = 1
       
   251      while i * i < n:
       
   252          i += 1
       
   253      return i * i == n, i
       
   254 #+end_src
       
   255 
       
   256 * ~is_perfect_square~
       
   257 #+begin_src python
       
   258  def is_perfect_square( n ):
       
   259      """returns True and square root of n, 
       
   260      if n is a perfect square, otherwise 
       
   261      returns False and the square root 
       
   262      of the next perfect square"""
       
   263      i = 1
       
   264      while i * i < n:
       
   265          i += 1
       
   266      return i * i == n, i
       
   267 #+end_src
       
   268 
       
   269 * Summary
       
   270  - Functions in Python
       
   271  - Passing parameters to a function
       
   272  - Returning values from a function
       
   273 
       
   274  - We also did few code reading exercises.
       
   275 
       
   276 * Thank you!
       
   277 #+begin_latex
       
   278   \begin{block}{}
       
   279   \begin{center}
       
   280   This spoken tutorial has been produced by the
       
   281   \textcolor{blue}{FOSSEE} team, which is funded by the 
       
   282   \end{center}
       
   283   \begin{center}
       
   284     \textcolor{blue}{National Mission on Education through \\
       
   285       Information \& Communication Technology \\ 
       
   286       MHRD, Govt. of India}.
       
   287   \end{center}  
       
   288   \end{block}
       
   289 #+end_latex