Code Is Poetry

Life is Binary


Python 5: Classes

class myClass():  def method1(self):    print "myClass method1"      def method2(self, someString):    print "myClass method2: " + someString    class anotherClass(myClass):  def method2(self):    print "anotherClass method2"      def method1(self):    myClass.method1(self);    print "anotherClass method1"      def main(): ...

Read more

Python 4: Loops

def main():  x = 0    # define a while loop  while (x < 5):     print x     x = x + 1  # define a for loop  for x in range(5,10):    print x      # use a for loop over a collection  days = ["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]  for d in days:    print d    # use the break and continue statements  for x in range(5,10):    #if...

Read more

Python 3: Conditional Statements

def main():  x, y = 10, 100  # conditional flow uses if, elif, else    if(x < y):    st= "x is less than y"  elif (x == y):    st= "x is same as y"  else:    st= "x is greater than y"  print st    # conditional statements let you use "a if C else b"  st = "x is less than y" if (x < y) else "x is greater than or equal to y"  print st    # Python does not have support for...

Read more

Python 2: Functions

## Example file for working with functions## define a functiondef func1():  print "I am a function"# function that takes argumentsdef func2(arg1, arg2):  print arg1, " ", arg2# function that returns a valuedef cube(x):  return x*x*x# function with default value for an argumentdef power(num, x=1):  result = 1;  for i in range(x):    result = result * num    return result#function with variable number of argumentsdef multi_add(*args): ...

Read more

Python 1: decalring variables

# Declare a variable and initialize itf = 0;print f# re-declaring the variable worksf = "abc"print f# ERROR: variables of different types cannot be combined#print "string type " + 123print "string type " + str(123)# Global vs. local variables in functionsdef someFunction():  #global f  f = "def"  print fsomeFunction()print f del fprint f

Read more