Manipulating lists LO - script and questions.
authorPuneeth Chaganti <punchagan@fossee.in>
Wed, 13 Oct 2010 11:12:18 +0530
changeset 312 8cb703eee88d
parent 311 3f942b8d3f2f
child 313 b9b7bfce773e
Manipulating lists LO - script and questions.
manipulating-lists.rst
manipulating-lists/questions.rst
manipulating-lists/quickref.tex
manipulating-lists/script.rst
manipulating-lists/slides.org
manipulating-lists/slides.tex
--- a/manipulating-lists.rst	Wed Oct 13 11:11:40 2010 +0530
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,180 +0,0 @@
-Hello friends. Welcome to this spoken tutorial on Getting started with
-strings.
-
-{{{ Show the slide containing the title }}}
-
-{{{ Show the slide containing the outline }}}
-
-We have already learnt a lot about Lists in Python. In this tutorial,
-we will learn more about advanced features of Lists in Python. We will
-see in detail how to concatenate two lists, slicing and striding of
-lists, methods to sort and reverse the list.
-
-{{{ Shift to terminal and start ipython }}}
-
-To begin with let us start ipython, by typing::
-
-  ipython
-
-on the terminal
-
-We already know what Lists are in Python, how to access individual
-elements in the list and some of the functions that can be run on the
-lists like max, min, sum len and so on. Now let us learn some of the
-basic operations that can be performed on Lists.
-
-We already know how to access individual elements in a List. But what
-if we have a scenario where we need to get a part of the entire list
-or what we call as a slice of the list? Python supports slicing on
-lists. Let us say I have the list::
-
-  primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
-
-To obtain the all the primes between 10 and 20 from the above list of
-primes we say::
-
-  primes[4:8]
-
-This gives us all the elements in the list starting from the element
-with the index 4 which is 11 in our list upto the element with index 8
-in the list but not including the eigth element. So we obtain a slice
-starting from 11 upto 19th. It is a very important to remember that
-when ever we specify a range of elements in Python the start index is
-included and end index is not included. So in the above case, 11 which
-was the element with the index 4 was included but 23 which was the
-element with index 8 was exluded.
-
-Generalizing, we can obtain a slice of the list "p" from the index
-"start" upto the index "end" but excluding "end" with the following
-syntax
-
-{{{ Show the slide containing p[start:stop] }}}
-
-By default the slice fetches all the elements between start and stop
-including start but not stop. So as to say we obtain all the elements
-between start and stop in steps of one. Python also provides us the
-functionality to specify the steps in which the slice must be
-obtained. Say we have::
-
-  num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
-
-If we want to obtain all the odd numbers less than 10 from the list
-"num" we have to start from element with index 1 upto the index 10 in
-steps of 2::
-
-  num[1:10:2]
-
-So if we don't specify the step it is by default 1. Similary there are
-default values for start and stop indices as well. If we don't specify
-the start index it is implicitly taken as the first element of the
-list::
-
-  num[:10]
-
-This gives us all the elements from the beginning upto the 10th
-element but not including the 10th element in the list "num". Similary
-if the stop index is not specified it is implicitly assumed to be the
-end of the list, including the last element of the list::
-
-  num[10:]
-
-gives all the elements starting from the 10th element in the list
-"num" upto the final element including that last element. Now::
-
-  num[::2]
-
-gives us all the even numbers in the list "num".
-
-The other basic operation that we can perform on list is concatenation
-of two or more lists. We can combine two lists by using the "plus"
-operator. Say we have
-
-{{{ Read as you type }}}::
-
-  a = [1, 2, 3, 4]
-  b = [4, 5, 6, 7]
-  a + b
-
-When we concatenate lists using the "plus" operator we get a new
-list. We can store this list in a new variable::
-
-  c = a + b
-  c
-
-It is important to observe that the "plus" operator always returns a
-new list without touching anything in the existing lists which are the
-operands of the concatenation operation.
-
-We know that list is a collection of data. Whenever we have a
-collection we run into situations where we want to start the
-collection. Lists support sort method which sorts the list inplace::
-
-  a = [5, 1, 6, 7, 7, 10]
-  a.sort()
-
-Now the contents of the list "a" will be::
-
-  a
-  [1, 5, 6, 7, 7, 10]
-
-Since the sort method sorts the list inplace the original list we had
-is overwritten or replaced. We have no way to obtain the original list
-back. One way to avoid this is to keep a copy of the original list in
-another variable and run the sort method on the list. However Python
-also provides a built-in function called sorted which sorts the list
-which is passed as an argument to it and returns a new sorted list::
-
-  a = [5, 1, 6, 7, 7, 10]
-  sorted(a)
-  
-We can store this sorted list another list variable::
-
-  sa = sorted(a)
-
-Similarly to perform certain operations on the list we would like to
-reverse the list. Python provides reverse method which again reverses
-the list inplace::
-
-  a = [1, 2, 3, 4, 5]
-  a.reverse()
-
-reverses the list "a" and stores the reversed list inplace i.e. in "a"
-itself. Lets see the list "a"::
-
-  a
-  [5, 4, 3, 2, 1]
-
-But again the original list is lost. If we want to obtain the reverse
-of a list keeping the original list intact we can use the Python
-built-in function reversed. reversed function returns a new list which
-is the reverse of the list which was passed as the argument to the
-reversed function::
-
-  a = [1, 2, 3, 4, 5]
-  reversed(a)
-
-We can also store this new reversed list in another list variable.
-
-{{{ Show summary slide }}}
-
-This brings us to the end of another session. In this tutorial session
-we learnt
-
-  * How to define strings
-  * Different types of defining a string
-  * String concatenation and repeatition
-  * Accessing individual elements of the string
-  * Immutability of strings
-
-{{{ Show the "sponsored by FOSSEE" slide }}}
-
-This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
-
-Hope you have enjoyed and found it useful.
-Thankyou
- 
-.. Author              : Madhu
-   Internal Reviewer 1 :         [potential reviewer: Nishanth]
-   Internal Reviewer 2 :         [potential reviewer: Amit]
-   External Reviewer   :
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/manipulating-lists/questions.rst	Wed Oct 13 11:12:18 2010 +0530
@@ -0,0 +1,72 @@
+Objective Questions
+-------------------
+
+.. A mininum of 8 questions here (along with answers)
+
+1. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
+   29]``, How do you obtain the last 4 primes?
+
+   Answer: primes[-4:]
+
+#. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
+   29]``, What is the output of ``primes[::5]``?
+
+   Answer: ``[2, 13]``
+   
+#. Given a list, p, of unknown length, obtain the first 3 (or all, if
+   there are fewer) characters of it. 
+
+   Answer: p[:3]
+
+#. The method ``reverse`` reverses a list in-place. True or False?
+
+   Answer: True
+   
+#. ``reversed`` function reverses a list in-place. True or False?
+
+   Answer: False
+
+#. Given the list ``p = [1, 2, 3]``. p[4] produces an IndexError. True
+   or False?
+
+   Answer: True
+
+#. Given the list ``p = [1, 2, 3]``. p[:4] produces an IndexError. True
+   or False?
+
+   Answer: False
+
+#. Given the list primes, ``primes = [2, 3, 5, 7, 11]``, What is the
+   output of ``primes[::-1]``?
+
+   Answer: [11, 7, 5, 3, 2]
+
+#. Given the list primes, ``primes = [2, 3, 5, 7, 11]``, What is the
+   output of ``primes[::-3]``?
+
+   Answer: [11, 3]
+
+
+Larger Questions
+----------------
+
+.. A minimum of 2 questions here (along with answers)
+
+#. Given a list p. Append it's reverse to itself. 
+
+   Answer::
+
+      p = p + reversed(p)
+
+
+#. Marks is a list containing the roll numbers of students followed by
+   marks. [This is not a recommended way to hold the marks details,
+   but the teacher knows only so much Python!] Now she wants to get
+   the average of the marks. Help her do it. 
+
+   Answer::
+
+     marks = [1, 9, 2, 8, 3, 3, 4, 10, 5, 2]
+     average = sum(marks[1::2])/len(marks[1::2])
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/manipulating-lists/quickref.tex	Wed Oct 13 11:12:18 2010 +0530
@@ -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)|}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/manipulating-lists/script.rst	Wed Oct 13 11:12:18 2010 +0530
@@ -0,0 +1,196 @@
+.. Objectives
+.. ----------
+
+.. Clearly state the objectives of the LO (along with RBT level)
+
+.. Prerequisites
+.. -------------
+
+..   1. getting started with lists
+..   2. 
+..   3. 
+     
+.. Author              : Madhu
+   Internal Reviewer   : 
+   External Reviewer   :
+   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
+
+Script
+------
+
+{{{ Show the slide containing the title }}}
+
+Hello friends. Welcome to this spoken tutorial on Manipulating Lists. 
+
+
+{{{ Show the slide containing the outline }}}
+
+We have already learnt a lot about Lists in Python. In this tutorial,
+we will learn more about advanced features of Lists in Python. We will
+see in detail how to concatenate two lists, slicing and striding of
+lists, methods to sort and reverse the list.
+
+{{{ Shift to terminal and start ipython }}}
+
+To begin with let us start ipython, by typing::
+
+  ipython
+
+on the terminal
+
+We already know what Lists are in Python, how to access individual
+elements in the list and some of the functions that can be run on the
+lists like max, min, sum len and so on. Now let us learn some of the
+basic operations that can be performed on Lists.
+
+We already know how to access individual elements in a List. But what
+if we have a scenario where we need to get a part of the entire list
+or what we call as a slice of the list? Python supports slicing on
+lists. Let us say I have the list::
+
+  primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
+
+To obtain the all the primes between 10 and 20 from the above list of
+primes we say::
+
+  primes[4:8]
+
+This gives us all the elements in the list starting from the element
+with the index 4 which is 11 in our list upto the element with index 8
+in the list but not including the eigth element. So we obtain a slice
+starting from 11 upto 19th. It is a very important to remember that
+when ever we specify a range of elements in Python the start index is
+included and end index is not included. So in the above case, 11 which
+was the element with the index 4 was included but 23 which was the
+element with index 8 was excluded.
+
+Generalizing, we can obtain a slice of the list "p" from the index
+"start" upto the index "end" but excluding "end" with the following
+syntax
+
+{{{ Show the slide containing p[start:stop] }}}
+
+By default the slice fetches all the elements between start and stop
+including start but not stop. So as to say we obtain all the elements
+between start and stop in steps of one. Python also provides us the
+functionality to specify the steps in which the slice must be
+obtained. Say we have::
+
+  num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
+
+If we want to obtain all the odd numbers less than 10 from the list
+"num" we have to start from element with index 1 upto the index 10 in
+steps of 2::
+
+  num[1:10:2]
+
+So if we don't specify the step it is by default 1. Similary there are
+default values for start and stop indices as well. If we don't specify
+the start index it is implicitly taken as the first element of the
+list::
+
+  num[:10]
+
+This gives us all the elements from the beginning upto the 10th
+element but not including the 10th element in the list "num". Similary
+if the stop index is not specified it is implicitly assumed to be the
+end of the list, including the last element of the list::
+
+  num[10:]
+
+gives all the elements starting from the 10th element in the list
+"num" upto the final element including that last element. Now::
+
+  num[::2]
+
+gives us all the even numbers in the list "num".
+
+The other basic operation that we can perform on list is concatenation
+of two or more lists. We can combine two lists by using the "plus"
+operator. Say we have
+
+{{{ Read as you type }}}::
+
+  a = [1, 2, 3, 4]
+  b = [4, 5, 6, 7]
+  a + b
+
+When we concatenate lists using the "plus" operator we get a new
+list. We can store this list in a new variable::
+
+  c = a + b
+  c
+
+It is important to observe that the "plus" operator always returns a
+new list without touching anything in the existing lists which are the
+operands of the concatenation operation.
+
+We know that list is a collection of data. Whenever we have a
+collection we run into situations where we want to start the
+collection. Lists support sort method which sorts the list inplace::
+
+  a = [5, 1, 6, 7, 7, 10]
+  a.sort()
+
+Now the contents of the list "a" will be::
+
+  a
+  [1, 5, 6, 7, 7, 10]
+
+Since the sort method sorts the list inplace the original list we had
+is overwritten or replaced. We have no way to obtain the original list
+back. One way to avoid this is to keep a copy of the original list in
+another variable and run the sort method on the list. However Python
+also provides a built-in function called sorted which sorts the list
+which is passed as an argument to it and returns a new sorted list::
+
+  a = [5, 1, 6, 7, 7, 10]
+  sorted(a)
+  
+We can store this sorted list another list variable::
+
+  sa = sorted(a)
+
+Similarly to perform certain operations on the list we would like to
+reverse the list. Python provides reverse method which again reverses
+the list inplace::
+
+  a = [1, 2, 3, 4, 5]
+  a.reverse()
+
+reverses the list "a" and stores the reversed list inplace i.e. in "a"
+itself. Lets see the list "a"::
+
+  a
+  [5, 4, 3, 2, 1]
+
+But again the original list is lost. If we want to obtain the reverse
+of a list keeping the original list intact we can use the Python
+built-in function reversed. reversed function returns a new list which
+is the reverse of the list which was passed as the argument to the
+reversed function::
+
+  a = [1, 2, 3, 4, 5]
+  reversed(a)
+
+We can also store this new reversed list in another list variable.
+
+{{{ Show summary slide }}}
+
+This brings us to the end of another session. In this tutorial session
+we learnt
+
+  * How to define strings
+  * Different types of defining a string
+  * String concatenation and repeatition
+  * Accessing individual elements of the string
+  * Immutability of strings
+
+{{{ Show the "sponsored by FOSSEE" slide }}}
+
+This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
+
+Hope you have enjoyed and found it useful.
+Thank you!
+ 
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/manipulating-lists/slides.org	Wed Oct 13 11:12:18 2010 +0530
@@ -0,0 +1,123 @@
+#+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:    Accessing parts of arrays
+#+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
+  - Manipulating one and multi dimensional arrays
+  - Access and change individual elements 
+  - Access and change rows and columns 
+  - Slicing and striding on arrays to access chunks 
+  - Read images into arrays and manipulations
+* Sample Arrays
+  #+begin_src python
+    In []: A = array([12, 23, 34, 45, 56])
+    
+    In []: C = array([[11, 12, 13, 14, 15],
+                      [21, 22, 23, 24, 25],
+                      [31, 32, 33, 34, 35],
+                      [41, 42, 43, 44, 45],
+                      [51, 52, 53, 54, 55]])
+    
+  #+end_src
+* Question 1
+  Change the last column of ~C~ to zeroes. 
+* Solution 1
+  #+begin_src python
+    In []:  C[:, -1] = 0
+  #+end_src
+* Question 2
+  Change ~A~ to ~[11, 12, 13, 14, 15]~. 
+* Solution 2
+  #+begin_src python
+    In []:  A[:] = [11, 12, 13, 14, 15]
+  #+end_src
+* squares.png
+  #+begin_latex
+    \begin{center}
+      \includegraphics[scale=0.6]{squares}    
+    \end{center}
+  #+end_latex
+* Question 3
+  - obtain ~[22, 23]~ from ~C~. 
+  - obtain ~[11, 21, 31, 41]~ from ~C~. 
+  - obtain ~[21, 31, 41, 0]~.   
+* Solution 3
+  #+begin_src python
+    In []:  C[1, 1:3]
+    In []:  C[0:4, 0]
+    In []:  C[1:5, 0]
+  #+end_src
+* Question 4
+  Obtain ~[[23, 24], [33, -34]]~ from ~C~
+* Solution 4
+  #+begin_src python
+    In []:  C[1:3, 2:4]
+  #+end_src
+* Question 5
+  Obtain the square in the center of the image
+* Solution 5
+  #+begin_src python
+    In []: imshow(I[75:225, 75:225])
+  #+end_src
+* Question 6
+  Obtain the following
+  #+begin_src python
+    [[12, 0], [42, 0]]
+    [[12, 13, 14], [0, 0, 0]]
+  #+end_src
+
+* Solution 6
+  #+begin_src python
+    In []: C[::3, 1::3]
+    In []: C[::4, 1:4]
+  #+end_src
+* Summary
+  You should now be able to --
+  - Manipulate 1D \& Multi dimensional arrays
+      - Access and change individual elements 
+      - Access and change rows and columns 
+      - Slice and stride on arrays
+  - Read images into arrays and manipulate them.
+* 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
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/manipulating-lists/slides.tex	Wed Oct 13 11:12:18 2010 +0530
@@ -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}