Unformatted text preview:

S11BLH21 PROGRAMMING IN PYTHON LAB PROGRAMS UNIT 2 1 Execute concepts on Polymorphism Encapsulation A simple Python function to demonstrate Polymorphism a Polymorphism def add x y z 0 return x y z print add 2 3 print add 2 3 4 b class India def capital self class USA def capital self print New Delhi is the capital of India def language self print Hindi is the most widely spoken language of India def type self print India is a developing country print Washington D C is the capital of USA def language self print English is the primary language of USA def type self print USA is a developed country obj ind India obj usa USA for country in obj ind obj usa country capital country language country type c Polymorphism with Inheritance class Bird def intro self def flight self print There are many types of birds print Most of the birds can fly but some cannot class sparrow Bird def flight self print Sparrows can fly class ostrich Bird def flight self print Ostriches cannot fly obj bird Bird obj spr sparrow obj ost ostrich obj bird intro obj bird flight obj spr intro obj spr flight obj ost intro obj ost flight 2 Implement Data Abstraction and Inheritance class llgm ABC abstract classdef calculate area self abstract methodpass a from abc import ABC pass class Square llgm length 5 def Area self class Circle llgm radius 4 def Area self return self length self length return 3 14 self radius self radius sq Square object created for the class Square cir Circle object created for the class Circle print Area of a Square sq Area call to calculate area method defined inside the class Square print Area of a circle cir Area b from abc import ABC abstractmethod Abstract Class class Bank ABC def bank info self print Welcome to bank abstractmethod def interest self Abstarct Method pass Sub class child class of abstract class class SBI Bank def interest self Implementation of abstract method print In sbi bank 5 rupees interest s SBI s bank info s interest 3 Differentiate Method Overloading and Overriding S N O 1 2 3 4 5 6 Method Overloading Method Overriding In the method overloading methods or functions must have the same name and different signatures Whereas in the method overriding methods or functions must have the same name and same signatures Method overloading is a example of compile time polymorphism Whereas method overriding is a example of run time polymorphism In the method overloading inheritance may or may not be required Whereas in method overriding inheritance always required Method overloading is performed between methods within the class Whereas method overriding is done between parent class and child class methods It is used in order to add more to the behavior of methods Whereas it is used in order to change the behavior of exist methods In method overloading there is no need of more than one class Whereas in method overriding there is need of at least of two classes 4 Create a class called Person with attributes name and age Make the age attribute private and implement a getter method to access it class Person def init self name age self name name self age age Private attribute def get age self return self age Example usage person1 Person John 30 print person1 name Output John Attempting to access age directly will result in an AttributeError print person1 age AttributeError Person object has no attribute age print person1 get age Output 30 5 Create a module called math operations py with a class called Calculator Import the Calculator class into another script and use its methods to perform mathematical operations class Calculator def add self x y return x y def subtract self x y return x y def multiply self x y return x y def divide self x y if y 0 raise ValueError Cannot divide by zero return x y from math operations import Calculator Create an instance of Calculator calc Calculator Perform some mathematical operations result add calc add 10 5 print Addition result add Output 15 result subtract calc subtract 10 5 print Subtraction result subtract Output 5 result multiply calc multiply 10 5 print Multiplication result multiply Output 50 result divide calc divide 10 5 print Division result divide Output 2 0


View Full Document

Anna GE 3151 - PYTHON LAB PROGRAMS

Download PYTHON LAB PROGRAMS
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view PYTHON LAB PROGRAMS and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view PYTHON LAB PROGRAMS 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?