Python Class example with Function

Example 1:-  Write code for file ok.py


class Person:
 

  def myfunc(self,a,b):
    print(a+b)
   
  def myage(self,age):
   
    print(age)

p1 = Person()
p1.myfunc(10,20)
p1.myage(20)

output:-run command  python ok.py 

30 

20

example 2)


 class Person:

  def __init__(self, name, age):
    self.name = name
    self.age = age

  def myfunc(self,a=None,b=None):
    print(a+b)
    print("Hello my name is " + self.name)

p1 = Person("John", 36)
p1.myfunc(10,20)


Output:-


Previous
Next Post »