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 “./web2py.py -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.”
or
>>>print “i love “+”mama”

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

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

Lists
enclosed in square brackets []
to append a new value
>>>list.append(5)
to change a value in the list
>>>list[2]=6
index starts from “0”
last index starts from [-1], then [-2]…

Slicing
to get certain parts of the list, use colon “:”
>>>list1=[1,2,3,4,5]
>>> 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.

Dictionaries
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]
‘ZAPP’
>>>print dic[‘title’]
title
>>>print dic[‘title’]
ZAPP

6.2

use 4 spaces as indentation of code blocks

if expression1:
codeblock
elif expression2:
codeblock
else:
codeblock

while expression:
codeblock

for element in sequence:
codeblock

Functions
def functionname([argument]):
codeblock

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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