Monday, November 30, 2009

byte of python 2

Continue learning Python after thanksgiving breaks.

Some notes about flow control:
* It's very interesting to see that python has an if-elif-else statement. A colon has to be placed after each statement (if, elif, else) to indicate the beginning of the block.
* An optional else statement can be created after a for or while loop.
* range(1,5,2) means [1,3] (start from 1, step 2, up to 5)
* use break or continue statements for more control.

Some notes about function:
* "def foo(a,b=4):" defines a function named foo with inputs a and optional input b equals to 4.
* inside functions, use global var to make var global so that one can change the value of var inside function.
* Docstrings are cool. The author suggested using it this way - "The convention followed for a docstring is a multi-line string where the first line starts with a capital letter and ends with a dot. Then the second line is blank followed by any detailed explanation starting from the third line."

Some notes about Python data structure:
* So far, I have felt that Python is a very flexible language. Its data structures(list, tuple, dictionary, set) are very interesting. Slicing each structure is also fun.
* One thing I want to mention is that Python binds name to an object. The name only refers to the object but not represent the object itself. So "if you want to make a copy of a list or such kinds of sequences or complex objects (not simple objects such as integers), then you have to use the slicing operation to make a copy."
shoplist = ['apple', 'mango']
mylist = shoplist[:]

No comments:

Post a Comment