cs record file
cs record file
cs record file
CLASS XII
OUTPUT:
Enter the string: PYTHON PROGRAMMING
Entered string = PYTHON PROGRAMMING
Number of vowels in entered string = 4
OUTPUT:
Division = 8.0
3. Write a program to read a file book.txt print the contents of file along with
frequency of word “computer” in it.
f=open("book.txt","r")
L=f.readlines()
c=c1=0
print("Contents of file :")
for i in L:
print(i)
j=i.split()
for k in j:
if k.lower()=="computer":
c1=c1+1
print("*****FILE END*****")
print()
print("Number of times 'computer' in the file=",c1)
f.close()
OUTPUT:
Contents of file :
Its language constructs and object-oriented approach aim to help computer
programmers to write clear with logical code.
*****FILE END*****
f=open("Top.txt","r")
st=f.readlines()
c=0
print("Contents of file :")
for i in st:
print(i)
if i[0]=="A":
c+=1
print("\n*****FILE END*****")
print()
print("Number of lines starting with 'A' =",c)
OUTPUT:
A Python is an interpreted, high-level, general-purpose programming language.
Created by Guido van Rossum and first released in 1991.
A Python's design philosophy emphasizes code readability.
Its language constructs and object-oriented approach aim to help programmers
write clear, logical code.
*****FILE END*****
5. WAP to input a list of number and search for a given number using linear
search
OUTPUT:
Enter the list of numbers:[5,3,4,2,1]
Enter the number: 3
Element present
6. Write a function DigitSum() that takes a number and returns its digit sum
def DigitSum(n):
s=0
n=str(n)
for i in n:
s=s+int(i)
return s
n=int(input("Enter the number"))
print("Sum of digits =", DigitSum(n))
OUTPUT:
Enter the number69 Sum of digits = 15
7. Write a function Div3and5() that takes a 10 elements numeric tuple and return
the sum of elements which are divisible by 3 and 5.
def Div3and5(t):
s=0
for i in t:
if i%3==0 and i%5==0:
s=s+i
return s #main
l=[]
for i in range(3):
print("Enter the ",i+1,"th number of the tuple",end=" ",sep="")
e=int(input())
l.append(e)
t=tuple(l)
print("Entered tuple :",t)
print("Sum of numbers in tuple divisible by 3 and 5 =",Div3and5(t))
OUTPUT:
Enter the 1th number of the tuple
15
Enter the 2th number of the tuple
27
Enter the 3th number of the tuple
81
Entered tuple: (15, 27, 81)
Sum of numbers in tuple divisible by 3 and 5 = 15
8. Write a Program to enter the string and to check if it‟s palindrome or not using
loop.
def palin(s):
return s==s[::-1]
s=str(input("Enter the string data:"))
ans=palin(s)
if ans:
print("The given string data:",s,":is pallindrome")
else:
print("The given string data:",s,":is not a pallindrome")
OUTPUT:
Enter the string data:MALAYALAM
The given string data: MALAYALAM :is palindrome
9. Program to read the content of file and display the total number of consonants,
uppercase, vowels and lower case characters‟
f = open("file1.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels=['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
elif ch!=' ' and ch!='\n':
o+=1
print("Total Vowels in file :",v)
print("Total Consonants in file n :",c)
print("Total Capital letters in file :",u)
print("Total Small letters in file :",l)
print("Total Other than letters :",o)
f.close()
CONTENT O FILE:
India is my country I love python
Python learning is fun 123@
OUTPUT:
Total Vowels in file 16
Total Consonants in file n 30
Total Capital letters in file 3
Total Small letters in file 43
Total Other than letters 4
10. Program to store student‟s information like admission number, roll no., name
and marks in a dictionary, and display information on the basis of admission
number.
SCL=dict()
i=1
flag=0
n=int(input(“\nEnter number of entries:”))
while i<=n:
Adm=input(“\nEnter admission no of student:”)
nm=input(“Enter name of the student:”)
sec=input(“Enter class and section:”)
per=float(input(“Enter percentage of a student:”))
b=(nm,sec,per)
SCL[Adm]=b
i=i+1
l=SCL.keys()
for i in l:
print(“\nAdmno- “,I,” :”)
z=SCL[i]
print(“Name\t”,”class\t”,”per\t”)
for j in z:
print(j, end=”\t”)
OUTPUT:
Admno- 12001 :
Name class per
GEETHA G XII A 86.0
11. Program to read the content of file line by line and write it to another file
except for the lines contains “a” letter in it.
#Program to read line from file and write it to another line
#Except for those line which contains letter 'a'
f1 = open("file2.txt")
f2 = open("file2copy.txt","w")
OUTPUT :
## File Copied Successfully! ##
NOTE: After copy content of file2copy.txt
one two three four five six seven eight nine ten bye!
12. Program to create binary file to store Rollno and Name, Search any Rollno
and display name if Rollno found otherwise “Rollno not found”
import pickle
student=[ ]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
student.append([roll,name])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb')
student=[ ]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to search :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()
OUTPUT:
13. Program to create binary file to store Rollno, Name and Marks and update
marks of entered Rollno.
import pickle
student=[ ]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
roll = int(input("Enter Roll Number :"))
name = input("Enter Name :")
marks = int(input("Enter Marks :"))
student.append([roll,name,marks])
ans=input("Add More ?(Y)")
pickle.dump(student,f)
f.close()
f=open('student.dat','rb+')
student=[]
while True:
try:
student = pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to update :"))
for s in student:
if s[0]==r:
print("## Name is :",s[1], " ##")
print("## Current Marks is :",s[2]," ##")
m = int(input("Enter new marks :"))
s[2]=m
print("## Record Updated ##")
found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Update more ?(Y) :")
f.close()
OUTPUT:
Enter Roll Number :12001
Enter Name :Raj
Enter Marks :78
14. Program to create CSV file and store empno,name,salary and search any
empno and display name,salary and if not found appropriate message.
import csv
with open('myfile.csv',mode='a') as csvfile:
mywriter = csv.writer(csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
eno=int(input("Enter Employee Number "))
name=input("Enter Employee Name ")
salary=int(input("Enter Employee Salary :"))
mywriter.writerow([eno,name,salary])
print("## Data Saved... ##")
ans=input("Add More ?")
ans='y'
with open('myfile.csv',mode='r') as csvfile:
myreader = csv.reader(csvfile,delimiter=',')
while ans=='y':
found=False
e = int(input("Enter Employee Number to search :"))
for row in myreader:
if len(row)!=0:
if int(row[0])==e:
print("============================")
print("NAME :",row[1])
print("SALARY :",row[2])
found=True
break
if not found:
print("==========================")
print(" EMPNO NOT FOUND")
print("==========================")
ans = input("Search More ? (Y)")
OUTPUT:
Enter Employee Number 1001
Enter Employee Name kala
Enter Employee Salary :67000
## Data Saved... ##
Add More ?y
Enter Employee Number 1002
Enter Employee Name selva
Enter Employee Salary :70000
## Data Saved... ##
Add More ?n
Enter Employee Number to search :1001
============================
NAME : kala
SALARY : 67000
Search More ? (Y)y
Enter Employee Number to search :1002
============================
NAME : selva
SALARY : 70000
Search More ? (Y)n
15. Program to implement Stack in Python using List
def isEmpty(S):
if len(S)==0:
return True
else:
return False
def Push(S,item):
S.append(item)
top=len(S)-1
def Pop(S):
if isEmpty(S):
return "Underflow"
else:
val = S.pop()
if len(S)==0:
top=None
else:
top=len(S)-1
return val
def Peek(S):
if isEmpty(S):
return "Underflow"
else:
top=len(S)-1
return S[top]
def Show(S):
if isEmpty(S):
print("Sorry No items in Stack ")
else:
t = len(S)-1
print("(Top)",end=' ')
while(t>=0):
print(S[t],"<==",end=' ')
t-=1
print()
# main begins here
S=[] #Stack
top=None
while True:
print("**** STACK DEMONSTRATION ******")
print("1. PUSH ")
print("2. POP")
print("3. PEEK")
print("4. SHOW STACK ")
print("0. EXIT")
ch = int(input("Enter your choice :"))
if ch==1:
val = int(input("Enter Item to Push :"))
Push(S,val)
elif ch==2:
val = Pop(S)
if val=="Underflow":
print("Stack is Empty")
else:
print("Deleted item", val)
elif ch==3:
val = Peek(S)
if val=="Underflow":
print("Stack Empty")
else:
print("Top Item", val)
elif ch==4:
Show(S)
elif ch==0:
print("Bye")
break
OUTPUT:
OUTPUT:
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :1
Enter Employee Number :1 Enter Name :AMIT
Enter Department :SALES Enter Salary :9000
## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :1
Enter Employee Number :2 Enter Name :NITIN
Enter Department :IT Enter Salary :80000 ## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :2
EMPNO NAME DEPARTMENT SALARY
1 AMIT SALES 9000
2 NITIN IT 80000
18. Program to connect with database and delete the record of entered employee
number.
SQL COMMANDS
19. Consider the following table STUDENT. Write SQL commands for the following
statements.
i) Create table for student using the above fields and apply Primary Key for the suitable
field.
ii) Enter the data in the column using SQL Command.
iii) Add another column in the STUDENT table as Gender.
iv) Remove the tuples from STUDENT table that have Age is less than 17.
v) Modify the table to change the StudName as “Gokul” for the StudID=1003
OUTPUT:
OUTPUT: