Added loops presentation and final script for loops.
authorShantanu <shantanu@fossee.in>
Mon, 26 Apr 2010 18:12:39 +0530
changeset 112 7de6be45182e
parent 111 4ebff217844e
child 113 6388eacf7502
Added loops presentation and final script for loops.
cond-loops.org
presentations/loops.tex
--- a/cond-loops.org	Mon Apr 26 18:15:00 2010 +0530
+++ b/cond-loops.org	Mon Apr 26 18:12:39 2010 +0530
@@ -12,46 +12,65 @@
     In this tutorial we shall be covering conditional statements and
     control loops. We have used them already in some of our previous
     sessions with brief introduction. We shall be covering 'if-else' 
-    statement, 'while' and 'for' loops.
-    For understanding of conditional statement we will write a python
-    script. Open Scite text editor by going to Applications->
+    statement, 'while' and 'for' statements formally.
+
+
+    For understanding of if-else statement we will write a python
+    script that takes a number as input from user and prints 0 if it is zero
+    and prints "Be positive" if it is negative, prints "Single" if the input is 1
+    and if the number is not 0 or 1 or negative, it prints "More".
+ 
+    To write the program, open Scite text editor by going to Applications->
     Programming->Scite:
+
     First we prompt user for entering a integer by using raw_input
-    x = int(raw_input("Enter an integer: "))
-    we convert the input string to 'int'
+    str_x = raw_input("Enter an integer: ")
+    since we know raw_input gives string, we convert the input string to an integer
+    by typing 
+    x = int(str_x)
+
+    now we check if the number is less than zero.
+    type 
     if x < 0:
-    We check if number is less then zero
-        if condition is true we print 
-        print 'Be positive!'	
-	note the indentation
+        Please not 
+        #if number is negative we have to print "Be positive"
+        #so we give four spaces for indentation and type
+        print 'Be positive!'
+
     elif x == 0:
-        This is else-if condition and corresponding message
-	end of previous indentation indicates ending of a block
+        to check if the number is equal to zero
+        #This is else-if condition
         print 'Zero'
     elif x == 1:
         print 'Single'
+    then we type the else statement which gets executed when all the if and elif statements fail
+    so type
     else:
-        This is else block which is executed when all if, and else
-	if statements fails.
         print 'More'
-    Save this script in home folder with name '.py'	
-    To run this script inside IPython we first start interpreter and 
-    type 
-    %run '.py'
+
+    Save this script by going to file menu and clicking on save.
+    save it in home folder with name 'ladder.py'	
+
+    let us check the program on ipython interpreter for various inputs
+    open ipython terminal and type
+    %run ladder.py
     It will prompt us to enter a integer and based on our input it 
     prints appropriate message. 
 
-    Python supports only if-elif-else conditional constructs, 
-    switch-case statements are not available/supported in Python.
     We can use binary operators like and/or/not to check for multiple
     conditions.
 
+    Python supports only if-elif-else conditional constructs, 
+    switch-case statements are not available/supported in Python.
+
     Now lets look at loop constructs available. Python supports 'while'
-    and 'for' statements. We will write a script to understand 'while'
-    statement. In Scite click on 'new' file shortcut to open a new tab
-    We shall write a script for printing all fabonacci numbers less then
-    10. In this series Sum of previous two elements defines the next 
-    element.
+    and 'for' statements. 
+
+    To understand the while we shall write a script that prints all the fibonacci 
+    numbers less than 10. In fibonacci series the sum of previous two elements
+    is equal to the next element.
+
+    In Scite go to file menu and click on new and it opens a new tab.
     
     First we initialize two variable to first and second number of 
     series
@@ -71,23 +90,31 @@
 
     'for' in python works any kind of iterable objects. In our 
     previous sessions we used 'for' to iterate through files and lists.
-    So in case we want to get square of say first five numbers using 
-    'for' loop, we will have to create a list. For this we will use 
-    'range' function available. Lets take a look at documentation 
-    available for 'range' function by typing
+
+    So we are going to use for loop print the squares of first five whole numbers.
+
+    To generate squares, we have to iterate on list of numbers and we are 
+    going to use the range function to get the list of numbers.
+
+    let us look at the documentation of range function by typing
     range?
-    It takes three arguments, first being the start/initial value
-    second one being stop/last value and third being the step size. 
-    Out of these 'start' and 'step' arguments are optional.
-    So if we use range to get first five number it would be 
+    we see that it takes three arguments, first is the start/initial value
+    second one is the stop/last value and third is the step size. 
+
+    Out of these, 'start' and 'step' arguments are optional.
+
+    So to get list of first five natural numbers, we use 
     range(5)
-    Note here that last/stop value is not included in resulting 
-    list. So to get square of first five number all we have to do is
+
+    Note here that last/stop value is not included in resulting list.
+
+    So to get square of first five number all we have to do is
     iterate over this list.
     for i in range(5):
     ....print i, i*i
     ....
     ....
+
     Similarly to get square of all odd numbers from 3 to 9 we can do 
     something like
 
@@ -98,10 +125,19 @@
     ....print i, i*i
     ....
     ....
-        
-    That brings us to the end of this tutorial. We have covered more
-    details on conditional statements in Python. How to write loops
-    using 'while' loops. Range function and using for loop with range
+ 
+    since the for statement in python works on any iterable we can also iterate through strings
+
+    to print each character in a string we do
+    for c in "Guido Van Rossum":
+        print c
+
+    we see that it prints all the characters one by one
+ 
+    That brings us to the end of this tutorial. We have learnt
+    conditional statements in Python. How to write loops
+    using 'while' statement. Range function and using for loop
+    
 
     Thank you!
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/presentations/loops.tex	Mon Apr 26 18:12:39 2010 +0530
@@ -0,0 +1,139 @@
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%Tutorial slides on Python.
+%
+% Author: FOSSEE 
+% Copyright (c) 2009, FOSSEE, IIT Bombay
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+\documentclass[14pt,compress]{beamer}
+%\documentclass[draft]{beamer}
+%\documentclass[compress,handout]{beamer}
+%\usepackage{pgfpages} 
+%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
+
+% Modified from: generic-ornate-15min-45min.de.tex
+\mode<presentation>
+{
+  \usetheme{Warsaw}
+  \useoutertheme{infolines}
+  \setbeamercovered{transparent}
+}
+
+\usepackage[english]{babel}
+\usepackage[latin1]{inputenc}
+%\usepackage{times}
+\usepackage[T1]{fontenc}
+
+% Taken from Fernando's slides.
+\usepackage{ae,aecompl}
+\usepackage{mathpazo,courier,euler}
+\usepackage[scaled=.95]{helvet}
+
+\definecolor{darkgreen}{rgb}{0,0.5,0}
+
+\usepackage{listings}
+\lstset{language=Python,
+    basicstyle=\ttfamily\bfseries,
+    commentstyle=\color{red}\itshape,
+  stringstyle=\color{darkgreen},
+  showstringspaces=false,
+  keywordstyle=\color{blue}\bfseries}
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+% Macros
+\setbeamercolor{emphbar}{bg=blue!20, fg=black}
+\newcommand{\emphbar}[1]
+{\begin{beamercolorbox}[rounded=true]{emphbar} 
+      {#1}
+ \end{beamercolorbox}
+}
+\newcounter{time}
+\setcounter{time}{0}
+\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
+
+\newcommand{\typ}[1]{\lstinline{#1}}
+
+\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}}  }
+
+% Title page
+\title{Python for Scientific Computing : Conditional flow and Loops}
+
+\author[FOSSEE] {FOSSEE}
+
+\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
+\date{}
+
+% DOCUMENT STARTS
+\begin{document}
+
+\begin{frame}
+  \maketitle
+\end{frame}
+
+\begin{frame}
+  \frametitle{About the Session}
+  \begin{block}{Goal}
+    We shall be covering:
+    \begin{itemize}
+    \item Conditional statements: if-else
+    \item Control flow: Loops
+      \begin{itemize}
+      \item while
+      \item for
+      \end{itemize}
+    \end{itemize}
+  \end{block}
+\end{frame}
+
+\begin{frame}
+  \frametitle{Python Script}
+  \begin{block}{Problem statement}    
+    \begin{itemize}
+    \item Prompt user for input
+    \item Based on input prints:
+      \begin{itemize}
+      \item 'Be positive' if number is negative
+      \item 'Zero' if number is zero
+      \item 'Single' if number is one
+      \item 'More' if number is greater than one
+      \end{itemize}
+    \end{itemize}
+  \end{block}
+\end{frame}
+
+\begin{frame}[fragile]
+  \frametitle{\typ{while}}
+\begin{block}{Example: Fibonacci series}
+  Sum of previous two elements defines the next
+\end{block}
+\begin{block}{Problem statement}
+  Print all Fibonacci numbers less than 10.
+\end{block}
+\begin{block}{Expected output}
+\typ{1 1 2 3 5 8}\\  
+\end{block}
+\end{frame}
+
+\begin{frame}[fragile]
+  \frametitle{Summary}
+  \begin{block}{}
+    \begin{itemize}
+    \item Writing conditional statements
+    \item While loops
+    \item range function
+    \item for and range
+    \end{itemize}
+  \end{block}    
+\end{frame}
+
+\begin{frame}
+  \frametitle{Thank you!}  
+  \begin{block}{}
+  This session is part of \textcolor{blue}{FOSSEE} project funded by:
+  \begin{center}
+    \textcolor{blue}{NME through ICT from MHRD, Govt. of India}.
+  \end{center}  
+  \end{block}
+\end{frame}
+
+\end{document}