diff options
author | Puneeth Chaganti | 2010-12-01 16:51:35 +0530 |
---|---|---|
committer | Puneeth Chaganti | 2010-12-01 16:51:35 +0530 |
commit | f3a34dfb4e879f3eb7274704f44546aac4add88f (patch) | |
tree | 1cb0a8cc5dbd5ee2b374350915ed2addfa0fb447 /getting_started_with_for | |
parent | 347866ed0d29db61ee062563b1e1616cfb85588c (diff) | |
download | st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.tar.gz st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.tar.bz2 st-scripts-f3a34dfb4e879f3eb7274704f44546aac4add88f.zip |
Renamed all LOs to match with their names in progress.org.
Diffstat (limited to 'getting_started_with_for')
-rw-r--r-- | getting_started_with_for/quickref.tex | 6 | ||||
-rw-r--r-- | getting_started_with_for/script.rst | 308 | ||||
-rw-r--r-- | getting_started_with_for/slides.org | 146 | ||||
-rw-r--r-- | getting_started_with_for/slides.tex | 285 |
4 files changed, 745 insertions, 0 deletions
diff --git a/getting_started_with_for/quickref.tex b/getting_started_with_for/quickref.tex new file mode 100644 index 0000000..d106d6d --- /dev/null +++ b/getting_started_with_for/quickref.tex @@ -0,0 +1,6 @@ +For loop:\\ +{\ex \lstinline| for i in range(1,11,2):|} +{\ex \lstinline| s = s + i|} + +Range function:\\ +{\ex \lstinline| range([start,]stop[,step])|} diff --git a/getting_started_with_for/script.rst b/getting_started_with_for/script.rst new file mode 100644 index 0000000..69f925b --- /dev/null +++ b/getting_started_with_for/script.rst @@ -0,0 +1,308 @@ +.. Objectives +.. ---------- + +.. At the end of this tutorial, you will be able to + +.. 1. Write blocks of code in python. +.. #. Use for loop. +.. #. Use ``range()`` function. +.. #. Write blocks in python interpreter +.. #. Write blocks in ipython interpreter. + + +.. Prerequisites +.. ------------- + +.. 1. should have ``ipython`` and ``pylab`` installed. +.. #. getting started with ``ipython``. +.. #. getting started with lists. + + +.. Author : Anoop Jacob Thomas <anoop@fossee.in> + Internal Reviewer : Nishanth + Internal Reviewer(2): Amit + External Reviewer : + Language Reviewer : Bhanukiran + Checklist OK? : <10-11-2010, Anand, OK> [2010-10-05] + + +============================= +Getting started with for loop +============================= + +{{{ show welcome slide }}} + +Hello and welcome to the tutorial `Getting started with ``for`` loop`. + +{{{ switch to next slide, outline slide }}} + +In this tutorial we will learn about ``for`` loops in python, and also +learn how to write blocks of code in Python. + +.. #[Nishanth]: Instead of saying basics of indenting code, + say How to define code blocks in Python + +{{{ switch to next slide, about whitespaces }}} + +In Python whitespace is significant, and the blocks are visually +separated. + +.. #[nishanth]: Simply tell how blocks are defined in python. + The details like braces are not used and its + advantages like neat code can be told after completely + explaining the indentation + +.. #[Amit]: Do you want to do that here. May be its better to talk about + this after some initiation into the idea of blocks. + +The best practice is to indent the code using four spaces. + +.. #[Nishanth]: Even this detail may be skipped. Simply say use 4 spaces + for indentation. Do that while typing so that they can + actually see what is being typed. + +As you can see in the slide, ``Block B`` is an inner block and it is +indented using 4 spaces, and after ``Block B`` the next statement in +``Block A`` starts from the same indentation level of other ``Block +A`` statements. + +Now let us move straight into ``for`` loop. + +{{{ switch to next slide, problem statement of exercise 1 }}} + + +Write a for loop which iterates through a list of numbers and find the +square root of each number. +:: + + numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916 + +.. #[nishanth]: making new list with square roots induces extra complication + like appending which has no use case here + +.. #[Nishanth]: The problem focuses more on square root and creation + of list. The problem must be simple and focusing on + nothing more but the indentation and for loop. + May be change the problem to print squares than to + print square roots. + +For the problem, first we need to create a ``list`` of numbers and +then iterate over the list and find the square root of each element in +it. And let us create a script, rather than typing it out in the +interpreter itself. Create a script called list_roots.py and type the +following. + +{{{ open the text editor and paste the following code there }}} +:: + + numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] + for each in numbers: + print "Square root of", each, "is", sqrt(each) + print "This is not in for loop!" + +.. numbers = [1, 12, 3, 4, 21, 17] + for each in numbers: + print each, each * each + +.. #[nishanth]: I don't see a use case to append the sq_root to + square_roots. It is only complicating stuff. + Simply iterate and print. + +{{{ switch to next slide, save and run script }}} + +{{{ save the script }}} + +Now save the script, and run it from your IPython interpreter. I +assume that you have started your IPython interpreter using ``-pylab`` +option. + +Run the script as, +:: + + %run -i list_roots.py + +.. #[Nishanth]: you don't have to use the -i option here + +{{{ run the script }}} + +So that was easy! All what we did was iterate over the list element by +element and then use the element for calculation. Note that here we +used two variables. One the variable ``numbers``, which is a list, +another one ``each``, which is the element of list under consideration +in each cycle of the ``for`` loop. The variable names can be chosen by +you. + +.. #[Nishanth]: The details like we didn't have to find the length + are relevant for people who have programmed in C or + other languages earlier. But for a newbie it is more + of confusing extra info. That part may be skipped. + Simply go ahead and focus on the syntax of for loop. + And how the variable name is used inside the for loop. + If you modify the question to only print, the extra + variable sq_root can also be avoided. let it be more + about "each", "numbers" and "for". no other new names. + +{{{ show the script which was created }}} + +Note that the lines after ``for`` statement, is indented using four +spaces. + +{{{ highlight the line after for statement }}} + +It means that line is part of the for loop. And it is a block of code, +although it is only a single statement in the block. And the fourth +line or the immediate line after the ``for`` block is not indented, + +{{{ highlight the fourth line - the line just after for loop }}} + +it means that it is not part of the ``for`` loop and the lines after +that doesn't fall in the scope of the ``for`` loop. Thus each block is +separated by the indentation level and that marks the importance of +white-spaces in Python. + +{{{ switch to the slide which shows the problem statement of the first +problem to be tried out }}} + +Now a question for you to try, from the given numbers make a list of +perfect squares and a list of those which are not. The numbers are, +:: + + 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, 7056, 576, 2916 + +Pause here and try to solve the problem before proceeding further. + +{{{ switch to next slide, problem statement of second problem in +solved exercise}}} + +Now let us try a simple one, to print the square root of numbers in +the list. And this time let us do it right in the IPython +interpreter. + +{{{ switch to next slide, Indentation in ``ipython`` }}} + +{{{ switch focus to the IPython interpreter }}} + +So let us start with making a list. Type the following +:: + + numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916] + for each in numbers: + +and now you will notice that, as soon as you press the return key +after for statement, the prompt changes to four dots and the cursor is +not right after the four dots but there are four spaces from the +dots. Please note that IPython automatically indents the block. The +four dots tell you that you are inside a block. Now type the rest of +the ``for`` loop, + +{{{ switch to next slide, Indentation in ``ipython`` (cont'd) }}} + +.. #[Nishanth]: Tell that IPython does auto indentation. + +:: + + print "Square root of", each, + print "is", sqrt(each) + +Now we have finished the statements in the block, and still the +interpreter is showing four dots, this means that you are still inside the +block. To exit from the block press the return key or the enter key twice +without entering anything else. It printed the square root of each +number in the list, and that is executed in a ``for`` loop. + +{{{ switch to next slide, Indentation in ``python`` interpreter }}} + +Now, let us find the cube of all the numbers from one to ten. But this +time let us try it in the vanilla version of Python interpreter. + +Start the vanilla version of Python interpreter by issuing the command +``python`` in your terminal. + +{{{ open the python interpreter in the terminal using the command +python to start the vanilla Python interpreter }}} + +{{{ switch to next slide, Indentation in ``python`` interpreter +(cont'd) }}} + +Start with, +:: + + for i in range(1,11): + +and press enter once, and we will see that this time it shows four +dots, but the cursor is close to the dots, so we have to indent the +block. The vanilla version of Python interpreter does not indent the +code automatically. So enter four spaces there and then type the +following +:: + + print i, "cube is", i**3 + +Now when we hit enter, we still see the four dots, to get out of the +block, hit enter once again + +.. #[Nishanth]: Here also the overhead on print can be reduced. + Think of a simple print statement. This statement + will be confusing for a newbie. + We can focus more on indentation in python. + +.. #[nishanth]: Not sure if you must use range here. You can + define a list of numbers and iterate on it. + Then say this list can also be generated using + the range function and hence introduce range. + +{{{ switch to the next slide, ``range()`` function }}} + +Okay! so the main thing that we learned here is how to use Python +interpreter and IPython interpreter to specify blocks. But while we +were generating the multiplication table we used something new, +``range()`` function. ``range()`` is an inbuilt function in Python +which can be used to generate a ``list`` of integers from a starting +number to an ending number. Note that the ending number that you +specify will not be included in the ``list``. + +.. #[Nishanth]: Show some examples of range without the step argument + May be give an exercise with negative numbers as arguments + +{{{ switch to next slide, problem statement of the next problem in +solved exercises }}} + +Now, let us print all the odd numbers from 1 to 50. Pause here and try +to solve the problem yourself. + +Let us do it in our IPython interpreter for ease of use. + +{{{ switch focus to ipython interpreter }}} + +The problem can be solved by just using the ``range()`` function. + +It can be solved as, +:: + + print range(1,51,2) + +This time we passed three parameters to ``range()`` function unlike +the previous case where we passed only two parameters. The first two +parameters are the same in both the cases. The first parameter is the +starting number of the sequence and the second parameter is the end of +the range. Note that the sequence doesn't include the ending +number. The third parameter is for stepping through the sequence. Here +we gave two which means we are skipping every alternate element. + +{{{ switch to next slide, summary slide }}} + +Thus we come to the end of this tutorial. We learned about blocks in +Python, indentation, blocks in IPython, for loop, iterating over a +list and then the ``range()`` function. + +.. #[Amit]: There does seem to too much overhead of details. Should + the first example be done using script is it necessary. + Do add some things in evolutionary manner. Like introducing + range as a list and doing a very very simple for loop.Like + iterating over [1,2,3] .Before getting into a problem. + And club details about problem in one paragraph and syntactic details + in other. + +{{{ switch to next slide, thank you slide }}} + +Thank you! diff --git a/getting_started_with_for/slides.org b/getting_started_with_for/slides.org new file mode 100644 index 0000000..51cb7ab --- /dev/null +++ b/getting_started_with_for/slides.org @@ -0,0 +1,146 @@ +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] +#+BEAMER_FRAME_LEVEL: 1 + +#+BEAMER_HEADER_EXTRA: \usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +#+COLUMNS: %45ITEM %10BEAMER_env(Env) %10BEAMER_envargs(Env Args) %4BEAMER_col(Col) %8BEAMER_extra(Extra) +#+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 + +#+LaTeX_CLASS: beamer +#+LaTeX_CLASS_OPTIONS: [presentation] + +#+LaTeX_HEADER: \usepackage[english]{babel} \usepackage{ae,aecompl} +#+LaTeX_HEADER: \usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} + +#+LaTeX_HEADER: \usepackage{listings} + +#+LaTeX_HEADER:\lstset{language=Python, basicstyle=\ttfamily\bfseries, +#+LaTeX_HEADER: commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +#+LaTeX_HEADER: showstringspaces=false, keywordstyle=\color{blue}\bfseries} + +#+TITLE: Getting started with for +#+AUTHOR: FOSSEE +#+EMAIL: +#+DATE: + +#+DESCRIPTION: +#+KEYWORDS: +#+LANGUAGE: en +#+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 + - ~for~ loop in Python. + - Blocks of code in Python. + - Indentation +* Whitespace in Python + - Whitespace is significant + - blocks are visually separated + - Blocks are indented using 4 spaces + : Block A + : Block A + : Block B + : Block B + : Block A + ~Block B~ is an inner block and is indented using 4 spaces +* Exercise 1 + Write a ~for~ loop which iterates through a list of numbers and find + the square root of each number. + : + The numbers are, + : 1369, 7225, 3364, 7056, 5625, 729, 7056, + : 576, 2916 +* Solution 1 + - Open text editor and type the following code + #+begin_src python + numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, + 576, 2916] + + for each in numbers: + print "Square root of", each, "is", sqrt(each) + + print "This is not in for loop!" + #+end_src +* Save \& run script + - Save the script as ~list_roots.py~ + - Run in ~ipython~ interpreter as, + : In []: %run -i list_roots.py +* Exercise 2 + From the given numbers make a list of perfect squares and a list of those which are not. + : + The numbers are, + : 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, + : 7056, 576, 2916 +* Exercise 3 (indentation in ~ipython~) + Print the square root of numbers in the list. + : + Numbers are, + : 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, + : 7056, 576, 2916 +* Indentation in ~ipython~ + : In []: numbers = [1369, 7225, 3364, 7056, 5625, + : ...: 729, 7056, 576, 2916] + + : In []: for each in numbers: + : ...: + Note the four spaces here + : + : + : + : + : + : +* Indentation in ~ipython~ (cont'd) + : In []: numbers = [1369, 7225, 3364, 7056, 5625, + : ...: 729, 7056, 576, 2916] + : In []: for each in numbers: + : ...: + Note the four spaces here + : + Now type the rest of the code + : ...: print "Square root of", each, + : ...: print "is", sqrt(each) + : ...: + : ...: +* Indentation in ~python~ interpreter + Find out the cube of all the numbers from 1 to 10. + : + /do it in the python interpreter/ +* Indentation in ~python~ interpreter (cont'd) + #+begin_src python + >>> for i in range(1, 11): + ... print i, "cube is", i**3 + ... + #+end_src +* ~range()~ function + - in built function in Python + - generates a list of integers + - /syntax:/ range([start,] stop[, step]) + - /example:/ + - range(1, 20) - /generates integers from 1 to 20/ + - range(20) - /generates integers from 0 to 20/ +* Exercise 4 + Print all the odd numbers from 1 to 50. +* Summary + - blocks in ~python~ + - indentation + - blocks in ~ipython~ interpreter + - ~for~ loop + - iterating over list using ~for~ loop + - ~range()~ function +* Thank you! +#+begin_latex + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +#+end_latex + + diff --git a/getting_started_with_for/slides.tex b/getting_started_with_for/slides.tex new file mode 100644 index 0000000..f6c1eaa --- /dev/null +++ b/getting_started_with_for/slides.tex @@ -0,0 +1,285 @@ +% Created 2010-10-12 Tue 12:55 +\documentclass[presentation]{beamer} +\usepackage[latin1]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{fixltx2e} +\usepackage{graphicx} +\usepackage{longtable} +\usepackage{float} +\usepackage{wrapfig} +\usepackage{soul} +\usepackage{t1enc} +\usepackage{textcomp} +\usepackage{marvosym} +\usepackage{wasysym} +\usepackage{latexsym} +\usepackage{amssymb} +\usepackage{hyperref} +\tolerance=1000 +\usepackage[english]{babel} \usepackage{ae,aecompl} +\usepackage{mathpazo,courier,euler} \usepackage[scaled=.95]{helvet} +\usepackage{listings} +\lstset{language=Python, basicstyle=\ttfamily\bfseries, +commentstyle=\color{red}\itshape, stringstyle=\color{darkgreen}, +showstringspaces=false, keywordstyle=\color{blue}\bfseries} +\providecommand{\alert}[1]{\textbf{#1}} + +\title{Getting started with for} +\author{FOSSEE} +\date{} + +\usetheme{Warsaw}\usecolortheme{default}\useoutertheme{infolines}\setbeamercovered{transparent} +\begin{document} + +\maketitle + + + + + + + + + +\begin{frame} +\frametitle{Outline} +\label{sec-1} + +\begin{itemize} +\item \texttt{for} loop in Python. +\item Blocks of code in Python. + +\begin{itemize} +\item Indentation +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Whitespace in Python} +\label{sec-2} + +\begin{itemize} +\item Whitespace is significant + +\begin{itemize} +\item blocks are visually separated +\end{itemize} + +\item Blocks are indented using 4 spaces +\begin{verbatim} + Block A + Block A + Block B + Block B + Block A +\end{verbatim} + + \texttt{Block B} is an inner block and is indented using 4 spaces +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Exercise 1} +\label{sec-3} + + Write a \texttt{for} loop which iterates through a list of numbers and find + the square root of each number. +\begin{verbatim} + +\end{verbatim} + + The numbers are, +\begin{verbatim} + 1369, 7225, 3364, 7056, 5625, 729, 7056, + 576, 2916 +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Solution 1} +\label{sec-4} + +\begin{itemize} +\item Open text editor and type the following code +\end{itemize} + +\begin{verbatim} +numbers = [1369, 7225, 3364, 7056, 5625, 729, 7056, + 576, 2916] + +for each in numbers: + print "Square root of", each, "is", sqrt(each) + +print "This is not in for loop!" +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Save \& run script} +\label{sec-5} + +\begin{itemize} +\item Save the script as \texttt{list\_roots.py} +\item Run in \texttt{ipython} interpreter as, +\begin{verbatim} + In []: %run -i list_roots.py +\end{verbatim} + +\end{itemize} +\end{frame} +\begin{frame}[fragile] +\frametitle{Exercise 2} +\label{sec-6} + + From the given numbers make a list of perfect squares and a list of those which are not. +\begin{verbatim} + +\end{verbatim} + + The numbers are, +\begin{verbatim} + 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, + 7056, 576, 2916 +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Exercise 3 (indentation in \texttt{ipython})} +\label{sec-7} + + Print the square root of numbers in the list. +\begin{verbatim} + +\end{verbatim} + + Numbers are, +\begin{verbatim} + 7225, 3268, 3364, 2966, 7056, 5625, 729, 5547, + 7056, 576, 2916 +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Indentation in \texttt{ipython}} +\label{sec-8} + +\begin{verbatim} + In []: numbers = [1369, 7225, 3364, 7056, 5625, + ...: 729, 7056, 576, 2916] +\end{verbatim} + + +\begin{verbatim} + In []: for each in numbers: + ...: +\end{verbatim} + + Note the four spaces here +\begin{verbatim} + + + + + + +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Indentation in \texttt{ipython} (cont'd)} +\label{sec-9} + +\begin{verbatim} + In []: numbers = [1369, 7225, 3364, 7056, 5625, + ...: 729, 7056, 576, 2916] + In []: for each in numbers: + ...: +\end{verbatim} + + Note the four spaces here +\begin{verbatim} + +\end{verbatim} + + Now type the rest of the code +\begin{verbatim} + ...: print "Square root of", each, + ...: print "is", sqrt(each) + ...: + ...: +\end{verbatim} +\end{frame} +\begin{frame}[fragile] +\frametitle{Indentation in \texttt{python} interpreter} +\label{sec-10} + + Find out the cube of all the numbers from 1 to 10. +\begin{verbatim} + +\end{verbatim} + + \emph{do it in the python interpreter} +\end{frame} +\begin{frame}[fragile] +\frametitle{Indentation in \texttt{python} interpreter (cont'd)} +\label{sec-11} + +\begin{verbatim} +>>> for i in range(1, 11): +... print i, "cube is", i**3 +... +\end{verbatim} +\end{frame} +\begin{frame} +\frametitle{\texttt{range()} function} +\label{sec-12} + +\begin{itemize} +\item in built function in Python +\item generates a list of integers + +\begin{itemize} +\item \emph{syntax:} range([start,] stop[, step]) +\item \emph{example:} + +\begin{itemize} +\item range(1, 20) - \emph{generates integers from 1 to 20} +\item range(20) - \emph{generates integers from 0 to 20} +\end{itemize} + +\end{itemize} + +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Exercise 4} +\label{sec-13} + + Print all the odd numbers from 1 to 50. +\end{frame} +\begin{frame} +\frametitle{Summary} +\label{sec-14} + +\begin{itemize} +\item blocks in \texttt{python} +\item indentation +\item blocks in \texttt{ipython} interpreter +\item \texttt{for} loop +\item iterating over list using \texttt{for} loop +\item \texttt{range()} function +\end{itemize} +\end{frame} +\begin{frame} +\frametitle{Thank you!} +\label{sec-15} + + \begin{block}{} + \begin{center} + This spoken tutorial has been produced by the + \textcolor{blue}{FOSSEE} team, which is funded by the + \end{center} + \begin{center} + \textcolor{blue}{National Mission on Education through \\ + Information \& Communication Technology \\ + MHRD, Govt. of India}. + \end{center} + \end{block} +\end{frame} + +\end{document} |