இனக்குழு மற்றும் பொருள் ஆகியவற்றை விளக்கும்
மாதிரி நிரல்கள்
நிரல் 1: வட்டத்தின் பரப்பையும், சுற்றளவையும் கணக்கிடும் பைத்தான்
நிரலை எழுதுக.
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())
வெளியீடு
Enter Radius: 5
The Area = 78.5
The Circumference = 31.400000000000002
நிரல் 2: உன்னுடைய பள்ளி நூலகத்தில் உள்ள புத்தகங்களை பதிவு செய்து, புத்தகங்களின் பட்டியலை காட்டும் பைத்தான் நிரலை எழுதுக
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....")
வெளியீடு:
1. 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
1. Add New Book
2.Display Books
Enter your choice : 1
Enter Name of the Book: Learn Python
Enter Author of the Book: V.G.Ramakrishnan
Do you want continue....y
1. 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 want continue....y
1. 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
1. 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
1. Add New Book
2.Display Books
Enter 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
1. 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
நிரல் 3: சரத்தை உள்ளீடாகப் பெற்று, அதில் எத்தனை பெரிய எழுத்துக்கள்,
சிறிய எழுத்துக்கள், உயிரெழுத்துகள், மெய்யெழுத்துகள் மற்றும் இடைவெளிகள் உள்ளன என்று
கண்டறியும் பைத்தான் நிரலை எழுதுக.
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', 'T',
'o', 'O', 'u','U')):
self.vowels+=1
def count_consonants(self):
for ch in self.string:
if (ch not in ('A', 'a', 'e', 'E', 'i',
'T', 'o', 'O', 'u','U')):
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()
வெளியீடு
Enter a String: Welcome To Learn Computer
Science
The given string contains...
5 Uppercase letters
24 Lowercase letters
12 Vowels
17 Consonants
3 Spaces
நிரல் 4: பொருட்களையும், அதன் அடக்க விலையையும் சேமிக்கவும், சேமிக்கப்பட்ட பொருட்களை திரையில் தோன்றவும் செய்ய வேண்டும். அப்போது அனைத்து பொருட்களையும் அளவுகளை உள்ளீடாக பெற்று இறுதியில் விலைப்பட்டியலை அச்சிடும் பைத்தான் நிரல் ஒன்றை எழுதுக.
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( "EnterProduct
Code: " )))
self.__prod_name.append(str(input( "EnterProduct
Name:" )))
self.__cost_price.append(int(input("Enter
Cost price: ")))
def display(self):
print("Stock in Stores")
print("-------------------------------------------------------")
print("Product Code \t Product
Name It 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("-----------------------------------------------------------------------------")
Total Amount = ", total_price)
S=MyStore()
S.getdata()
S.display()
S.print_bill()
வெளியீடு:
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