Python notes

Most Basic

var = "strong, but ducked data types"
var = 3
var = 4.3 # ie this sequence of assignments IS valid
var, otherVar = 3 # but not bools

def function( arguments ):
    'optionally, a string between the def & block serves as documentation'
    print " indentation substitutes for braces" # which appearantly forbids multiline lambdas. feh?
    return arguments
    # also, forward declarations so most nested are declared on the top

while var > 0 :
    print "var is %d and nn to that many decimal places is %.2f and a word is %s" % ( var, nn "a word" )
    print # blank line; fouled above, I would have had to break the line I think ; %r is raw output without interpretation
    print blah, # where blah already has a \n at the end - as from file lines - the ending comma suppresses print's \n
for elem in iterableCollection :
    print elem

if var == 0 :
    nn += 1 # no ++ operator
elif var < 0 :
    print "else if, but sugar can save strokes"
else :
    nn = int( nn )
# no switch so it's this or a dict

aList = [ "any dType, even lists", 0, [ .01, .10 ] ]
aTuple = ( 1, "3", "lists?NotSure" ) # can't be changed unless internal elements are mutable, ie obj's
aDictHash = { 0 : "item", "key" : 0, 5 : function } # contain anything
aDictConstructedFromLists = dict( zip( keyList, valueList ) ) # zip returns a tuple of the pairs which form the new dict
exe = aDictHash[ 5 ] # even functions
exe( "arguments" ) # calls function( ) above
forwardListSlice = aList[ :1 ] # not inclusive
rearListSlice = aList[ 1: ] # inclusive, go figure. Also slices from neg indicies indicate dist from rear.

for uu in range( 0, 10, 1 ):    # makes a list; in xrange( 0, 19 ) will generate numbers individually
    aList.append( uu ) # to rear

I-O

nn = raw_input( "prompt" )

for line in sys.stdin:  # however, this reads until EoF before starting the loop
  print line # or whatever; print line[comma] to suppress print's auto-\n

from sys import argv # takes the arguments when you opened the python script ie "[python] shaw.py target.txt 444"
script, file = argv # the script is always the first in the list, so this form 'discards' it
# open for reading
file.open( "nn.txt" )
# open for writing
file = open( file, 'w' )
# open for appending
file = open( file, 'a' )
# print all a file's lines
file.read( )
# change the pointer
file.seek( index ) # though I'm not sure whether those indicate lines or chars or what. Test it mr bacon
# read a line
file.readline( )
file.close( )

OOP

class bla( object ): # or other base class
    nn = "a static field"
    def __init__( self ): # constructor
        self.attribute = val # declares the attribute's field
        nn = self.attribute + 1
    def whinge( self, degree ):
        return degree
        # every method's first argument is self, gah

instance = bla( ) # if the class is in the same file or if 
exn = module.bla( ) # you've imported it from another file
instance.whinge( "I don't wanna" )
module.bla.nn = "something else" # change static of a class of a different module