Linear Pythonism for the Absolute Beginner.
Preliminaries
Why learn python? Well, why indeed. If you don't want to be able to manipulate your computer or make advanced web pages or scientific simulations, you might as well do something else.
If you wonder why not learn C, Java, Basic, Lisp, Perl, etc.. instead I will comment on it.
Python is a widely supported scripting language, which means that many computers are able to run it. It is easy to adopt new features which is custom made by yourself or by others.
A program idea can be made on a timescale of hours and days rather than weeks and months as with conventional programming languages. If you know why you need these other functions you also probably already know how to program in one of them, for novices timescale is of less importance.
Why not other scripting languages?
There is no argument for not using Lisp or Perl or any other scripting language, but you need to pick one of them and python is as good as any. The best advantage by learning python is that it is very easy to write and learn compared to older scripting languages. It resembles the way you would write a Java application, php and javascript so you get insight in general programming for free. Perl and Bourne and Lisp is more technical and cryptic compared to python's pseudo code style.
The scene of the plot.
Linux & or BSD/OSX: In a terminal window, write following command. ->sudo apt-get install python, or goto http://www.python.org/download/ and download tarball.
Windows: goto http://www.python.org/download/ and download install files. Then do it.
And or goto http://www.ubuntu.com/ for some change of scenery.
Presumably the stage is now set. And presumably everyone is using some form of UNIX based OS.
Act 1. (declaration of dependents)
All python programs should at least sport a header line such as #!/usr/bin/env python or #!/store/bin/python
To break it down (#) declares a comment, (!) declares something important for the system and the rest is a path to either envelop the user $PATH or the absolute path of the program named python, which is the program we wish to utilize. For windows environment this is handled automagically.
For those of you who didn't understand what this was about, just write one of the latter as the first line in the text file you want to run as a python script.
You may also wish to use some certain premade and included functions all these can be imported by writing import, wuha!.
If you want standard system tools or math functions etc... you may write ->import sys, math, string, glob.... etc... and should you be wondering what sys, math,string,glob does just write in your terminal window ->pydoc math or google it!
If you want your script to be purely mathematic you may write ->from math import * (which means from the class math get all the functions )
The difference between the two is in the first import you must write math.cos(42) to make it work, in the last import you need only write cos(42) and it works!
Act 2. (the hello of worlds)
You will now be able to greet the world and all its might as is the conventional practice for scripters and programmers learning new languages.
Write the following in a pure text file and call it hw001.py run it either by writing python hw001.py or just write hw001.py if you know what needs to be done.
--fig 01 (hw001.py)------------------------------------------------------
#!/usr/bin/env python
a = 4
b = 3.1415
c = "Hello World"
print "a = %i, b = %2.4f, c = %sn" % (a,b,c)
--------------------------------------------------------------------------------
NOTE!
If you experience problems while running your script, you might want to learn about indents. Indents are whitespaces, tabs that indicates control hierarchy in your script.
To make a python script run, these must be consistent, you need to decide whether to use some whitespaces or a tab space as a rule.
fig 03 illustrates the issue with a for loop where anything indented beneath the for loop's (:) is belonging to the loop.
Whether or not you can see the indents below depends on the browser. Use the link below to see the original scripts.
A similar script extending the math module and greeting the world again.
--fig 02 (hw002.py)-----------------------------------------------------
#!/usr/bin/env python
from math import *
a = pi
b = cos(a)
c = "Hello World"
print "a = %2.4f, b = %2.2f, c = %sn" % (a,b,c)
--------------------------------------------------------------------------------
A loop may be a useful tool and we greet the world repeatedly by using one.
--fig 03(hw003.py)------------------------------------------------------
#!/usr/bin/env python
a = 8
for i in range(a):
print "%i: Hello World" % i
--------------------------------------------------------------------------------
For the last hello world script in this act, the second one. We will include a function and call it.
--fig04(hw004.py)------------------------------------------------------
#!/usr/bin/env python
a = 8
def myfunc(x):
if x == 8:
myfunc(a)
-------------------------------------------------------------------------------
This completes the absolute basics of the plot and brings you baby steps closer to pythonism.
Act 3. (a variety of variables)
Python is a type weak, language. Meaning that you pretty much can assign anything and it will try to accomodate whatever you write.
Basic scalar variables as integers(whole numbers), floats(numbers with a comma) and strings of characters can be declared by giving them a name and assigning a value.
a = 5
i = 2
hw = "hello, world"
flt = 1,23457
You also have lists, tuples and dictionaries.
list = ["item one","item two","item three",1,2,3]
print list will give you a list of all these six items. And as you can see a list holds any type.
list.append("item 7") puts another index on the list.
list[0] = "eno meti" alters the entry on the first index of the list.
del list[5] deletes the sixth entry of the list.
list.pop() returns and removes the last item while list.pop(0) returns and removes the first item making it possible to emulate a first in first out stack. or any other order push and pop stack for that matter. Just remember, no rules, no safety.
For lists and other indexable data there is a way of indexing ranges.
list[2:5] will give you a sublist of the content between index 2 and 5
list[1:] will return a sublist of list avoiding the first element.
list[:len(list)-1] will return the sublist without the last element.
And so on.
tuple = ("x","y","z")
Tuples are containers for keeping information, you can not alter a tuple and it remains the way it was assigned. The content of the tuple variable will allways be x,y,z.
del tuple will delete the tuple if it is no longer needed.
dictionary = {"a":1, "b":2, "c":3}
dictionary["d"] = 4
dictionary.keys() will list all the keys and leave out all the values.
dictionary.has_key("b") boolean as in true or false. You could also say; b in dictionary to get a true, false response.
dictionary2 = {"Arachna": ("Peter","Parker","Spider"), "Alien": ("Clark","Kent","Super")}
A dictionary can also hold any type and the index may be numbers or strings.
You can also define sets which is a list only it has unique entries. No multi storing of the same values.
list = ["monkey", "horse", "elephant", "monkey", "monkey", "lion", "tiger", "tiger"]
animals = set(list)
print animals will result in ["monkey", "horse", "elephant", lion", "tiger"]
or if you say print set("abbacab")
it will print ["a","b","c"]
Arrays
A single array can be created by using lists, tuples or dictionaries. If you want multi-dimensional arrays you need to import a tool called NumPy.
import numpy
a = arange(10).reshape(2.4)
a = array([0,1,2,3],
[4,5,6,7])
print a[1,1] will print the number 5.
Comments