loops/slides.org
author Nishanth <nishanth@fossee.in>
Tue, 12 Oct 2010 11:26:09 +0530
changeset 334 4b1e81da1c80
parent 278 461a68dbefb1
permissions -rw-r--r--
Finished using_sage_to_teach
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
278
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
#+LaTeX_CLASS: beamer
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
#+LaTeX_CLASS_OPTIONS: [presentation]
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
#+BEAMER_FRAME_LEVEL: 1
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra)
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@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
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
#+LaTeX_CLASS: beamer
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
#+LaTeX_CLASS_OPTIONS: [presentation]
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
#+LaTeX_HEADER: \usepackage{listings}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries,
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
#+LaTeX_HEADER:  commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen},
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
#+LaTeX_HEADER:  showstringspaces=false, keywordstyle=\color{blue}\bfseries}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
#+TITLE:    Loops
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
#+AUTHOR:    FOSSEE
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
#+EMAIL:     
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
#+DATE:    
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
#+DESCRIPTION: 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
#+KEYWORDS: 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
#+LANGUAGE:  en
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
#+OPTIONS:   H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
#+OPTIONS:   TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
* Outline
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
  - Loop while a condition is true. 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
  - Iterate over a sequence
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
  - Breaking out of loops.
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
  - Skipping iterations.
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
* Question 1
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
  Write a ~while~ loop to print the squares of all the even
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
  numbers below 10. 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
* Solution 1
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
  #+begin_src python
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
    In []: i = 2
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
    
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
    In []:  while i<10:
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
     ....:     print i*i
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
     ....:     i += 2
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
  #+end_src
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
* Question 2
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
  Write a ~for~ loop to print the squares of all the even numbers
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
  below 10.
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
* Solution 2
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
  #+begin_src python    
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
    In []: for n in range(2, 10, 2):
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
     ....:     print n*n
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
  #+end_src
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
* Question 3
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
  Using the ~continue~ keyword modify the ~for~ loop to print the
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
  squares of even numbers below 10, to print the squares of only
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
  multiples of 4. (Do not modify the range function call.)
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
* Solution 3
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
  #+begin_src python    
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
    for n in range(2, 10, 2):
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
        if n%4:
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
            continue      
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
        print n*n
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
  #+end_src
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
* Summary
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
  You should now be able to --
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
  - use the ~for~ loop 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
  - use the ~while~ loop
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
  - Use ~break~, ~continue~ and ~pass~ statements 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
* Thank you!
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
#+begin_latex
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
  \begin{block}{}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
  \begin{center}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    76
  This spoken tutorial has been produced by the
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
  \textcolor{blue}{FOSSEE} team, which is funded by the 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    78
  \end{center}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
  \begin{center}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
    \textcolor{blue}{National Mission on Education through \\
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
      Information \& Communication Technology \\ 
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
      MHRD, Govt. of India}.
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
  \end{center}  
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
  \end{block}
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
#+end_latex
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87