Class12_Python_Programs
Class12_Python_Programs
['hello everyone\n']
['hello', 'everyone']
hello# everyone#
'''Program-06: 06 Read a text file and display the number the number
of vowels/consonants/
uppercase/lowercase characters in the file.'''
vowels = 0
consonants = 0
lowercase = 0
uppercase = 0
for ch in content :
if (ch.isalpha()):
if (ch.islower( )):
lowercase += 1
elif (ch.isupper()):
uppercase +=1
ch = (ch.lower ( ))
if (ch in ['a', 'e', 'i', 'o', 'u']) :
vowels += 1
else :
consonants += 1
file.close ( )
hello everyone
Vowels are : 6
Consonants are : 7
Lowercase are : 13
Uppercase are : 0
'''Program - 7 (a):Create a binary file with name and roll no. Search
for a given roll no. and display
the name, if not found display appropriate message.'''
import pickle
file = open ("stud1.bin", "wb")
dic = { }
n = int (input ("Enter number of students: ") )
for key in range (n):
roll_no = int (input ("Enter roll no") )
name = input ("Enter name: ")
dic [roll_no] = { }
dic [roll_no] ["name"] = name
print(dic)
pickle.dump (dic, file)
file.close ( )
import pickle
#opening the dictionary using command line:
file = open ("stud1.bin", "rb")
d = pickle. load (file)
roll_no = int (input ("Enter roll no to search the students name :") )
for key in d :
if key == roll_no :
print ("The name of roll no" , key, "is", d[key])
if (roll_no not in d) :
print ("This roll no doesn’t exist")
break
file. close ( )
import pickle
file = open ("stud.bin", "rb+")
d = pickle.load(file)
roll_no = int (input ("Enter roll no to update marks") )
for key in d :
if key == roll_no:
m = int (input ("Enter the marks : ") )
d [roll_no] ["marks"] = m
print (d)
if roll_no not in d :
print ("roll no doesn’t exist")
break
pickle.dump(d,file)
file.close ( )
import random
while True :
choice = input ("Enter r to roll dice or press any other key to
quit")
if choice !='r':
break
n=random.randint(1,6)
print(n)
stack = []
print('Initial stack')
print(stack)
# pop() function to pop
# element from stack in
# LIFO order
print('\nElements popped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
Initial stack
['a', 'b', 'c']
import csv
fh=open("student.csv","w")
stuwriter=csv.writer(fh)
stuwriter.writerow(["rollno","name","marks"])
for i in range(2):
print("student record", (i+1))
rollno=int(input ("enter rollno:"))
name=input("Enter the name:")
marks=float(input("enter marks"))
stu_rec=[rollno, name, marks]
stuwriter.writerow(stu_rec)
fh.close()
student record 1
enter rollno: 1
Enter the name: Koushik
enter marks 34
student record 2
enter rollno: 2
Enter the name: Abhilash
enter marks 35
import csv
#user -id and password list
List=[["user1", "password1"],
["user2", "password2"],
["user3", "password3"],
["user4", "password4"],
["user5", "password5"]]
def Pop(stk) :
if isEmpty(stk) :
return "Underflow"
else :
item = stk.pop()
if len(stk) == 0:
top = None
else:
top = len(stk) - 1
return item
def Peek(stk):
if isEmpty(stk):
return "UnderFlow"
else:
top=len(stk)-1
return stk[top]
def Display(stk):
if isEmpty(stk):
print("stack empty")
else :
top =len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
#_main_
Stack=[]
top=None
while True :
print("STACK OPERATIONS")
print("1.PUSH")
print("2.POP")
print("3.Peek")
print("4.Display Stack")
print("5.Exit")
ch=int(input("Enter your choice(1-5):"))
if ch==1:
item = int(input("Enter item:"))
Push(Stack, item)
elif ch==2:
item = Pop(Stack)
if item =='underflow':
print("Underflow!stack is empty!")
else:
print("popped item is",item)
elif ch==3:
item = Peek(Stack)
if item=='underflow':
print("Underflow! Stack is empty!")
else :
print('topmost item is',item)
elif ch==4:
Display(Stack)
elif ch==5:
break
else:
print("Invalid choice!")
STACK OPERATIONS
1.PUSH
2.POP
3.Peek
4.Display Stack
5.Exit
Enter your choice(1-5):1
Enter item:2
STACK OPERATIONS
1.PUSH
2.POP
3.Peek
4.Display Stack
5.Exit
Enter your choice(1-5):2
popped item is 2
STACK OPERATIONS
1.PUSH
2.POP
3.Peek
4.Display Stack
5.Exit
Enter your choice(1-5):1
Enter item:2
STACK OPERATIONS
1.PUSH
2.POP
3.Peek
4.Display Stack
5.Exit
Enter your choice(1-5):1
Enter item:3
STACK OPERATIONS
1.PUSH
2.POP
3.Peek
4.Display Stack
5.Exit
Enter your choice(1-5):3
topmost item is 3
STACK OPERATIONS
1.PUSH
2.POP
3.Peek
4.Display Stack
5.Exit
Enter your choice(1-5):4
3 <-top
2
STACK OPERATIONS
1.PUSH
2.POP
3.Peek
4.Display Stack
5.Exit
Enter your choice(1-5):5
Deleted Successfully
Updated Successfully
if mycon.is_connected()== False:
print('Error')
cursor = mycon.cursor()
cursor.execute("select * from student")
data=cursor.fetchall()
count=cursor.rowcount
for row in data:
print(row)
mycon.close()
mycon= sqltor.connect(host =
"localhost",user="root",passwd="root",database="koushik")
if mycon.is_connected()== False:
print('Error')
cursor = mycon.cursor()
cursor.execute("insert into student values(10,'ramu',23)")
mycon.commit()
print("One Record Inserted Successfully!!")
mycon.close()
mycon= sqltor.connect(host =
"localhost",user="root",passwd="root",database="koushik")
if mycon.is_connected()== False:
print('Error')
cursor = mycon.cursor()
cursor.execute("delete from student where name = 'Abhi'")
mycon.commit()
print("Deleted Successfully")
mycon.close()
mycon= sqltor.connect(host =
"localhost",user="root",passwd="root",database="koushik")
if mycon.is_connected()== False:
print('Error')
cursor = mycon.cursor()
cursor.execute("update student set name='Vani' where sid=3")
mycon.commit()
print("Updated Successfully")
mycon.close()