Minor edits.
--- a/lists.org Wed Apr 28 16:33:18 2010 +0530
+++ b/lists.org Thu Apr 29 17:59:25 2010 +0530
@@ -6,13 +6,13 @@
*** Script
Welcome friends.
- In this tutorial we shall look at Data structures supported by
- Python. We shall look at Lists and Tuples. We have already seen
- lists in some of previous sessions, here we shall cover more
- details about them.
+ In this tutorial we shall look at some special Data structures
+ supported by Python namely Lists and Tuples. We have already been
+ introduced to lists in some of the previous tutorials, here we
+ shall look at them in little more detail.
The list type is a container that holds a number of other
- objects, in a given order. The list type implements the sequence
+ objects, in the given order. The list type implements the sequence
protocol, and also allows you to add and remove objects from
the sequence.
@@ -21,21 +21,21 @@
num = [1, 2, 3, 4]
Items enclosed in square brackets separated by comma
constitutes a list.
- One neat feature of Python list is we can store data belonging
- to multiple kind of data structures. We can have a list with
- something:
+ One neat feature of Python list is that we can store data of any
+ type in them. We can have a list something like:
var = [1, 1.2, 'string']
print var
and with this list we can perform most of list operations.
Python lists are very versatile, that is we can change it as we
- wish. It supports features like removal, addition.
+ wish. It supports features like removal, addition, sort, etc.
Similar to strings, we can concatenate two lists using '+'
operator
so num + var will return a new list with 'var' added in end of
'num'
- We have already covered append functionality.
- To add single object at end of list 'append' function is used
+ We have already covered the append function.
+ To add single object at the end of a list the 'append'
+ function is used
num
num.append(-5)
num
@@ -50,48 +50,54 @@
num = [1, 4, -6]
num.extend([2, 8, 0])
num
- As we can notice extend and append behaves differently.
+ As we can notice extend and append behave differently.
To reverse a list 'reverse' function is available.
num
This is current content of list
num.reverse()
Now after using reverse function, lets check the value of 'num'
num
- To remove a particular element from list Python provides remove
- num.reverse(8)
- if argument is present more than once, then first occurrence is
- removed from list.
+ Please note, reverse actually manipulated the list.
+ To remove a particular element from the list Python provides
+ the remove() function
+ num.remove(8)
+ if the given argument is present more than once in the list,
+ then the first occurrence of that element is removed from list.
- Slicing and Striding concepts which we covered for Arrays work
- with lists also. Lets revisit the concept with some examples
+ The Slicing and Striding concepts which we covered for Arrays work
+ with lists as well. Lets revisit the concept by looking at some examples
a = [1, 2, 3, 4, 5]
- a[1:3] returns a list with second and third element of a
- One important feature of list indexing is negative values. In
- Lists -1 indicates last element of a list
+ a[1:3] returns a list with second and third element of 'a'
+ One important feature of list indexing is the negative index. In
+ Lists -1 indicates last element of the list
a[-1]
similarly -2 will be second last and so forth. Now these
negative indexes can also be used with slicing. If we try
a[1:-1]
we get list which excludes first and last element of a.
- and if we miss start/end index value default are taken
- a[:3] will return list from start till fourth element of a.
- If we give step size we can do striding also
+ and if we do not specify the start or the end index value the default
+ values are taken. The default values being the first element and the
+ last element.
+ a[:3] will return a list from beginning upto the fourth element of a.
+ We can perform striding as well, by specifying the step size
a[1:-1:2]
This gives second, fourth and so on items of a till we reach
last item of list.
a[::2] will skip all the even placed elements of a
- With step sizes, if we give negative values we get some
+ With step sizes, if we specify negative values we get some
interesting results. Lets try
a[::-1]
It returns reversed 'a'
- We can check for containership also with lists
+ We can check for containership with lists as well.
+ Let's look at the contents of num
num
+ To check if the number 4 is present in the list we type
4 in a
True
- Python provides support to special immutable lists known as
- 'tuple' To create a tuple instead of square brackets'[' we have
- to use normal brackets '('
+ Python provides support for special immutable lists known as
+ 'tuple' To create a tuple instead we use normal brackets '('
+ unlike '[' for lists.
t = (1, 2, 3, 4, 5, 6, 7, 8)
its elements can also be accessed using indexes
t[0] + t[3] + t[-1]
@@ -101,13 +107,16 @@
are they used we first create two variables
a, b = 1, 6
print a, b
+ As you can see multiple variable assignments are possible using
+ tuples.
Now lets swap values their values. Normal approach would be
to create a temporary to hold the value but because of tuples
we can do something cool like
b, a = a, b
print a, b
and values are swapped. And this swapping works for all types
- of variables.
+ of variables. This is possible because of something magical
+ that Python does called as tuple packing and unpacking.
With this we come to the end of this tutorial on Lists and
tuples. In this tutorial we have learnt some more operations
--- a/odes.org Wed Apr 28 16:33:18 2010 +0530
+++ b/odes.org Thu Apr 29 17:59:25 2010 +0530
@@ -11,40 +11,36 @@
In this tutorial we shall look at solving Ordinary Differential Equations,
ODE henceforth using odeint in Python. In this tutorial we shall be using
- the concepts of arrays, functions and lists which we have covered in various
+ the concepts of arrays, functions and lists which we have covered in
previous tutorials.
Let's consider the classic problem of the spread of an epidemic in a
population.
This is given by the ordinary differential equation dy/dt = ky(L-y)
where L is the total population and k is an arbitrary constant. For our
- problem Let us use L=25000, k=0.00003.
+ problem Let us use L=250000, k=0.00003.
Let the boundary condition be y(0)=250.
Let's now fire up IPython by typing ipython -pylab interpreter.
- As we saw in one of earlier session, sometimes pylab wont 'import' all
- packages. For solving 'ordinary differential equations' also we shall
- have to import 'odeint' function which is a part of the SciPy package.
+ As we saw in one of the earlier sessions, sometimes we will need more than
+ pylab to get our job done. For solving 'ordinary differential equations'
+ we shall have to import 'odeint' function, which is a part of the SciPy package.
So we run the magic command:
In []: from scipy.integrate import odeint
- For now just remember this as a command that does some magic to obtain
- the function odeint in to our program.
The details regarding `import' shall be covered in a subsequent tutorial.
We can represent the given ODE as a Python function.
This function takes the dependent variable y and the independent variable t
as arguments and returns the ODE.
- Our function looks like this:
- (Showing the slide should be sufficient)
Let us now define our function.
In []: def epid(y, t):
.... k = 0.00003
- .... L = 25000
+ .... L = 250000
.... return k*y*(L-y)
Independent variable t can be assigned the values in the interval of
@@ -118,13 +114,10 @@
In []: pend_sol = odeint(pend_int, initial,t)
- In []: plot(pend_sol[0], t) plot theta against t
- In []: plot(pend_sol[1], t) will plot omega against t
- Plotting theta against t and omega against t we obtain the plots as shown
- in the slide.
+ In []: plot(pend_sol)
+ This gives us 2 plots. The green plot is omega vs t and the blue is theta vs t.
- Thus we come to the end of this tutorial on solving ordinary differential
- equations in Python. In this tutorial we have learnt how to solve ordinary
+ Thus we come to the end of this tutorial. In this tutorial we have learnt how to solve ordinary
differential equations of first and second order.
*** Notes