Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Saturday, January 21, 2012

Python Note 6(Classes and Objects)

Sample Code  : General class
#define a class
class User :
 def __init__(self,name,user_id,acc_balance):
  self.Name=name
  self.User_id=user_id
  self.Acc_Balance=acc_balance
  
 def recharge(self,amount):
  self.Acc_Balance+=amount
 
 def withdraw(self,amount):
  if amount <= self.Acc_Balance :
   self.Acc_Balance-=amount
  else :
   pass #do nothing
 
 def getBalance(self): #parameter less method
  return self.Acc_Balance
  
 def __del__(self) :
  print("Destructor started!")
#end of the class
 
usr=User("Kanishka",1133,27000.00)
print("User {0} , Balance {1:5.2f}".format(usr.Name,usr.getBalance()))
usr.withdraw(2000.50) #withdrwaw money
print("User {0} , Balance {1:5.2f}".format(usr.Name,usr.getBalance()))

Wednesday, January 18, 2012

Python Note 3 (Functions)

We can define number of functions in same python script.
Code
#function1
def cube(x) :
 return x**3

#function2
def maxVal(x,y) :
 if x > y :
  return x
 else :
  return y

print (cube(3))
print (maxVal(12,22))

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis