Skip to content

Add comments to samplefunc.py #21

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions sampleclass.py
Original file line number Diff line number Diff line change
@@ -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
28 changes: 22 additions & 6 deletions samplefunc.py
Original file line number Diff line number Diff line change
@@ -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