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