diff options
author | Jovina | 2011-06-27 11:27:39 +0530 |
---|---|---|
committer | Jovina | 2011-06-27 11:27:39 +0530 |
commit | 67effe3ddc032bee22bf34fa39ec1222255aca5a (patch) | |
tree | 4c149dae11b6250329b937398c6b559d520ce14d /loops | |
parent | 5c48ff60aba4f0c201ae80f6ac7840caeddfd2aa (diff) | |
download | st-scripts-67effe3ddc032bee22bf34fa39ec1222255aca5a.tar.gz st-scripts-67effe3ddc032bee22bf34fa39ec1222255aca5a.tar.bz2 st-scripts-67effe3ddc032bee22bf34fa39ec1222255aca5a.zip |
Major changes to script & slides of 'Loops'.
Diffstat (limited to 'loops')
-rw-r--r-- | loops/script.rst | 253 | ||||
-rw-r--r-- | loops/slides.org | 102 | ||||
-rw-r--r-- | loops/slides.tex | 146 |
3 files changed, 334 insertions, 167 deletions
diff --git a/loops/script.rst b/loops/script.rst index 421794f..75ae0ef 100644 --- a/loops/script.rst +++ b/loops/script.rst @@ -25,26 +25,49 @@ Script ------ -{{{ Show the slide containing title }}} +.. L1 -Hello Friends. Welcome to the tutorial on loops in Python. +{{{ Show the first slide containing title, name of the production +team along with the logo of MHRD }}} -{{{ Show the outline slide }}} +.. R1 -In this tutorial, we shall look at ``while`` and ``for`` loops. We -shall then look at how to break out of them, or skip some iterations -in loops. +Hello Friends and Welcome to the tutorial on 'loops' in Python. -.. #[[Anoop: for loop is a pre-requisite and has been already covered, - so i think our emphasize can be on while loops]] +.. L2 -.. #[[punch: I think, we should have both of them. It gives a better -.. context and comparison.] +{{{ Show the objectives slide }}} + +.. R2 + +At the end of this tutorial, you will be able to, + + 1. use the ``for`` loop + #. use the ``while`` loop + #. Use ``break``, ``continue`` and ``pass`` statements to play around + with loops. + +.. L3 + +{{{ Switch to the pre-requisite slide }}} + +.. R3 + +Before beginning this tutorial,we would suggest you to complete the +tutorial on "Getting started with for" and "Conditionals". + +.. R4 + +Let us start our ipython interpreter. + +.. L4 {{{ switch to the ipython terminal }}} +:: -We have an ``ipython`` terminal, that we shall use through out this -tutorial. + ipython + +.. R5 We shall first begin with the ``while`` loop. The ``while`` loop is used for repeated execution as long as a condition is ``True``. @@ -52,13 +75,16 @@ used for repeated execution as long as a condition is ``True``. Let us print the squares of all the odd numbers less than 10, using the ``while`` loop. +.. L5 :: - i = 1 + i = 1 + + while i<10: + print i*i + i += 2 - while i<10: - print i*i - i += 2 +.. R6 This loop prints the squares of the odd numbers below 10. @@ -67,24 +93,36 @@ executes the block of code within the loop, if it is. As with any other block in Python, the code within the ``while`` block is indented to the right by 4 spaces. -{{{ switch to next slide }}} +Pause the video here, try out the following exercise and resume the video. + +.. L6 -Following is an exercise that you must do. +.. L7 -%%1%% Write a ``while`` loop to print the squares of all the even -numbers below 10. +{{{ Show slide with exercise 1 }}} -Please, pause the video here. Do the exercise and then continue. +.. R7 -{{{ switch to next slide after a seconds break}}} + Write a ``while`` loop to print the squares of all the even + numbers below 10. +.. R8 + +Switch to the terminal for solution. + +.. L8 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - i = 2 + i = 2 + + while i<10: + print i*i + i += 2 - while i<10: - print i*i - i += 2 +.. R9 Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the ``for`` loop. As we know, the ``for`` @@ -92,26 +130,44 @@ loop iterates over a list or any other sequential data type. So, we use the ``range`` function to get a list of odd numbers below 10, and then iterate over it and print the required stuff. +.. L9 :: - for n in range(1, 10, 2): - print n*n + for n in range(1, 10, 2): + print n*n + +.. R10 + +We can see that we got the same ouput as before.Note that the lines of code +are less. + +Pause the video here, try out the following exercise and resume the video. -Following is an exercise that you must do. +.. L10 -{{{ switch to next slide }}} +.. L11 -%%2%% Write a ``for`` loop to print the squares of all the even -numbers below 10. +{{{ Show slide with exercise 2 }}} -Please, pause the video here. Do the exercise and then continue. +.. R11 -{{{ switch to next slide after a seconds break }}} + Write a ``for`` loop to print the squares of all the even + numbers below 10. +.. R12 + +Switch to the terminal for solution. + +.. L12 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - for n in range(2, 10, 2): - print n*n + for n in range(2, 10, 2): + print n*n + +.. R13 Let us now look at how to use the keywords, ``pass``, ``break`` and ``continue``. @@ -120,67 +176,128 @@ As we already know, ``pass`` is just a syntactic filler. It is used for the sake of completion of blocks, that do not have any code within them. +.. L13 :: - for n in range(2, 10, 2): - pass + for n in range(2, 10, 2): + pass + +.. R14 ``break`` is used to break out of the innermost loop. The ``while`` loop to print the squares of all the odd numbers below 10, can be modified using the ``break`` statement, as follows + +.. L14 :: - i = 1 + i = 1 - while True: - print i*i - i += 2 - if i<10: - break + while True: + print i*i + i += 2 + if i<10: + break + +.. R15 ``continue`` is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration. -.. #[[Anoop: should add slides for break, continue, pass]] - Say, we wish to print the squares of all the odd numbers below 10, which are not multiples of 3, we would modify the ``for`` loop as -follows. :: +follows. + +.. L15 +:: - for n in range(1, 10, 2): - if n%3 == 0: - continue - print n*n + for n in range(1, 10, 2): + if n%3 == 0: + continue + print n*n + +.. R16 +Pause the video here, try out the following exercise and resume the video. + +.. L16 + +.. L17 -Following is an exercise that you must do. +{{{ Show slide with exercise 3 }}} -{{{ switch to next slide }}} +.. R17 -%%3%%Using the ``continue`` keyword modify the ``for`` loop, with the -``range(2, 10, 2)``, to print the squares of even numbers below 10, to -print the squares of only multiples of 4. +Using the ``continue`` keyword modify the ``for`` loop, with the +``range(2, 10, 2)``, to print the squares of even numbers below 10, +which are multiples of 4. +(Do not modify the range function call.) -Please, pause the video here. Do the exercise and then continue. +.. R18 -{{{ switch to next slide after a seconds break}}} +Switch to the terminal for solution. +.. L18 + +{{{continue from paused state}}} +{{{ Switch to the terminal }}} :: - for n in range(2, 10, 2): - if n%4: - continue - print n*n + for n in range(2, 10, 2): + if n%4: + continue + print n*n + +.. L19 {{{ Show summary slide }}} -That brings us to the end of this tutorial. In this tutorial, we have -learnt about looping structures in Python and the use of the keywords -``pass``, ``break`` and ``continue``. +.. R19 + +This brings us to the end of this tutorial. In this tutorial, we have learnt to, + + 1. Iterate over a sequence using ``for'' and ``while'' loops. + #. Break out of loops using ``break'' statement. + #. Skip iterations using ``continue'' statement. + #. Use the ``pass'' statement in a loop. + +.. L20 + +{{{Show self assessment questions slide}}} + +.. R20 + +Here are some self assessment questions for you to solve + + 1. Given ``range(1,4)``; Write a code to print only the number 1. + + 2. Which statement do you use to skip iterations. + - break + - pass + - continue + +.. L21 + +{{{solution of self assessment questions on slide}}} + +.. R21 + +And the answers, + +1. We can use the break statement in a for loop as, + +:: + for i in range(1, 4): + print i + break + +2. In order to skip iterations,we make use of the ``continue`` statement. + +.. L22 -{{{ Show the "sponsored by FOSSEE" slide }}} +{{{ Show the thankyou slide }}} -This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India +.. R22 -Hope you have enjoyed and found it useful. +Hope you have enjoyed this tutorial and found it useful. Thank you! diff --git a/loops/slides.org b/loops/slides.org index 2003620..15b7f0b 100644 --- a/loops/slides.org +++ b/loops/slides.org @@ -18,7 +18,7 @@ #+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, #+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} -#+TITLE: Loops +#+TITLE: #+AUTHOR: FOSSEE #+EMAIL: #+DATE: @@ -29,57 +29,77 @@ #+OPTIONS: H:3 num:nil toc:nil \n:nil @:t ::t |:t ^:t -:t f:t *:t <:t #+OPTIONS: TeX:t LaTeX:nil skip:nil d:nil todo:nil pri:nil tags:not-in-toc -* Outline - - Loop while a condition is true. - - Iterate over a sequence - - Breaking out of loops. - - Skipping iterations. -* Question 1 +* +#+begin_latex +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Loops} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} +#+end_latex +* Objectives + At the end of this tutorial, you will be able to, + + - use the ``for`` loop + - use the ``while`` loop + - Use ``break``, ``continue`` and ``pass`` statements to play around + with loops. +* Pre-requisite +Spoken tutorial on - +- Getting started with ~for~. +- Conditionals. +* Exercise 1 Write a ~while~ loop to print the squares of all the even numbers below 10. -* Solution 1 - #+begin_src python - In []: i = 2 - - In []: while i<10: - ....: print i*i - ....: i += 2 - #+end_src -* Question 2 +* Exercise 2 Write a ~for~ loop to print the squares of all the even numbers below 10. -* Solution 2 - #+begin_src python - In []: for n in range(2, 10, 2): - ....: print n*n - #+end_src -* Question 3 +* Exercise 3 Using the ~continue~ keyword modify the ~for~ loop to print the - squares of even numbers below 10, to print the squares of only - multiples of 4. (Do not modify the range function call.) -* Solution 3 - #+begin_src python - for n in range(2, 10, 2): - if n%4: - continue - print n*n - #+end_src + squares of even numbers below 10, which are multiples of 4. + (Do not modify the range function call.) * Summary - You should now be able to -- - - use the ~for~ loop - - use the ~while~ loop - - Use ~break~, ~continue~ and ~pass~ statements -* Thank you! + In this tutorial, we have learnt to, + - Iterate over a sequence using ``for'' and ``while'' loops. + - Break out of loops using ``break'' statement. + - Skip iterations using ``continue'' statement. + - Use the ``pass'' statement in a loop. +* Evaluation + 1. Given ``range(1,4)''; Write a code to print only the number 1. + + 2. Which statement do you use to skip iterations. + - break + - pass + - continue +* Solutions +1. +#+begin_src Python +for i in range(1,4): + print i + break +#+end_src + +2. ~continue~ +* #+begin_latex \begin{block}{} \begin{center} - This spoken tutorial has been produced by the - \textcolor{blue}{FOSSEE} team, which is funded by the + \textcolor{blue}{\Large THANK YOU!} \end{center} + \end{block} +\begin{block}{} \begin{center} - \textcolor{blue}{National Mission on Education through \\ - Information \& Communication Technology \\ - MHRD, Govt. of India}. + For more Information, visit our website\\ + \url{http://fossee.in/} \end{center} \end{block} #+end_latex diff --git a/loops/slides.tex b/loops/slides.tex index 1713886..5cbafaa 100644 --- a/loops/slides.tex +++ b/loops/slides.tex @@ -1,6 +1,6 @@ -% Created 2010-10-27 Wed 17:51 +% Created 2011-06-27 Mon 11:03 \documentclass[presentation]{beamer} -\usepackage[latin1]{inputenc} +\usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{fixltx2e} \usepackage{graphicx} @@ -8,7 +8,6 @@ \usepackage{float} \usepackage{wrapfig} \usepackage{soul} -\usepackage{t1enc} \usepackage{textcomp} \usepackage{marvosym} \usepackage{wasysym} @@ -24,14 +23,14 @@ commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, showstringspaces=false, keywordstyle=\color{blue}\bfseries} \providecommand{\alert}[1]{\textbf{#1}} -\title{Loops} +\title{} \author{FOSSEE} \date{} \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} \begin{document} -\maketitle + @@ -42,96 +41,127 @@ showstringspaces=false, keywordstyle=\color{blue}\bfseries} \begin{frame} -\frametitle{Outline} -\label{sec-1} -\begin{itemize} -\item Loop while a condition is true. -\item Iterate over a sequence -\item Breaking out of loops. -\item Skipping iterations. -\end{itemize} +\begin{center} +\vspace{12pt} +\textcolor{blue}{\huge Loops} +\end{center} +\vspace{18pt} +\begin{center} +\vspace{10pt} +\includegraphics[scale=0.95]{../images/fossee-logo.png}\\ +\vspace{5pt} +\scriptsize Developed by FOSSEE Team, IIT-Bombay. \\ +\scriptsize Funded by National Mission on Education through ICT\\ +\scriptsize MHRD,Govt. of India\\ +\includegraphics[scale=0.30]{../images/iitb-logo.png}\\ +\end{center} \end{frame} \begin{frame} -\frametitle{Question 1} +\frametitle{Objectives} \label{sec-2} - Write a \texttt{while} loop to print the squares of all the even - numbers below 10. + At the end of this tutorial, you will be able to, + + +\begin{itemize} +\item use the ``for'' loop. +\item use the ``while'' loop. +\item Use ``break'', ``continue'' and ``pass'' statements to play around + with loops. +\end{itemize} \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 1} +\begin{frame} +\frametitle{Pre-requisite} \label{sec-3} -\begin{verbatim} -In []: i = 2 +Spoken tutorial on - -In []: while i<10: - ....: print i*i - ....: i += 2 -\end{verbatim} +\begin{itemize} +\item Getting started with \verb~for~. +\item Conditionals. +\end{itemize} \end{frame} \begin{frame} -\frametitle{Question 2} +\frametitle{Exercise 1} \label{sec-4} - Write a \texttt{for} loop to print the squares of all the even numbers - below 10. + Write a \verb~while~ loop to print the squares of all the even + numbers below 10. \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 2} +\begin{frame} +\frametitle{Exercise 2} \label{sec-5} -\begin{verbatim} -In []: for n in range(2, 10, 2): - ....: print n*n -\end{verbatim} + Write a \verb~for~ loop to print the squares of all the even numbers + below 10. \end{frame} \begin{frame} -\frametitle{Question 3} +\frametitle{Exercise 3} \label{sec-6} - Using the \texttt{continue} keyword modify the \texttt{for} loop to print the - squares of even numbers below 10, to print the squares of only - multiples of 4. (Do not modify the range function call.) + Using the \verb~continue~ keyword, modify the \verb~for~ loop, with the range ``range(2,10,2)'', to print the + squares of even numbers below 10, which are multiples of 4.\\ + (Do not modify the range function call.) \end{frame} -\begin{frame}[fragile] -\frametitle{Solution 3} +\begin{frame} +\frametitle{Summary} \label{sec-7} -\begin{verbatim} -for n in range(2, 10, 2): - if n%4: - continue - print n*n -\end{verbatim} + In this tutorial, we have learnt to, + +\begin{itemize} +\item Iterate over a sequence using ``for'' and ``while'' loops. +\item Break out of loops using ``break'' statement. +\item Skip iterations using ``continue'' statement. +\item Use the ``pass'' statement in a loop. +\end{itemize} \end{frame} \begin{frame} -\frametitle{Summary} +\frametitle{Evaluation} \label{sec-8} - You should now be able to -- + +\begin{enumerate} +\item Given ``range(1,4)'' \\ Write a code to print only the number 1. +\vspace{12pt} +\item Which statement do you use to skip iterations. \begin{itemize} -\item use the \texttt{for} loop -\item use the \texttt{while} loop -\item Use \texttt{break}, \texttt{continue} and \texttt{pass} statements +\item break +\item pass +\item continue \end{itemize} +\end{enumerate} \end{frame} -\begin{frame} -\frametitle{Thank you!} +\begin{frame}[fragile] +\frametitle{Solutions} \label{sec-9} + +\begin{enumerate} +\item \lstset{language=Python} +\begin{lstlisting} +for i in range(1,4): + print i + break +\end{lstlisting} +\vspace{8pt} +\item \verb~continue~ +\end{enumerate} +\end{frame} +\begin{frame} + \begin{block}{} \begin{center} - This spoken tutorial has been produced by the - \textcolor{blue}{FOSSEE} team, which is funded by the + \textcolor{blue}{\Large THANK YOU!} \end{center} + \end{block} +\begin{block}{} \begin{center} - \textcolor{blue}{National Mission on Education through \\ - Information \& Communication Technology \\ - MHRD, Govt. of India}. + For more Information, visit our website\\ + \url{http://fossee.in/} \end{center} \end{block} \end{frame} -\end{document} +\end{document}
\ No newline at end of file |