Friday, 21 September 2018

Python Lab - Day 2


Python Labs  2nd   Day
  1. Class Demo in Python
  2. Constructor and Destructors in Python
  3. Data Hiding in Class in Python
  4. Inheritance in Python
  5. Self Keyword in python
  6. Exception Handling in Python
  7. Documentation functions
  8. Consume User Module in Python
  9. OS import module demo in python Lab
  10. Time and calendar import demo in python




Lab 1
Class Demo
class Car:

    "my class contains method"

    price = 0

    year = 0

    rateofintrenet = 0

    def getmodelandyear(self,year,model):

        print("Your Model {0} and  price is {1}".format(year,model))

    def getpriceofcar(self,val):

        s = val.price * val.year * val.rateofintrenet / 100

        return s
 

c = Car()

c.getmodelandyear(2,"honda")

c.price = 2000000

  c.year = 5

  c.rateofintrenet = 5

  result = c.getpriceofcar(c)

  print("price : ", result)

  #print("price : ", Car.getmodelandyear(34,23))

  print(c.__doc__)


Lab 2
Constructor and Destructors in Python
# construtor and destrctor

  import math

  class Demo :

    count = 0

    def __init__(self):

        print("Object loaded",Demo.count)

       # self.count + 1

        Demo.count += 1

  

    def func(self):

        print("Constructor demo")

  

    def __del__(self):

        print("Object Gone")

  #print(math.factorial(100000))

  d1 = Demo()

  del d1

d2 = Demo()

d2.func()


Lab 3 :
Data Hiding in Class in Python
# data hiding

  

  class Demo:

    __id =  1234
    emp = 1111
    def show(self):
        print(self.__id)

  

    def __str__(self):

        return "From str method of Test: a is %s," \

               "b is %s" % (self.__id, self.emp)

  

d1 = Demo()
  #print(Demo.__id)
  print(d1.emp)
  print(d1._Demo__id)
d1.show()
  print(d1)


Lab 4
Inheritance in Python
#Inheritence

  class A:
    def __f1(self):   # defining private method
        print("i am class A")

  

  class C:
    def f1(self):
        print("i am Virtual class")

  

    def f2(self):
        print("i am class C")

  

  class B(A,C):
    def f1(self):
        super(B,self).f1()

    def f3(self):
        print("i am class B")


Lab 5
Self Keyword in python
# class sample and having self heyword
 

  class Bike :
    count =0
    def __init__(self,noofbike):
        self.count = noofbike

  

    def typeofbike(self, bikename):
        print("you have selected bike {1} of {0} type".format(bikename,self.count))

  

h1 =  Bike(2)
h1.typeofbike("honda")


Lab 6
Exception Handling in Python
a = [1,2,3]

  

  try:

    print(a[2])

    raise NameError("This is an error")

  except NameError as err:

    print("named error",err.args)

  except IndexError as err:

    print("Having Index error",err.args)

  except ZeroDivisionError as err:

    print("Error in code", err)

  except Exception as err:

    print("Error in code", err)

  finally:

    print("will definitely execute")


Lab 7
Documentation functions
def demo():

    '''

    i don't know why i am using this function.

    '''

    print("Documentaion")


demo()

  help(demo)

  class MyClass:

    a= 10

    def func(self):

        print("hello")

  

  

  print(MyClass.a)

  print(MyClass.func)


Lab  8
Consume User Module in Python
from modules_demo import *
testmodule()
 

  print(MyCalculation(1,2))



Lab 9
OS import module demo in python Lab
import os

  import  math

  print(os.getcwd())
  print(math.factorial(12))
 

  '''
a= 45.7878787878

print(math.ceil(a))

print(math.floor(a))

print("{0:.2f}".format(a))

print(round(a,2))

print("{0:.2}".format(a))

'''

  #os.chdir()


Lab 10
Time and calendar import demo in python
import time

import calendar
 

print(time.localtime())

print(calendar.calendar(2018))



No comments:

Post a Comment