diff options
author | Puneeth Chaganti | 2010-10-06 15:16:09 +0530 |
---|---|---|
committer | Puneeth Chaganti | 2010-10-06 15:16:09 +0530 |
commit | 8f629313fdb1ed609b7c18928616c994f58b7576 (patch) | |
tree | f1f09d517284797d303dc56626ee39f77cec83f9 /loops | |
parent | cff8fa89de273338c6ccca0a22217ff3f3858955 (diff) | |
download | st-scripts-8f629313fdb1ed609b7c18928616c994f58b7576.tar.gz st-scripts-8f629313fdb1ed609b7c18928616c994f58b7576.tar.bz2 st-scripts-8f629313fdb1ed609b7c18928616c994f58b7576.zip |
Changed structure of my scripts.
Diffstat (limited to 'loops')
-rw-r--r-- | loops/quickref.tex | 8 | ||||
-rw-r--r-- | loops/script.rst | 124 | ||||
-rw-r--r-- | loops/slides.tex | 106 |
3 files changed, 238 insertions, 0 deletions
diff --git a/loops/quickref.tex b/loops/quickref.tex new file mode 100644 index 0000000..b26d168 --- /dev/null +++ b/loops/quickref.tex @@ -0,0 +1,8 @@ +Creating a linear array:\\ +{\ex \lstinline| x = linspace(0, 2*pi, 50)|} + +Plotting two variables:\\ +{\ex \lstinline| plot(x, sin(x))|} + +Plotting two lists of equal length x, y:\\ +{\ex \lstinline| plot(x, y)|} diff --git a/loops/script.rst b/loops/script.rst new file mode 100644 index 0000000..131dcb8 --- /dev/null +++ b/loops/script.rst @@ -0,0 +1,124 @@ +======== + Script +======== + +{{{ show the welcome slide }}} + +Welcome this tutorial on loops in Python. + +{{{ show the outline slide }}} + +In this tutorial, we shall look at ``while`` and ``for`` loops. We +shall then look at the ``break``, ``continue`` and ``pass`` keywords +and how to use them. + +{{{ switch to the ipython terminal }}} + +We have an ``ipython`` terminal, that we shall use through out this +tutorial. + +We shall first begin with the ``while`` loop. The ``while`` loop is +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. + +:: + + i = 1 + + while i<10: + print i*i + i += 2 + +This loop prints the squares of the odd numbers below 10. + +The ``while`` loop, repeatedly checks if the condition is true and +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. + +E%% %% Pause the video here and write a ``while`` loop to print the +squares of all the even numbers below 10. Then, return to the video. + +:: + + i = 2 + + while i<10: + print i*i + i += 2 + +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`` +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. + +:: + + for n in range(1, 10, 2): + print n*n + +E%% %% Pause the video here and write a ``for`` loop to print the +squares of all the even numbers below 10. Then, return to the video. + +:: + + for n in range(2, 10, 2): + print n*n + +Let us now look at how to use the keywords, ``pass``, ``break`` and +``continue``. + +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. + +:: + + for n in range(2, 10, 2): + pass + +``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 +:: + + i = 1 + + while True: + print i*i + i += 2 + if i<10: + break + +``continue`` is used to skip execution of the rest of the loop on this +iteration and continue to the end of this iteration. + +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. +:: + + for n in range(1, 10, 2): + if n%3 == 0: + continue + print n*n + + +E%% %%Pause the video here and 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.) Then, resume the video. +:: + + for n in range(2, 10, 2): + if n%4: + continue + print n*n + +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``. + +Thank You! diff --git a/loops/slides.tex b/loops/slides.tex new file mode 100644 index 0000000..df1462c --- /dev/null +++ b/loops/slides.tex @@ -0,0 +1,106 @@ +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%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} + +\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{Your Title Here} + +\author[FOSSEE] {FOSSEE} + +\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay} +\date{} + +% DOCUMENT STARTS +\begin{document} + +\begin{frame} + \maketitle +\end{frame} + +\begin{frame}[fragile] + \frametitle{Outline} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +%% All other slides here. %% +%% The same slides will be used in a classroom setting. %% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +\begin{frame}[fragile] + \frametitle{Summary} + \begin{itemize} + \item + \end{itemize} +\end{frame} + +\begin{frame} + \frametitle{Thank you!} + \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} |