Computer Science With Pyhton Practical File
Computer Science With Pyhton Practical File
KOTTURPURAM, CHENNAI
def series1(n):
init1 = 8
init2 = 7
print(init1)
print(init2)
for i in range(3,n+1):
if(i%2==1):
init1 = init1 + 3
print(init1)
else:
init2 = init2 + 5
print(init2)
def sereis2(n):
mul = 1
term = 0
for i in range(n):
mul = mul + 2
print(term)
while(1):
print(" Menu")
print("3. Exit")
series1(n)
elif(ch==2):
sereis2(n)
elif(ch==3):
break
else:
print("Wrong Choice")
output
Menu
3. Exit
11
12
14
17
17
Menu
1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?
3. Exit
12
27
48
75
Menu
3. Exit
>>>
# PROGRAM 2
import math
def tarea(b,h):
a = 0.5 * b * h
return a
def carea(r):
a = math.pi * r *r
return a
def rarea(n,s):
a = (s * s * n) / ( 4 * math.tan(180/n))
return a
while(1):
print(" Menu")
print("4. Exit")
if(ch==1):
elif(ch==2):
elif (ch==3):
elif(ch==4):
break
else:
print("Wrong Choice")
output
Menu
1. Area of Triangle
2. Area of Circle
4. Exit
Menu
1. Area of Triangle
2. Area of Circle
4. Exit
Menu
1. Area of Triangle
2. Area of Circle
4. Exit
Menu
1. Area of Triangle
2. Area of Circle
4. Exit
>>>
# PROGRAM 3
import math
def palindrome(s):
rev = s[::-1]
if(rev == s):
else:
def countc(s,c):
c = s.count(c)
return c
def replacec(s,i):
c = s[i]
ns = s.replace(c,nc)
print(ns)
return ns
else:
while(1):
print(" Menu")
print("1. Palindrome")
print("4. Exit")
if(ch==1):
palindrome(s)
elif(ch==2):
if(len(c)==1):
else:
elif (ch==3):
elif(ch==4):
break
else:
print("Wrong Choice")
output
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter a character :o
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
Enter an index :1
myyzy
Menu
1. Palindrome
2. Number of Occurence
3. Replace character
4. Exit
>>>
# PROGRAM 4
def maximum(l):
m = l[0]
for i in range(len(l)):
if(l[i]>m):
m=l[i]
return m
def minimum(l):
m = l[0]
for i in range(len(l)):
if(l[i]<m):
m=l[i]
return m
def suml(l):
s=0
for i in range(len(l)):
s = s + l[i]
return s
while(1):
print(" Menu")
print("4. Exit")
m = maximum(l)
elif(ch==2):
m = minimum(l)
elif (ch==3):
s = suml(l)
elif(ch==4):
break
else:
print("Wrong Choice")
output
Menu
1. Maximum of list
2. Minimum of list
3. Sum of list
4. Exit
1. Maximum of list
2. Minimum of list
3. Sum of list
4. Exit
Menu
1. Maximum of list
2. Minimum of list
3. Sum of list
4. Exit
Menu
1. Maximum of list
2. Minimum of list
3. Sum of list
4. Exit
>>>
#PROGRAM 5
import math
def sin(x,n):
t=x
su=t
d=1
for i in range(n):
t=((-1)*t*x*x)/((d+1)*(d+2))
d=d+2
su=su+t
return su
def cos(x,n):
t=1
su=t
for i in range(1,n):
t=((-1)*t*x*x)/((i*(i+1)))
su=su+t
return su
while(1):
print('''menu
1. Sine
2. Cos
3. Exit''')
ch=int(input('enter choice'))
if ch==1:
x = float(input('enter the value of x'))
x = x * 3.14 / 180
elif ch==2:
x = x * 3.14 / 180
elif ch==3:
break
else:
print('invalid input')
output
menu
1. Sine
2. Cos
3. Exit
enter choice1
menu
1. Sine
2. Cos
3. Exit
enter choice2
menu
1. Sine
2. Cos
3. Exit
enter choice3
>>>
# PROGRAM 6
def fact(n):
if(n<2):
return 1
else:
return n * fact(n-1)
def sumn(n):
if (n==1):
return 1
else:
return n + sumn(n-1)
def fibo(n):
if(n==1):
return 0
elif(n==2)or(n==3):
return 1
else:
return fibo(n-1)+fibo(n-2)
def sumd(n):
if(n==0):
return 0
else:
d = n%10
n=n//10
return d + sumd(n)
while(1):
print('''menu
1. Factorial of a number
3. Fibonacci Series
5. Exit''')
ch=int(input('enter choice'))
if (ch==1):
n = int(input("Enter the number : "))
if(n>=0):
else:
elif (ch==2):
elif (ch==3):
for i in range(1,n-1):
x = fibo(i)
print(x)
elif (ch==4):
elif (ch==5):
break
else:
print('invalid input')
output
menu
1. Factorial of a number
3. Fibonacci Series
4. Sum of digits of a number
5. Exit
enter choice1
menu
1. Factorial of a number
3. Fibonacci Series
5. Exit
enter choice2
menu
1. Factorial of a number
3. Fibonacci Series
5. Exit
enter choice3
2
3
menu
1. Factorial of a number
3. Fibonacci Series
5. Exit
enter choice4
menu
1. Factorial of a number
3. Fibonacci Series
5. Exit
enter choice5
>>>
# PROGRAM 7
def palindrome(s):
if(len(s)<=1):
return True
if(s[0]==s[-1]):
return palindrome(s[1:-1])
return False
def lens(s):
if(s==""):
return 0
else:
return 1 + lens(s[1:])
def revs(s):
if len(s) == 0:
return
temp = s[0]
revs(s[1:])
print(temp, end='')
while(1):
print('''menu
1. Palindrome
2. Length of string
3. Reverse a string
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
if(palindrome(s)):
else :
print("The string is not a palindrome")
elif (ch==2):
elif (ch==3):
revs(s)
print()
elif (ch==4):
break
else:
print('invalid input')
output
menu
1. Palindrome
2. Length of string
3. Reverse a string
4. Exit
enter choice1
menu
1. Palindrome
2. Length of string
3. Reverse a string
4. Exit
enter choice1
menu
1. Palindrome
2. Length of string
3. Reverse a string
4. Exit
enter choice2
menu
1. Palindrome
2. Length of string
3. Reverse a string
4. Exit
enter choice3
noops
menu
1. Palindrome
2. Length of string
3. Reverse a string
4. Exit
enter choice4
>>>
# PROGRAM 8
def bsearch(arr,ele,s,e):
if s <= e:
mid = (e + s) // 2
if arr[mid] == ele:
return mid
else:
else:
return -1
def lsearch(arr,l,r,x):
if r < l:
return -1
if arr[l] == x:
return l
if arr[r] == x:
return r
return lsearch(arr, l+1, r-1, x)
while(1):
print('''menu
1. Linear Search
2. Binary Search
3. Exit''')
ch=int(input('enter choice'))
if (ch==1):
res = lsearch(l,0,len(l)-1,ele)
if(res == -1):
else:
elif (ch==2):
l.sort()
print(l)
res = bsearch(l,ele,0,len(l)-1)
if(res == -1):
else:
break
else:
print('invalid input')
output
menu
1. Linear Search
2. Binary Search
3. Exit
enter choice1
menu
1. Linear Search
2. Binary Search
3. Exit
enter choice2
menu
1. Linear Search
2. Binary Search
3. Exit
enter choice3
>>>
# PROGRAM 9
def character(file):
cc = 0
ch='1'
while(ch):
ch = file.read(1)
cc = cc + 1
def words(file):
cw = 0
ch='1'
while(ch):
ch = file.readline()
wd = ch.split()
cw = cw + len(wd)
def vowels(file):
cv = 0
ch='1'
while(ch):
ch = file.read(1)
if(ch in ('a','e','i','o','u')):
cv = cv + 1
def lines(file):
cl = 0
ch='1'
while(ch):
ch = file.readline()
cl = cl + 1
def digits(file):
cd = 0
ch='1'
while(ch):
ch = file.read(1)
if(ch.isdigit()==True):
cd = cd + 1
def spcharacter(file):
cs = 0
ch='1'
while(ch):
ch = file.read(1)
if(ch.isalnum()==False):
cs = cs + 1
file = open("read.txt","r")
while(1):
print('''menu
7. Exit''')
ch=int(input('enter choice'))
if (ch==1):
file.seek(0,0)
character(file)
elif (ch==2):
file.seek(0,0)
words(file)
elif (ch==3):
file.seek(0,0)
vowels(file)
elif (ch==4):
file.seek(0,0)
lines(file)
elif (ch==5):
file.seek(0,0)
digits(file)
elif (ch==6):
file.seek(0,0)
spcharacter(file)
elif (ch==7):
break
else:
print('invalid input')
output
menu
1. Count number of characters
7. Exit
enter choice1
menu
7. Exit
enter choice2
menu
7. Exit
enter choice3
menu
7. Exit
enter choice4
menu
7. Exit
enter choice5
menu
7. Exit
enter choice6
menu
7. Exit
enter choice7
>>> # PROGRAM 9
def words(file):
wdict = {}
ch='1'
while(ch):
ch = file.readline()
wd = ch.split()
for w in wd:
if(w in wdict):
wdict[w] = wdict[w] + 1
else:
wdict[w] = 1
def mmword(file):
max = 0
min = 100
ch='1'
while(ch):
ch = file.readline()
wd = ch.split()
for w in wd:
max = len(w)
mxword = w
if(len(w)<min):
min = len(w)
mnword = w
def uword(file):
ch='1'
while(ch):
ch = file.readline()
wd = ch.split()
for w in wd:
if(w[0].isupper()):
print(w)
file = open("files.txt","r")
while(1):
print('''menu
4. Exit''')
ch=int(input('enter choice'))
if (ch==1):
file.seek(0,0)
words(file)
elif (ch==2):
file.seek(0,0)
mmword(file)
elif (ch==3):
file.seek(0,0)
uword(file)
elif (ch==4):
break
else:
print('invalid input')
output
menu
4. Exit
enter choice1
menu
4. Exit
enter choice2
menu
4. Exit
enter choice3
Eid
Mubarak!
Sending
Eid.
Remember
In
In
In
May
Allah
menu
1. Number of occurance of each word
4. Exit
enter choice4
>>>
# PROGRAM 11
import pickle
def writefile(file):
data = admno,name,age
pickle.dump(data,file)
def readfile(file):
while(1):
try:
data = pickle.load(file)
print(data)
except EOFError :
return
def above(file):
try:
data = pickle.load(file)
if(data[2]>=cutoff):
print(data)
except EOFError :
return
def search(file):
flag = 0
while(1):
try:
data = pickle.load(file)
if(data[0]==admno):
print(data)
flag = 1
return flag
except EOFError :
return flag
file = open("student.dat","ab+")
while(1):
print('''menu
5. Exit''')
ch=int(input('enter choice'))
if (ch==1):
file.seek(0,0)
writefile(file)
elif (ch==2):
file.seek(0,0)
readfile(file)
elif (ch==3):
file.seek(0,0)
above(file)
elif (ch==4):
file.seek(0,0)
res = search(file)
if(res == 0):
elif (ch==5):
break
else:
print('invalid input')
Output
menu
5. Exit
enter choice1
menu
5. Exit
enter choice2
menu
5. Exit
enter choice3
menu
5. Exit
enter choice4
menu
5. Exit
enter choice5
>>>