1.
EB BILL CALCULATOR
PROGRAM
def EBReading():
p=int(input("Enter previous EB reading: "))
c=int(input("Enter present EB reading: "))
return c-p
def calcEBBill(u):
if u<=100:
amt=0
elif u<=400:
amt=(u-100)*4.8
elif u<=500:
amt=300*4.8+(u-400)*6.45
elif u<=600:
amt=300*4.8+100*6.45+(u-500)*8.55
elif u<=800:
amt=300*4.8+100*6.45+100*8.55+(u-600)*9.65
elif u<=1000:
amt=300 * 4.8 + 100 * 6.45 + 100 * 8.55 + 200*9.65+(u-800)*10.7
else:
amt=300 * 4.8 + 100 * 6.45 + 100 * 8.55 + 200*9.65+200*10.7+(u-1000)*11.8
return amt
units=EBReading()
print("Units consumed:",units)
print("Electricity Bill Amount:",calcEBBill(units))
OUTPUT
Enter the previous EB reading (in Units): 1250
Enter the present EB reading (in Units): 2765
Units consumed: 1515
Electricity Bill Amount: 13087.0
2. DISPLAY CALENDAR MONTHS
PROGRAM
def DaysinMonth():
while True:
m=input("Enter the name of the first 3 letters of a month beginning with capital letter: ")
if m in d:
print("The number of days in the month",m,"is",d[m])
break
m=input("Incorrect month. Enter the correct month name: ")
def MonthsWith31Days():
print("The months with 31 days are:")
for i in d:
if d[i]==31:
print(i,end=' ')
print()
def MonthsInAlphabeticalOrder():
print("The months in alphabetical order are:")
for i in sorted(d):
print(i,d[i],sep='\t')
def MonthsInNDaysOrder():
print("The months sorted according to the no. of days are:")
for i in sorted(d,key=[Link]):
print(i,d[i],sep='\t')
d={'Jan':31, 'Feb':28, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':31, 'Sep':30,
'Oct':31, 'Nov':30, 'Dec':31}
y=int(input("Enter the current year in 4 digits (yyyy): "))
if y%400==0 or y%4==0 and y%100!=0:
d['Feb']=29
print("1. Days in a Month.")
print("2. Months with 31 Days.")
print("3. Months in Alphabetical Order.")
print("4. Months Sorted in Days Order.")
print("5. Exit")
while True:
choice=int(input("Enter your choice: "))
if choice==1:
DaysinMonth()
elif choice==2:
MonthsWith31Days()
elif choice==3:
MonthsInAlphabeticalOrder()
elif choice==4:
MonthsInNDaysOrder()
elif choice==5:
break
else:
print("Invalid choice.")
OUTPUT
Enter the current year in 4 digits (yyyy): 2024
1. Days in a Month.
2. Months with 31 Days.
3. Months in Alphabetical Order.
4. Months Sorted in Days Order.
5. Exit
Enter your choice: 1
Enter the name of the first 3 letters of a month beginning with capital letter: Feb
The number of days in the month Feb is 29
Enter your choice: 2
The months with 31 days are:
Jan Mar May Jul Aug Oct Dec
Enter your choice: 3
The months in alphabetical order are:
Apr 30
Aug 31
Dec 31
Feb 29
Jan 31
Jul 31
Jun 30
Mar 31
May 31
Nov 30
Oct 31
Sep 30
Enter your choice: 4
The months sorted according to the no. of days are:
Feb 29
Apr 30
Jun 30
Sep 30
Nov 30
Jan 31
Mar 31
May 31
Jul 31
Aug 31
Oct 31
Dec 31
Enter your choice: 5
3. PHONE NUMBER VALIDATOR
PROGRAM
def check (n):
if len (n) == 12 and \
n[3] == '-' and \
n[7] == '-' and \
[Link]('-', ' ').isdigit():
print (n, "is valid")
else:
print (n, "is invalid")
phone_no = input ("Enter the phone number: ")
check (phone_no)
OUTPUT
Enter the phone number: 017-555-1212
017-555-1212 is valid
Enter the phone number: 0175551212
0175551212 is invalid
4. DISPLAY MINIMUM ONES DIGIT
PROGRAM
def min_1s_digit (n1, n2):
if n1 % 10 < n2 % 10:
return n1
return n2
num1 = eval (input ("Enter the first number: "))
num2 = eval (input ("Enter the second number: "))
print ("The number that has minimum one's digit is", min_1s_digit (num1, num2))
OUTPUT
Enter the first number: 491
Enter the second number: 278
The number that has minimum one's digit is 491
5. DISPLAY EACH WORD SEPARATED BY ‘#’
PROGRAM
fname = input ("Enter the file name: ")
f = open (fname, "r")
lines = [Link] ()
for line in lines:
words = [Link] ()
print('#'.join (words))
[Link] ()
OUTPUT
Enter the file name: [Link]
We#are#class#XII#students#of#Pon#Vidyashram.
Our#school#is#located#in#Kolapakkam.
There#are#33#students#in#our#class.
6. DISPLAY THE NUMBER OF VOWELS, CONSONANTS, UPPERCASE AND
LOWERCASE CHARACTERS
PROGRAM
fname = input ("Enter the file name: ")
f = open (fname, "r")
vow = cons = ucase = lcase = 0
chars = [Link] ()
for char in chars:
if [Link] ():
if [Link] () in 'aeiou':
vow += 1
else:
cons += 1
if [Link] ():
ucase += 1
else:
lcase += 1
print ("No. of Vowels is", vow)
print ("No. of Consonants is", cons)
print ("No. of Uppercase characters is", ucase)
print ("No. of Lowercase characters is", lcase)
[Link] ()
OUTPUT
Enter the file name: [Link]
No. of Vowels is 36
No. of Consonants is 56
No. of Uppercase characters is 9
No. of Lowercase characters is 83
7. REMOVE ALL LINES THAT CONTAIN CHARACTER 'a'
PROGRAM
fname1 = input ("Enter input file name: ")
f1 = open (fname1,"r")
fname2 = input ("Enter output file name: ")
f2 = open (fname2, "w")
lines = [Link] ()
newlines = []
for line in lines:
if "a" in line or "A" in line:
[Link] (line)
else:
[Link] (line)
[Link] ()
[Link] ()
f1 = open (fname1,"w")
[Link] (newlines)
[Link] ()
print ("Removed all lines that contain Character 'a' and written it to another file")
OUTPUT
Enter input file name: [Link]
Enter output file name: [Link]
Removed all lines that contain Character 'a' and written it to another file
8A CREATE A BINARY FILE WITH NAME AND ROLL NUMBER
PROGRAM
import pickle
fname = input ("Enter the file name: ")
f = open (fname, "wb")
choice = 'y'
stu = {}
while [Link]() == 'Y':
rno = int (input ("Enter roll no.: "))
name = input ("Enter name: ")
stu['Roll No.'] = rno
stu['Name'] = name
[Link] (stu, f)
choice = input ("Another Record (y/n)? ")
[Link] ()
OUTPUT
Enter the file name: [Link]
Enter roll no.: 1
Enter name: Sharine
Another Record (y/n)? y
Enter roll no.: 2
Enter name: Sheba
Another Record (y/n)? n
8B SEARCH FOR ROLL NUMBER AND DISPLAY THE NAME
PROGRAM
import pickle
fname = input ("Enter the file name: ")
f = open (fname, "rb")
rno = int (input ("Enter roll no.: "))
try:
while True:
stu = [Link](f)
if stu['Roll No.'] == rno:
print ("Search Successful.")
print ("Student Name:", stu['Name'])
break
except EOFError:
print ("No such record found in the file.")
[Link] ()
OUTPUT
Enter the file name: [Link]
Enter roll no.: 2
Search Successful.
Student Name: Sheba
Enter the file name: [Link]
Enter roll no.: 3
No such record found in the file.
8C DISPLAY THE CONTENTS
PROGRAM
import pickle
fname = input ("Enter the file name: ")
f = open (fname, "rb")
try:
while True:
print ([Link](f))
except EOFError:
[Link] ()
OUTPUT
Enter the file name: [Link]
{'Roll No.': 1, 'Name': 'Sharine'}
{'Roll No.': 2, 'Name': 'Sheba'}
9A CREATE A BINARY FILE WITH ROLL NUMBER, NAME AND MARKS
PROGRAM
import pickle
fname = input ("Enter the file name: ")
f = open (fname, "wb")
choice = 'y'
stu = {}
while [Link]() == 'Y':
rno = int (input ("Enter roll no.: "))
name = input ("Enter name: ")
mark = float (input ("Enter marks: "))
stu['Roll No.'] = rno
stu['Name'] = name
stu['Marks'] = mark
[Link] (stu, f)
choice = input ("Another Record (y/n)? ")
[Link] ()
OUTPUT
Enter the file name: [Link]
Enter roll no.: 1
Enter name: Sharine
Enter marks: 95
Another Record (y/n)? y
Enter roll no.: 2
Enter name: Sheba
Enter marks: 85
Another Record (y/n)? n
9B INPUT A ROLL NUMBER, UPDATE NAME AND MARKS
PROGRAM
import pickle
fname = input ("Enter the file name: ")
f = open (fname, "rb+")
rno = int (input ("Enter roll no.: "))
try:
while True:
pos = [Link] ()
stu = [Link] (f)
if stu['Roll No.'] == rno:
name = input ("Enter name: ")
mark = float (input ("Enter marks: "))
stu['Name'] = name
stu['Marks'] = mark
[Link] (pos)
[Link] (stu, f)
print ("Record successfully updated.")
break
except EOFError:
print ("No such record found in the file.")
[Link] ()
OUTPUT
Enter the file name: [Link]
Enter roll no.: 2
Enter name: Sheba P
Enter marks: 90
Record successfully updated.
10. RANDOM NUMBER GENERATOR (SIMULATE A DICE)
PROGRAM
import random
x = "y"
while [Link]() == "Y":
no = [Link](1,6)
print(" _____")
print("| |")
if no == 1:
print("| |")
print("| 0 |")
print("| |")
if no == 2:
print("|0 |")
print("| |")
print("| 0|")
if no == 3:
print("| |")
print("|0 0 0|")
print("| |")
if no == 4:
print("|0 0|")
print("| |")
print("|0 0|")
if no == 5:
print("|0 0|")
print("| 0 |")
print("|0 0|")
if no == 6:
print("|0 0 0|")
print("| |")
print("|0 0 0|")
print("|_____|")
x=input("Roll again (y/n)? ")
OUTPUT
_____
| |
|0 0 0|
| |
|0 0 0|
|_____|
Roll again (y/n)? Y
_____
| |
|0 |
| |
| 0|
|_____|
Roll again (y/n)? Y
_____
| |
|0 0|
| |
|0 0|
|_____|
Roll again (y/n)? Y
_____
| |
|0 0|
| 0 |
|0 0|
|_____|
Roll again (y/n)? Y
_____
| |
| |
| 0 |
| |
|_____|
Roll again (y/n)? Y
_____
| |
| |
|0 0 0|
| |
|_____|
Roll again (y/n)? N
11. IMPLEMENT STACK USING LIST
PROGRAM
def Push(stk,clid,clnm,ph):
[Link]([clid,clnm,ph])
def Pop(stk):
if stk == []:
return "Underflow"
return [Link]()
def Peek(stk):
if stk == []:
return "Underflow"
return stk[len(stk)-1]
def Display(stk):
if stk == []:
print("Stack empty")
else:
for i in range(len(stk)-1,-1,-1):
print(stk[i])
Stack=[]
while True:
print("WELCOME TO CLIENT STACK")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display stack")
print("5. Exit")
choice = int(input("Enter your choice (1-5): "))
if choice == 1:
clid=int(input("Enter client id:"))
clnm=input("Enter the client name:")
ph=int(input("Enter phone number of client:"))
Push (Stack, clid, clnm, ph)
elif choice==2:
item=Pop(Stack)
if item == "Underflow":
print("Underflow! Stack is empty!")
else:
print("Popped item is ",item)
elif choice == 3 :
item = Peek(Stack)
if item == "Underflow":
print("Underflow! Stack is empty!")
else:
print ("Topmost item is", item)
elif choice == 4:
Display(Stack)
elif choice == 5:
break
else:
print("Invalid choice!")
OUTPUT
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 1
Enter client id:1
Enter the client name:Sharine
Enter phone number of client:1234567890
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 4
[1, 'Sharine', 1234567890]
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 3
Topmost item is [1, 'Sharine', 1234567890]
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 2
Popped item is [1, 'Sharine', 1234567890]
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 4
Stack empty
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 5
12A CREATE A CSV FILE WITH USER-ID AND PASSWORD
PROGRAM
import csv
fname = input ("Enter the file name: ")
with open (fname, 'w', newline = ' ') as f:
rec=[Link](f)
[Link](['USERNAME','PASSWORD'])
choice = 'y'
while [Link] () == 'Y':
uname = input ('Enter username: ')
pwd = input ('Enter password: ')
data = [uname, pwd]
[Link] (data)
choice = input ('Add records(y/n)? ')
OUTPUT
Enter the file name: [Link]
Enter username: sharine
Enter password: student
Add records(y/n)? y
Enter username: sheba
Enter password: student
Add records(y/n)? n
12B DISPLAY THE CONTENTS
PROGRAM
import csv
fname = input ("Enter the file name: ")
with open (fname, 'r') as f:
rec = [Link] (f)
for i in rec:
print (i)
OUTPUT
Enter the file name: [Link]
['USERNAME', 'PASSWORD']
['sharine', 'student']
['sheba', 'student']
12C SEARCH THE PASSWORD FOR GIVEN USERID
PROGRAM
import csv
fname = input ("Enter the file name: ")
with open (fname, 'r') as f:
rec = [Link] (f)
uname = input ("Enter username: ")
for i in rec:
if i[0] == uname:
print ('PASSWORD:', i[1])
break
else:
print ("User Name not found.")
OUTPUT
Enter the file name: [Link]
Enter username: sheba
PASSWORD: student
Enter the file name: [Link]
Enter username: student
User Name not found.
OUTPUT
Enter the previous EB reading (in Units): 1250
Enter the present EB reading (in Units): 2765
Units consumed: 1515
Electricity Bill Amount: 13087.0
OUTPUT
Enter the current year in 4 digits (yyyy): 2024
1. Days in a Month.
2. Months with 31 Days.
3. Months in Alphabetical Order.
4. Months Sorted in Days Order.
5. Exit
Enter your choice: 1
Enter the name of the first 3 letters of a month beginning with capital letter: Feb
The number of days in the month Feb is 29
Enter your choice: 2
The months with 31 days are:
Jan Mar May Jul Aug Oct Dec
Enter your choice: 3
The months in alphabetical order are:
Apr 30
Aug 31
Dec 31
Feb 29
Jan 31
Jul 31
Jun 30
Mar 31
May 31
Nov 30
Oct 31
Sep 30
Enter your choice: 4
The months sorted according to the no. of days are:
Feb 29
Apr 30
Jun 30
Sep 30
Nov 30
Jan 31
Mar 31
May 31
Jul 31
Aug 31
Oct 31
Dec 31
Enter your choice: 5
OUTPUT
Enter the phone number: 017-555-1212
017-555-1212 is valid
Enter the phone number: 0175551212
0175551212 is invalid
OUTPUT
Enter the first number: 491
Enter the second number: 278
The number that has minimum one's digit is 491
OUTPUT
Enter the file name: [Link]
We#are#class#XII#students#of#Pon#Vidyashram.
Our#school#is#located#in#Kolapakkam.
There#are#33#students#in#our#class.
OUTPUT
Enter the file name: [Link]
No. of Vowels is 36
No. of Consonants is 56
No. of Uppercase characters is 9
No. of Lowercase characters is 83
OUTPUT
Enter input file name: [Link]
Enter output file name: [Link]
Removed all lines that contain Character 'a' and written it to another file
OUTPUT
Enter the file name: [Link]
Enter roll no.: 1
Enter name: Sharine
Another Record (y/n)? y
Enter roll no.: 2
Enter name: Sheba
Another Record (y/n)? n
OUTPUT
Enter the file name: [Link]
Enter roll no.: 2
Search Successful.
Student Name: Sheba
Enter the file name: [Link]
Enter roll no.: 3
No such record found in the file.
OUTPUT
Enter the file name: [Link]
{'Roll No.': 1, 'Name': 'Sharine'}
{'Roll No.': 2, 'Name': 'Sheba'}
OUTPUT
Enter the file name: [Link]
Enter roll no.: 1
Enter name: Sharine
Enter marks: 95
Another Record (y/n)? y
Enter roll no.: 2
Enter name: Sheba
Enter marks: 85
Another Record (y/n)? n
OUTPUT
Enter the file name: [Link]
Enter roll no.: 2
Enter name: Sheba P
Enter marks: 90
Record successfully updated.
OUTPUT
_____
| |
|0 0 0|
| |
|0 0 0|
|_____|
Roll again (y/n)? Y
_____
| |
|0 |
| |
| 0|
|_____|
Roll again (y/n)? Y
_____
| |
|0 0|
| |
|0 0|
|_____|
Roll again (y/n)? Y
_____
| |
|0 0|
| 0 |
|0 0|
|_____|
Roll again (y/n)? Y
_____
| |
| |
| 0 |
| |
|_____|
Roll again (y/n)? Y
_____
| |
| |
|0 0 0|
| |
|_____|
Roll again (y/n)? N
OUTPUT
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 1
Enter client id:1
Enter the client name:Sharine
Enter phone number of client:1234567890
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 4
[1, 'Sharine', 1234567890]
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 3
Topmost item is [1, 'Sharine', 1234567890]
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 2
Popped item is [1, 'Sharine', 1234567890]
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 4
Stack empty
WELCOME TO CLIENT STACK
1. Push
2. Pop
3. Peek
4. Display stack
5. Exit
Enter your choice (1-5): 5
OUTPUT
Enter the file name: [Link]
Enter username: sharine
Enter password: student
Add records(y/n)? y
Enter username: sheba
Enter password: student
Add records(y/n)? n
OUTPUT
Enter the file name: [Link]
['USERNAME', 'PASSWORD']
['sharine', 'student']
['sheba', 'student']
OUTPUT
Enter the file name: [Link]
Enter username: sheba
PASSWORD: student
Enter the file name: [Link]
Enter username: student
User Name not found.