summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJovina2011-06-29 14:25:31 +0530
committerJovina2011-06-29 14:25:31 +0530
commitd23eafd39061db1ef64b3177d7eca988da967daa (patch)
tree10b734a5329d9b0b20cfcd4b61bd8c5b80de7b60
parent7b783e26304f54d7623570037cf85fee65ce06ee (diff)
downloadst-scripts-d23eafd39061db1ef64b3177d7eca988da967daa.tar.gz
st-scripts-d23eafd39061db1ef64b3177d7eca988da967daa.tar.bz2
st-scripts-d23eafd39061db1ef64b3177d7eca988da967daa.zip
Minor changes to the script 'manipulating lists'.
-rw-r--r--basic_datatypes_and_operators/slides.org.orig158
-rw-r--r--input_output/script.rst.orig222
-rw-r--r--manipulating_lists/script.rst158
3 files changed, 153 insertions, 385 deletions
diff --git a/basic_datatypes_and_operators/slides.org.orig b/basic_datatypes_and_operators/slides.org.orig
deleted file mode 100644
index b78549f..0000000
--- a/basic_datatypes_and_operators/slides.org.orig
+++ /dev/null
@@ -1,158 +0,0 @@
-#+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: Plotting Data
-#+AUTHOR: FOSSEE
-#+DATE: 2010-09-14 Tue
-#+EMAIL: info@fossee.in
-
-#+DESCRIPTION:
-#+KEYWORDS:
-#+LANGUAGE: en
-#+OPTIONS: H:1 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
-#+STARTUP: align fold nodlcheck hidestars oddeven lognotestate
-
-* Outline
-** Datatypes in Python
-*** Numbers
-*** Boolean
-*** Sequence
-** Operators in Python
-*** Arithmetic Operators
-*** Boolean Operators
-** Python Sequence Datatypes
-*** list
-*** string
-*** tuple
-
-* Numbers
- - int
- - float
- - complex
-* Question 1
- - Find the absolute value of 3+4j
-* Solution 1
- #+begin_src python
- abs(3+4j)
- #+end_src python
-* Question 2
- - What is the datatype of number 999999999999999999? Is it
-not int?
-
-* Solution 2
- - Long
- - Large integers numbers are internally stored in python as Long
- datatype.
-
-
-* Boolean
- #+begin_src python
- In []: t=True
- In []: f=False
- #+end_src
-
-* Question 3
- - Using python find sqaure root of 3?
-
-* Solution 3
-
- - 3**0.5
-
-* Question 4
- - Is 3**1/2 and 3**0.5 same
-* Solution 4
- - No,One gives an int answer and the other float
-
-* Sequence Data types
-** Properties
- - Data in Sequence
- - Accessed using Index
-** Type
- - list
- - String
- - Tuple
-
-* All are Strings
- #+begin_src python
- k = 'Single quote'
- l = "Double quote contain's single quote"
- m = '''"Contain's both"'''
-
- #+end_src
-* Immutabilty Error
- #+begin_src python
- In []: greeting_string[1]='k'
- -------------------------------------------------------
- TypeError Traceback (most recent call last)
-
- /home/fossee/<ipython console> in <module>()
-
- TypeError: 'str' object does not support item assignment
- #+end_src
-
-* Question 5
- Check if 3 is an element of the list [1,7,5,3,4]. In case it is
-change it to 21.
-
-* Solution 5
- #+begin_src python
- l=[1,7,5,3,4]
- 3 in l
- l[3]=21
- l
- #+end_src
-* Question 6
- Convert the string ~"Elizabeth is queen of england"~ to ~"Elizabeth is
-queen"~
-
-* Solution 6
- #+begin_src python
- s = "Elizabeth is queen of england"
- stemp = s.split()
- ' '.join(stemp[:3])
- #+end_src
-* Summary
- - Number Datatypes -- integer,float and complex
- - Boolean and datatype and operators
- - Sequence data types -- List, String and Tuple
- - Accesing sequence
- - Slicing sequences
- - Finding length, sorting and reversing operations on sequences
- - Immutability
-* 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/input_output/script.rst.orig b/input_output/script.rst.orig
deleted file mode 100644
index b35507d..0000000
--- a/input_output/script.rst.orig
+++ /dev/null
@@ -1,222 +0,0 @@
-.. Objectives
-.. ----------
-
-.. #. How to print some value
-.. #. How to print using modifiers
-.. #. How to take input from user
-.. #. How to display a prompt to the user before taking the input
-
-.. Prerequisites
-.. -------------
-
-.. 1. Loops
-
-.. Author : Nishanth Amuluru
- Internal Reviewer : Puneeth
- External Reviewer :
- Checklist OK? : <put date stamp here, if OK> [2010-10-05]
-
-Script
-------
-
-Hello friends and welcome to this tutorial on Input/Output
-
-{{{ Show the slide containing title }}}
-
-{{{ Show the slide containing the outline slide }}}
-
-Input and Output are used in almost every program we use.
-In this tutorial, we shall learn how to
-
- * Output data
- * Take input from the user
-
-type
-::
-
- a = "This is a string"
- a
- print a
-
-<<<<<<< local
-print a, prints the value of a.
-=======
-``print a``, obviously, is printing the value of ``a``.
->>>>>>> other
-As you can see, even when you type just a, the value of a is shown.
-But there is a difference.
-
-.. #[Amit: The next sentence does seem to be clear enough]
-
-Typing a shows the value of a while print a prints the string. This difference
-becomes more evident when we use strings with newlines in them.
-type
-::
-
- b = "A line \n New line"
- b
- print b
-
-As you can see, just typing b shows that b contains a newline character.
-While typing print b prints the string and hence the newline.
-
-Moreover when we type just a, the value a is shown only in interactive mode and
-does not have any effect on the program while running it as a script.
-
-.. #[punch: I think we could show that?]
-
-We shall look at different ways of outputting the data.
-
-<<<<<<< local
-.. #[Amit: C's printf syntax ?? i think its better to elaborate the
- idea]
-
-print statement in python supports string formatting.
-Various arguments can be passed to print using modifiers.
-=======
-``print`` statement also accepts the syntax of C's ``printf`` statement.
-Various arguments can be passed to ``print`` using modifiers.
->>>>>>> other
-type
-::
-
- x = 1.5
- y = 2
- z = "zed"
- print "x is %2.1f y is %d z is %s"%(x,y)
-
-As you can see, the values of x and y are substituted in place of
-``%2.1f`` and ``%d``
-
-{{{ Pause here and try out the following exercises }}}
-
-%% 1 %% What happens when you do ``print "x is %d y is %f" %(x, y)``
-
-{{{ continue from paused state }}}
-
-We see that the ``int`` value of x and ``float`` value of y are
-printed corresponding to the modifiers used in the print statement.
-
-We can also see that ``print`` statement prints a new line character
-at the end of the line, everytime it is called. This can be suppressed
-by using a "," at the end ``print`` statement.
-
-Let us see this by typing out following code on an editor as print_example.py
-
-{{{ open an editor }}}
-type
-::
-
- print "Hello"
- print "World"
-
- print "Hello",
- print "World"
-
-Now we run the script using %run /home/fossee/print_example.py
-
-As we can see, the print statement when used with comma in the end, prints a
-space instead of a new line.
-
-Now we shall look at taking input from the user.
-We will use the ~~raw_input~~ for this.
-type
-::
-
- ip = raw_input()
-
-The cursor is blinking indicating that it is waiting for input
-type
-::
-
- an input
-
-and hit enter.
-Now let us see what is the value of ip by typing.
-::
-
- ip
-
-We can see that it contains the string "an input"
-
-{{{ Pause here and try out the following exercises }}}
-
-%% 2 %% enter the number 5.6 as input and store it in a variable called c.
-
-{{{ continue from paused state }}}
-
-We have to use the raw_input command with variable c.
-type
-::
-
- c = raw_input()
- 5.6
- c
-
-Now let us see the type of c.
-
-::
-
- type(c)
-
-We see that c is a string. This implies that anything you enter as input, will
-be taken as a string no matter what you enter.
-
-{{{ Pause here and try out the following exercises }}}
-
-%% 3 %% What happens when you do not enter anything and hit enter
-
-{{{ continue from paused state }}}
-
-::
-
- d = raw_input()
- <RET>
- d
-
-We see that when nothing is entered, an empty string is considered as input.
-
-raw_input also can display a prompt to assist the user.
-::
-
- name = raw_input("Please enter your name: ")
-
-prints the string given as argument and then waits for the user input.
-
-{{{ Pause here and try out the following exercises }}}
-
-%% 4 %% How do you display a prompt and let the user enter input in next line
-
-{{{ continue from paused state }}}
-
-.. #[Puneeth: We didn't talk of new-line character till now, did we?]
-.. #[Puneeth: non-programmers might not know?]
-
-.. #[Amit: Well there is a discussion earlier about new lines, I think its good
-.. as a slight trick question. But may be next line is a more easier lexicon]
-
-The trick is to include a newline character at the end of the prompt string.
-::
-
- ip = raw_input("Please enter a number in the next line\n> ")
-
-prints the newline character and hence the user enters input in the next line
-
-{{{ Show summary slide }}}
-
-This brings us to the end of the tutorial.
-In this totorial we have learnt
-
- * How to print some value
- * How to print using modifiers
- * How to take input from user
- * How to display a prompt to the user before taking the input
-
-{{{ 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.
-
diff --git a/manipulating_lists/script.rst b/manipulating_lists/script.rst
index b8727a9..7fe6aea 100644
--- a/manipulating_lists/script.rst
+++ b/manipulating_lists/script.rst
@@ -19,31 +19,49 @@
Script
------
+.. L1
+
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
+.. R1
+
Hello friends and Welcome to the tutorial on 'Manipulating Lists'.
+.. L2
+
{{{ Show the slide containing objectives }}}
+.. R2
+
At the end of this tutorial, you will be able to,
1. Concatenate two lists
#. Learn the details of slicing and striding of lists
#. Sort and reverse lists.
+.. L3
+
{{{ Switch to the pre-requisite slide }}}
+.. R3
+
Before beginning this tutorial,we would suggest you to complete the
tutorial on "Getting started with Lists".
-{{{ Open the terminal and start ipython }}}
+.. L4
-let us start ipython on our terminal
+{{{ Open the terminal and start ipython }}}
::
ipython
+.. R4
+
+let us start ipython on our terminal
+
+.. R5
+
We have already learnt about lists 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
@@ -53,16 +71,24 @@ 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,
+
+.. L5
::
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
+.. R6
+
To obtain all the primes between 10 and 20 from the above list of
primes we say
+
+.. L6
::
primes[4:8]
+.. R7
+
This gives us all the elements in the list starting from the element
with the index 4, which is 11, upto the element with index 8
in the list but not including the eighth element. So we obtain a slice
@@ -74,113 +100,187 @@ element with index 8 was excluded.
Pause the video here, try out the following exercise and resume the video.
+.. L7
+
+.. L8
+
{{{ Show slide with exercise 1 }}}
+.. R9
+
Obtain the primes less than 10, from the list ``primes``.
+.. R10
+
Switch to the terminal for solution
+.. L10
+
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::
primes[0:4]
+.. R11
+
It give us the primes below 10.
+.. L11
+
+.. L12
+
{{{ Show the slide containing p[start:stop] }}}
+.. R12
+
Generalizing, we can obtain a slice of the list "p" from the index
"start" upto the index "end" but excluding "end" with the
syntax ``p[start:stop]``
+.. L13
+
{{{ Switch to terminal }}}
+.. R13
+
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
+between start and stop in steps of one.
+
+.. R14
+
+Python also provides us the functionality to specify the steps in which
+the slice must be obtained. Say we have
+
+.. L14
::
num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
+.. R15
+
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
+
+.. L15
::
num[1:10:2]
+.. R16
+
When no step is specified, it is assumed to be 1. Similarly, 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
+
+.. L16
::
num[:10]
+.. R17
+
This gives us all the elements from the beginning upto the 10th
element but not including the 10th element in the list. Similarly
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
+
+.. L17
::
num[10:]
+.. R18
+
This gives all the elements starting from the 10th element in the list
"num" upto the final element including that last element.
To get all the even numbers in the list "num", we do
+
+.. L18
::
num[::2]
+.. R19
+
Pause the video here, try out the following exercise and resume the video.
+.. L19
+
+.. L20
+
{{{ Show slide with exercise 2 }}}
+.. R20
+
Obtain all the multiples of three from the list ``num``.
+.. L21
+
{{{ Show slide with Solution 2 }}}
+.. R21
+
+The solution is on your screen.
+
``num[::3]`` gives us all the multiples of 3 from the list, since every
third element in it, starting from 0, is divisible by 3.
+.. R22
+
The other basic operation that we can perform on lists is concatenation
of two or more lists. We can combine two lists by using the "plus"
operator. Say we have
+.. L22
::
a = [1, 2, 3, 4]
b = [4, 5, 6, 7]
a + b
+.. R23
+
When we concatenate lists using the "plus" operator we get a new
list. We can store this list in a new variable,say c,
+
+.. L23
::
c = a + b
c
+.. R24
+
It is important to observe that the "plus" operator always returns a
new list without altering the lists being concatenated in any way.
We know that a list is a collection of data. Whenever we have a
collection, we run into situations where we want to sort the
collection. Lists support ``sort`` method which sorts the list in place
+
+.. L24
::
a = [5, 1, 6, 7, 7, 10]
a.sort()
+.. R25
+
Now the contents of the list ``a`` will be
+
+.. L25
::
a
+.. R26
+
As the ``sort`` method sorts the elements of a list, 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
@@ -188,62 +288,99 @@ 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
+
+.. L26
::
a = [5, 1, 6, 7, 7, 10]
sorted(a)
+
+.. R27
We can store this sorted list into another list variable
+
+.. L27
::
sa = sorted(a)
+.. R28
+
Python also provides the ``reverse`` method which reverses
the list in place
+
+.. L28
::
a = [1, 2, 3, 4, 5]
a.reverse()
+.. R29
+
the ``reverse`` method reverses the list "a" and stores the reversed
list in place i.e. in "a" itself. Lets see the list "a"
+
+.. L29
::
a
+.. R30
+
But again the original list is lost.
To reverse a list, we could use striding with negative indexing.
+
+.. L30
::
a[::-1]
+.. R31
+
We can also store this new reversed list in another list variable.
Pause the video here, try out the following exercise and resume the video.
+.. L31
+
+.. L32
+
{{{ Show slide with exercise 3 }}}
+.. R32
+
Given a list of marks of students in an examination, obtain a
list with marks in descending order.
marks = [99, 67, 47, 100, 50, 75, 62]
+.. R33
+
Switch to terminal for solution.
+.. L33
+
{{{continue from paused state}}}
{{{ Switch to the terminal }}}
::
sorted(marks)[::-1]
+.. R34
+
OR
+.. L34
::
sorted(marks, reverse = True)
+.. L35
+
{{{ Show summary slide }}}
+.. R35
+
This brings us to the end of this tutorial. In this tutorial,
we have learnt to,
@@ -252,8 +389,12 @@ we have learnt to,
#. Sort lists using the ``sort`` method.
#. Use the method ``reverse`` to reverse the lists.
+.. L36
+
{{{Show self assessment questions slide}}}
+.. R36
+
Here are some self assessment questions for you to solve
1. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
@@ -266,9 +407,12 @@ Here are some self assessment questions for you to solve
3. ``reversed`` function reverses a list in place. True or False?
+.. L37
{{{solution of self assessment questions on slide}}}
+.. R37
+
And the answers,
1. The last four primes can be obtained from the given list as,
@@ -283,8 +427,12 @@ And the answers,
3. False. The function ``reverse`` will reverse a list in place.
+.. L38
+
{{{ Show the thank you slide }}}
+.. R38
+
Hope you have enjoyed this tutorial and found it useful.
Thank you!