diff --git a/sampleclass.py b/sampleclass.py index 0870430..3d2ef2b 100644 --- a/sampleclass.py +++ b/sampleclass.py @@ -1,23 +1,35 @@ # SAMPLE CODE TO ILLUSTRATE CLASSES IN PYTHON - - +# here we define a class class TheThing(object): + # this is a constructor def __init__(self): - self.number = 0 + self.number = 0 # this is a attribute + + # here we define a method def some_function(self): print "I got called." + + # here we define a method and return a value def add_me_up(self, more): - self.number += more + self.number += more # here return self.number -# two different things + +# we have one instance a = TheThing() + +# here we have other instance b = TheThing() + a.some_function() b.some_function() + print a.add_me_up(20) print a.add_me_up(20) print b.add_me_up(30) print b.add_me_up(30) + print a.number print b.number + +# learn more about classes here https://docs.python.org/2/tutorial/classes.html \ No newline at end of file diff --git a/samplefunc.py b/samplefunc.py index 998966e..e197524 100644 --- a/samplefunc.py +++ b/samplefunc.py @@ -1,8 +1,24 @@ +# functions example + +# we define a function like this: +# def funName(attrs): +# def multiply(a,b): - c=a*b - print "product is %r" %c + # Here return a value, a function can return or not a value + return a*b + def add(a,b): - e=a+b - print "addition is %r" %e -multiply(3,4) -add(3,4) + # Here return a value + return a+b + +# In this function don't return any value +def sayHello(name): + print "Hello ", name + +# we call this functions here +print "multiply: ", multiply(3,4) +print "add: ", add(3,4) + +sayHello("Python") + +# learn more about functions here https://www.learnpython.org/en/Functions \ No newline at end of file