Wednesday, August 7, 2013

Python 101, part 2

If you haven't loaded Python then you need to do that first.  Let me know if the instruction I wrote up in Python 101, part 1 are too confusing.  I've tried to be pretty thorough.

If you're new to programming don't get too worried.  You will get the hang of it quite quickly.

Python can be used like a very strange calculator.  When you start python you see something like this:
The cursor will be one space after the ">>>" characters.  These are the command prompt characters.
To use python as a calculator one just types the formula after the prompt then press the enter key:

>>> 5 + 1
6
>>>

Unlike the normal calculator you probably have around the house, Python follows the math rules of operator precedence.  What that means is the multiply and divide operators get executed before the add and subtract operators.

>>> 5 + 1 * 2
7
>>>

You can tell python you want certain operations to happen first by putting parens (parentheses) around the operations you want done first.

>>> (5 + 1) * 2
12
>>>

All of this is defined in the Python Tutorial under Using Python as a Calculator.  While the manual may seem frightening or confusing at first you get use to these things.  Don't read manuals like you would a book.  Use the manual like you would a catalog or a web side like BestBuy.com.

Most calculators have MR and MS buttons.  These are Memory Recall and Memory Store.  Most programming languages let you name storage locations and Python is no exception.  If you've looked at the link to Using Python as a Calculator you see these memory stores are call "Variables".  In Python you put the variable on the left side of the equal sign(=) and the remember this is just a convention to store the result of a calculation in the named location.  You can then recall the value by using its name.

>>> a = 5 + 1 * 2
>>> a
7
>>>

This, and the stuff one gets when one clicks on the "click to expand" text should be enough to solve Rosalind's Variables and Some Arithmetic.  I haven't really added much other than a lot of words.

If one was limited to numbers programming languages wouldn't be very interesting.
Strings are covered quite nicely in the Python Tutorial.  If it's too much then just stick with the basic string for now and remember the backslash(\) needs to be doubled up in certain cases.

>>> a
'this\that'
>>> print(a)
this hat
>>> a='this\\that'
>>> print(a)
this\that
>>>

Rosalind suggests using v2.7.5.  Version 2 of Python doesn't put parens around what one wants to print.  Rosalind covers lists and strings together because strings are handled like lists of characters.  There is a never ending battle among programmers concerning zero relative or one relative indexing.  Python is zero relative indexing.  Python also lets you index a range of items in a list at the same time, just remember that the range starts at the zero relative indexed item and stops one short of the second index.  One can "add" one string or list to another using the plus sing(+). Be careful when dealing with lists. Selecting a range returns a list.

>>> a='one rainy day'
>>> a[0]
'o'
>>> a[0:4]+ a[10:]
'one day'
>>> a=['one','rainy','day']
>>> a[2]+' '+a[0]
'day one'
>>> a[1:3]+a[0:1]
['rainy', 'day', 'one']
>>>

I hope this is helping you get started.

No comments:

Post a Comment