Home | | Computer Science 12th Std | Sample Python Programs to illustrate classes and objects

Chapter: 12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects

Sample Python Programs to illustrate classes and objects

Python Classes and Objects

Sample Programs to illustrate classes and objects

 

Program 1: Write a program to calculate area and circumference of a circle

class Circle:

pi=3.14

def__init__(self,radius):

self.radius=radius

def area(self):

return Circle.pi*(self.radius**2)

def circumference(self):

return 2*Circle.pi*self.radius

r=int(input("Enter Radius: "))

C=Circle(r)

print("The Area =",C.area())

print("The Circumference =", C.circumference())

Output:

Enter Radius: 5

The Area = 78.5

The Circumference = 31.400000000000002

 

Program 2: write a menu driven program that keeps record of books available in you school library

class Library:

def __init__(self):

self.bookname=""

self.author=""

def getdata(self):

self.bookname = input("Enter Name of the Book: ")

self.author = input("Enter Author of the Book: ")

def display(self):

print("Name of the Book: ",self.bookname)

print("Author of the Book: ",self.author)

print(“\n”)

book=[] #empty list

ch = 'y'

while(ch=='y'):

print("1. Add New Book \n 2.Display Books")

resp = int(input("Enter your choice : "))

if(resp==1):

L=Library()

L.getdata()

book.append(L)

elif(resp==2):

for x in book:

x.display()

else:

print("Invalid input....")

ch = input("Do you want continue....")

Output:

Add New Book

2.Display BookS

Enter your Choice : 1

Enter Name of the Book: Programming in C++

Enter Author of the Book: K. Kannan Do you want continue....y

Add New Book

 

2.Display Books

Enter your choice : 1

Enter Name of the Book: Advanced Python

Enter Author of the Book: Dr.

Vidhya Do you wantcontinue....y

Add New Book

 

2.Display Books Enter your choice : 1

Enter Name of the Book: Working with OpenOffice

Enter Author of the Book: N.V.Gowrisankar

Do you want continue....y

Add New Book

2.Display Books

Enter your choice : 1

Enter Name of the Book: Data Structure

Enter Author of the Book: K.Lenin

Do you want continue....y

Add New Book

2.Display Books

Ener your choice : 1

Enter Name of the Book: An Introduction to Database System

Enter Author of the Book: R.Sreenivasan

Do you want continue....y

Add New Book

2.Display Books

Enter your choice : 2

Name of the Book: Programming in C++

Author of the Book: K. Kannan

Name of the Book: Learn Python

Author of the Book: V.G.Ramakrishnan

Name of the Book: Advanced Python

Author of the Book: Dr. Vidhya

Name of the Book: Working with OpenOffice

Author of the Book: N.V.Gowrisankar

Name of the Book: Data Structure

Author of the Book: K.Lenin

Name of the Book: An Introduction to Database System

Author of the Book: R.Sreenivasan

Do you want continue....n

 

Program 3: Write a program to accept a string and print the number of uppercase, lowercase, vowels, consonants and spaces in the given string

class String:

def __init__(self):

self.uppercase=0

self.lowercase=0

self.vowels=0

self.consonants=0

self.spaces=0

self.string=""

def getstr(self):

self.string=str(input("Enter a String: "))

def count_upper(self):

for ch in self.string:

if (ch.isupper()):

self.uppercase+=1

def count_lower(self):

for ch in self.string:

if (ch.islower()):

self.lowercase+=1

def count_vowels(self):

for ch in self.string:

if (ch in ('A', 'a', 'e', 'E', 'i', 'I', 'o', 'O', 'l', 'L')):

self.vowels+=1

def count_consonants(self):

for ch in self.string:

if (ch not in ('A', 'a', 'e', 'E', 'i', 'I', 'o', 'O', 'l', 'L')):

self.consonants+=1

def count_space(self):

for ch in self.string:

if (ch==" "):

self.spaces+=1

def execute(self):

self.count_upper()

self.count_lower()

self.count_vowels()

self.count_consonants()

self.count_space()

def display(self):

print("The given string contains...")

print("%d Uppercase letters"%self.uppercase)

print("%d Lowercase letters"%self.lowercase)

print("%d Vowels"%self.vowels)

print("%d Consonants"%self.consonants)

print("%d Spaces"%self.spaces)

S = String()

S.getstr()

S.execute()

S.display()

Output

Enter a String: Welcome To Learn Computer Science

The given string contains...

5 Uppercase letters

24 Lowercase letters

13 Vowels

20 Consonants

4 Spaces

 

Program 4: Write a program to store product and its cost price. Display all the available products and prompt to enter quantity of all the products. Finally generate a bill which displays the total amount to be paid

class MyStore:

__prod_code=[]

__prod_name=[]

__cost_price=[]

__prod_quant=[]

def getdata(self):

self.p = int(input("Enter no. of products you need to store: "))

for x in range(self.p):

self.__prod_code.append(int(input("Enter Product Code: ")))

self.__prod_name.append(str(input("Enter Product Name: ")))

self.__cost_price.append(int(input("Enter Cost price: ")))

def display(self):

print("Stock in Stores")

print("----------------------------------------------------------")

print("Product Code \t Product Name \t Cost Price")

print("----------------------------------------------------------")

for x in range(self.p):

print(self.__prod_code[x], "\t\t", self.__prod_name[x], "\t\t", self.__cost_ price[x])

print("----------------------------------------------------------")

def print_bill(self):

total_price = 0

for x in range(self.p):

q=int(input("Enter the quantify for the product code %d : "%self.__ prod_code[x]))

self.__prod_quant.append(q)

total_price = total_price +self.__cost_price[x]*self.__prod_quant[x]

print("Invoice Receipt")

print("-----------------------------------------------------------------------------")

print("Product Code\t Product Name\t Cost Price\t Quantity \t Total Amount")

print("-----------------------------------------------------------------------------")

for x in range(self.p):

print(self.__prod_code[x], "\t\t", self.__prod_name[x], "\t\t",

self.__cost_price[x], "\t\t", self.__prod_quant[x], "\t\t",

self.__prod_quant[x]*self.__cost_price[x])

print("-----------------------------------------------------------------------------")

print("         Total Amount = ", total_price)

S=MyStore()

S.getdata()

S.display()

S.print_bill()

Output:

Enter no. of products you need to store: 5

Enter Product Code: 101

Enter Product Name: Product-A

Enter Cost price: 25

Enter Product Code: 201

Enter Product Name: Product-B

Enter Cost price: 35

Enter Product Code: 301

Enter Product Name: Product-C

Enter Cost price: 35

Enter Product Code: 401

Enter Product Name: Product-D

Enter Cost price: 50

Enter Product Code: 501

Enter Product Name: Product-E

Enter Cost price: 120


Enter the quantify for the product code 101 : 10

Enter the quantify for the product code 201 : 15

Enter the quantify for the product code 301 : 10

Enter the quantify for the product code 401 : 20

Enter the quantify for the product code 501 : 10



Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
12th Computer Science : Chapter 10 : Python Modularity and OOPS : Python Classes and Objects : Sample Python Programs to illustrate classes and objects |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.