Getting Pythonic (Useful Notes)

Here is a very basic python notes from extracted from the very basic introduction in Killer Web Development Part 6.

In 6.1
Firstly, Mr Laspe statement is not true. How do you run web2py.exe from the tukker app folder when that file doesnt exist there? Secondly, if you run web2py.exe, you start a server and i never get a web2py console before. Thirdly, doesn’t “./ -S tukker” create a new tukker application? It just doesn’t make sense!

Anyway, what i am going to do is to type “python” in the console to get “>>>” prompt.

>>>print “something” or print ‘something’ both acceptable

Print several line breaks
>>>print “””something
… lalala
… lalala”””

combine string (leave a space in between)
>>>print “I love” +” “+”Mama.”
>>>print “i love “+”mama”

variables are like normal but have to begin with alphabetic letter or underscore. other characters have to be alphanumeric.

+ – * /
** exponential
% modulo
boolean returns true or false. can be used to compare sting as well.

enclosed in square brackets []
to append a new value
to change a value in the list
index starts from “0”
last index starts from [-1], then [-2]…

to get certain parts of the list, use colon “:”
>>> list1[0:]
[1, 2, 3, 4, 5]
>>> list1[1:]
[2, 3, 4, 5]  #start at index 1
>>> list1[1: -2] # start at index 1 and slice off at index -2
[2, 3]
>>> list1[:-2]
[1, 2, 3]
>>> list1[:-1]
[1, 2, 3, 4]
>>> list1[:]
[1, 2, 3, 4, 5]
also works with strings and tuples.

to map a key to a value
enclosed in curly braces {}
often used by controller function which will return a Dict and available to the view.
>>> dic = {‘title’ : ‘ZAPP’, ‘headline’ : ‘awesome app’}
>>> dic [title]
>>>print dic[‘title’]
>>>print dic[‘title’]


use 4 spaces as indentation of code blocks

if expression1:
elif expression2:

while expression:

for element in sequence:

def functionname([argument]):

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s