Tuesday, February 22, 2005
First python program
With a excellent introduction to python language I started working on python and today I thought I will dedicate the first python program of mine to Sidharth Kuruvila LJUserInfo
Though this program is not essentially any worth(my opinion), I am still happy because it is my first program :)
People really interested can go ahead to have look at the source and comment on it.
View Python sourceHide Python source
I created a small program Evaluate.py:
View Python outputHide Python output
I executed the script using IDLE and here were some of the actions I proceeded with some actions and got the results:
def functionCreator(choice):
"""Create the function and return it depending on choice."""
if(choice==1):
print "Create function for evaluating x*y+z ..."
func = lambda x,y,z: x*y+z
elif(choice==2):
print "Create function for evaluating x*y-z ..."
func = lambda x,y,z: x*y-z
elif(choice==3):
print "Create function for evaluating x*y*z ..."
func = lambda x,y,z: x*y*z
elif(choice==4):
print "Create function for evaluating x*y**z ..."
func = lambda x,y,z: x*y**z
# return the generated function
return func
# A class which defines a default function definition to evaulate its variables
# It also allows user to change the function definition at runtime so the class
# behaviour to evaulate its variables changes.
class Evaluate(object):
""" Allow evaluation of 3 class variables with custom function at runtime."""
def __init__(self,x,y,z):
"""Initializer which takes 3 variables x,y,z"""
self.x = x #self.__dict__["x"] = x
self.y = y #self.__dict__["x"] = y
self.z = z #self.__dict__["x"] = z
def evaluate(self,x,y,z):
""" A default function definition to evaluate the variables.
This function definition is configurable at runtime."""
return x+y+z
def func(self):
""" Call this function to evaluate the variables of the class
This will delegate the work to evaluate function."""
print "%s: %f" % ("Result",self.evaluate(self.x,self.y,self.z))
def change(self):
""" This function will allow user to configure the function definition
of evaluate."""
print "Available function generation for evaluating expression..."
print "1. x*y+z"
print "2. x*y-z"
print "3. x*y*z"
print "4. x*y**z"
choice = int(raw_input("Please enter your choice(1,2,3,4): "))
# Change the evaluate function defintion
self.__dict__["evaluate"] = functionCreator(choice)
# Inform user that the function definition was changed and can view the change
print "Please try object.func() to see the change in evaluation."
# Testing for this module
if __name__ == "__main__":
print "Evaluate Module loaded..."
View Python outputHide Python output
>>> exeval=Evaluate(10,20,30)
>>> exeval.func()
Result: 60.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 1
Create function for evaluating x*y+z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 230.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 2
Create function for evaluating x*y-z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 170.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 3
Create function for evaluating x*y*z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 6000.000000
>>> exeval.change()
Available function generation for evaluating expression...
1. x*y+z
2. x*y-z
3. x*y*z
4. x*y**z
Please enter your choice(1,2,3,4): 4
Create function for evaluating x*y**z ...
Please try object.func() to see the change in evaluation.
>>> exeval.func()
Result: 10737418239999999609046623630827404132352.000000
>>>
# posted by Prasad.A : 9:55 PM