[go: up one dir, main page]

0% found this document useful (0 votes)
97 views43 pages

Computer Science With Pyhton Practical File

The document contains the practical file of a 12th standard student from Unity Public School in Kotturpuram, Chennai. It includes 5 Python programs related to series, area calculations, strings, lists and mathematical functions like sine, cosine, factorial etc. Each program has the relevant code, input/output and menu driven options to choose the program to run.

Uploaded by

nubaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views43 pages

Computer Science With Pyhton Practical File

The document contains the practical file of a 12th standard student from Unity Public School in Kotturpuram, Chennai. It includes 5 Python programs related to series, area calculations, strings, lists and mathematical functions like sine, cosine, factorial etc. Each program has the relevant code, input/output and menu driven options to choose the program to run.

Uploaded by

nubaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

UNITY PUBLIC SCHOOL

KOTTURPURAM, CHENNAI

XIIth STD PRACTICAL FILE

SUBJECT: COMPUTER SCIENCE WITH PYTHON


SUB.CODE:083
# PROGRAM 1

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):

term = term + 3 * mul

mul = mul + 2

print(term)

while(1):

print(" Menu")

print("1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?")

print("2. Series 2 : 3, 12, 27, 48, 75, 108, ?")

print("3. Exit")

ch = int(input("Enter your choice:"))


if(ch==1):

n = int(input("Enter the number of terms (greater than 2) :"))

series1(n)

elif(ch==2):

n = int(input("Enter the number of terms :"))

sereis2(n)

elif(ch==3):

break

else:

print("Wrong Choice")

output

Menu

1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?

2. Series 2 : 3, 12, 27, 48, 75, 108, ?

3. Exit

Enter your choice:1

Enter the number of terms (greater than 2) :7

11

12

14

17

17

Menu
1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?

2. Series 2 : 3, 12, 27, 48, 75, 108, ?

3. Exit

Enter your choice:2

Enter the number of terms :5

12

27

48

75

Menu

1. Series 1 : 8, 7, 11, 12, 14, 17, 17, 22, ?

2. Series 2 : 3, 12, 27, 48, 75, 108, ?

3. Exit

Enter your choice:3

>>>

# 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("1. Area of Triangle")

print("2. Area of Circle")

print("3. Area of Regular polygon")

print("4. Exit")

ch = int(input("Enter your choice:"))

if(ch==1):

b = float(input("Enter the base of triangle : "))

h = float(input("Enter the height of triangle : "))

print("The area of triangle : ",tarea(b,h))

elif(ch==2):

r = float(input("Enter the radius of circle :"))

print("The area of circle :",carea(r))

elif (ch==3):

n= int(input("Enter the number of sides"))

s = float(input("Enter the dimension of side"))

print("The area of the polygon :",rarea(n,s))

elif(ch==4):

break

else:
print("Wrong Choice")

output

Menu

1. Area of Triangle

2. Area of Circle

3. Area of Regular polygon

4. Exit

Enter your choice:1

Enter the base of triangle : 5

Enter the height of triangle : 7

The area of triangle : 17.5

Menu

1. Area of Triangle

2. Area of Circle

3. Area of Regular polygon

4. Exit

Enter your choice:2

Enter the radius of circle :7

The area of circle : 153.93804002589985

Menu

1. Area of Triangle

2. Area of Circle

3. Area of Regular polygon

4. Exit

Enter your choice:3

Enter the number of sides3


Enter the dimension of side2

The area of the polygon : 9.373816866726925

Menu

1. Area of Triangle

2. Area of Circle

3. Area of Regular polygon

4. Exit

Enter your choice:4

>>>

# PROGRAM 3

import math

def palindrome(s):

rev = s[::-1]

if(rev == s):

print("The string is palindrome")

else:

print("The string is not a palindrome")

def countc(s,c):

c = s.count(c)

return c

def replacec(s,i):

c = s[i]

nc = input("Enter the character to replace :")


if(len(nc)==1):

ns = s.replace(c,nc)

print(ns)

return ns

else:

print("Enter only one character")

while(1):

print(" Menu")

print("1. Palindrome")

print("2. Number of Occurence")

print("3. Replace character")

print("4. Exit")

ch = int(input("Enter your choice:"))

if(ch==1):

s = input("Enter the string :")

palindrome(s)

elif(ch==2):

s = input("Enter the string :")

c = input("Enter a character :")

if(len(c)==1):

print("The character ",c," is in ",s, ",", countc(s,c), " times")

else:

print("Enter only one character")

elif (ch==3):

s = input("Enter the string :")


i = int(input("Enter an index :"))

print("The string after replacement is :",replacec(s,i))

elif(ch==4):

break

else:

print("Wrong Choice")

output

Menu

1. Palindrome

2. Number of Occurence

3. Replace character

4. Exit

Enter your choice:1

Enter the string :teacher

The string is not a palindrome

Menu

1. Palindrome

2. Number of Occurence

3. Replace character

4. Exit

Enter your choice:1

Enter the string :madam

The string is palindrome

Menu

1. Palindrome
2. Number of Occurence

3. Replace character

4. Exit

Enter your choice:2

Enter the string :unity public school

Enter a character :o

The character o is in unity public school , 2 times

Menu

1. Palindrome

2. Number of Occurence

3. Replace character

4. Exit

Enter your choice:3

Enter the string :maaza

Enter an index :1

Enter the character to replace :y

myyzy

The string after replacement is : myyzy

Menu

1. Palindrome

2. Number of Occurence

3. Replace character

4. Exit

Enter your choice:4

>>>

# 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("1. Maximum of list")

print("2. Minimum of list")

print("3. Sum of list")

print("4. Exit")

ch = int(input("Enter your choice:"))


if(ch==1):

l = list(map(int,input("\nEnter the numbers : ").strip().split()))

m = maximum(l)

print("Maximum of given list elements is :",m)

elif(ch==2):

l = list(map(int,input("\nEnter the numbers : ").strip().split()))

m = minimum(l)

print("Minimum of given list elements is :",m)

elif (ch==3):

l = list(map(int,input("\nEnter the numbers : ").strip().split()))

s = suml(l)

print("Sum of given list elements is :",s)

elif(ch==4):

break

else:

print("Wrong Choice")

output

Menu

1. Maximum of list

2. Minimum of list

3. Sum of list

4. Exit

Enter your choice:1

Enter the numbers : 23 45 78 100 199 27

Maximum of given list elements is : 199


Menu

1. Maximum of list

2. Minimum of list

3. Sum of list

4. Exit

Enter your choice:2

Enter the numbers : 1 23 7 9 67 0 12

Minimum of given list elements is : 0

Menu

1. Maximum of list

2. Minimum of list

3. Sum of list

4. Exit

Enter your choice:3

Enter the numbers : 4 3 2 9 7 6 5 1

Sum of given list elements is : 37

Menu

1. Maximum of list

2. Minimum of list

3. Sum of list

4. Exit

Enter your choice:4

>>>

#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

n = int(input('enter no. of terms'))

print("Sine value by user defined function : ",sin(x,n))

print("Sine value by built in function : ",math.sin(x))

elif ch==2:

x = float(input('enter the value of x'))

x = x * 3.14 / 180

n = int(input('enter no. of terms'))

print("Cos value by user defined function : ",cos(x, n))

print("Cos value by built in function : ",math.cos(x))

elif ch==3:

break

else:

print('invalid input')

output

menu

1. Sine

2. Cos

3. Exit

enter choice1

enter the value of x7

enter no. of terms5

Sine value by user defined function : 0.12180786830843254

Sine value by built in function : 0.12180786830843253

menu
1. Sine

2. Cos

3. Exit

enter choice2

enter the value of x9

enter no. of terms12

Cos value by user defined function : 0.9877260272276547

Cos value by built in function : 0.9877007947590948

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

2. Sum of n natural numbers

3. Fibonacci Series

4. Sum of digits of a number

5. Exit''')

ch=int(input('enter choice'))

if (ch==1):
n = int(input("Enter the number : "))

if(n>=0):

print("The factorial of the number is : ",fact(n))

else:

print("The factorial of negative number is not defined")

elif (ch==2):

n = int(input("Enter the number : "))

print("The sum of natural numbers upto ",n," is ",sumn(n))

elif (ch==3):

n = int(input("Enter the number of terms : "))

print("The Fibonacci Series is:")

for i in range(1,n-1):

x = fibo(i)

print(x)

elif (ch==4):

n = int(input("Enter the number : "))

print("The sum of digits is :",sumd(n))

elif (ch==5):

break

else:

print('invalid input')

output

menu

1. Factorial of a number

2. Sum of n natural numbers

3. Fibonacci Series
4. Sum of digits of a number

5. Exit

enter choice1

Enter the number : 7

The factorial of the number is : 5040

menu

1. Factorial of a number

2. Sum of n natural numbers

3. Fibonacci Series

4. Sum of digits of a number

5. Exit

enter choice2

Enter the number : 9

The sum of natural numbers upto 9 is 45

menu

1. Factorial of a number

2. Sum of n natural numbers

3. Fibonacci Series

4. Sum of digits of a number

5. Exit

enter choice3

Enter the number of terms : 9

The Fibonacci Series is:

2
3

menu

1. Factorial of a number

2. Sum of n natural numbers

3. Fibonacci Series

4. Sum of digits of a number

5. Exit

enter choice4

Enter the number : 45

The sum of digits is : 9

menu

1. Factorial of a number

2. Sum of n natural numbers

3. Fibonacci Series

4. Sum of digits of a number

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):

s = input("Enter the string")

if(palindrome(s)):

print("The string is a palindrome")

else :
print("The string is not a palindrome")

elif (ch==2):

s = input("Enter the string")

print("The length of the string ",s , "is ",lens(s))

elif (ch==3):

s = input("Enter the string")

print("The reverse of the string is ")

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

Enter the stringmalayalam

The string is a palindrome

menu

1. Palindrome

2. Length of string

3. Reverse a string
4. Exit

enter choice1

Enter the stringgod

The string is not a palindrome

menu

1. Palindrome

2. Length of string

3. Reverse a string

4. Exit

enter choice2

Enter the stringmasha allah

The length of the string masha allah is 11

menu

1. Palindrome

2. Length of string

3. Reverse a string

4. Exit

enter choice3

Enter the stringspoon

The reverse of the string is

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

elif arr[mid] > ele:

return bsearch(arr, ele,s, mid-1)

else:

return bsearch(arr, ele, mid + 1, e)

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):

l = list(map(int,input("Enter the elements of list :").strip().split()))

ele = int(input("Enter the element to search"))

res = lsearch(l,0,len(l)-1,ele)

if(res == -1):

print("The element not found")

else:

print("The element found at ",res)

elif (ch==2):

l = list(map(int,input("Enter the elements of list :").strip().split()))

ele = int(input("Enter the element to search"))

l.sort()

print(l)

res = bsearch(l,ele,0,len(l)-1)

if(res == -1):

print("The element not found")

else:

print("The element found at ",res)


elif (ch==3):

break

else:

print('invalid input')

output

menu

1. Linear Search

2. Binary Search

3. Exit

enter choice1

Enter the elements of list :4 5 6 12 45 19 23

Enter the element to search45

The element found at 4

menu

1. Linear Search

2. Binary Search

3. Exit

enter choice2

Enter the elements of list :12 34 5 6 7 99 1 7 9

Enter the element to search99

[1, 5, 6, 7, 7, 9, 12, 34, 99]

The element found at 8

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

print("The number of characters are :",cc)

def words(file):

cw = 0

ch='1'

while(ch):

ch = file.readline()

wd = ch.split()

cw = cw + len(wd)

print("The number of words are :",cw)

def vowels(file):

cv = 0

ch='1'

while(ch):

ch = file.read(1)
if(ch in ('a','e','i','o','u')):

cv = cv + 1

print("The number of vowels are :",cv)

def lines(file):

cl = 0

ch='1'

while(ch):

ch = file.readline()

cl = cl + 1

print("The number of lines are",cl)

def digits(file):

cd = 0

ch='1'

while(ch):

ch = file.read(1)

if(ch.isdigit()==True):

cd = cd + 1

print("The number of digits are :",cd)

def spcharacter(file):

cs = 0

ch='1'
while(ch):

ch = file.read(1)

if(ch.isalnum()==False):

cs = cs + 1

print("The number of special characters are :",cs)

file = open("read.txt","r")

while(1):

print('''menu

1. Count number of characters

2. Count number of words

3. Count number of vowels

4. Count number of lines

5. Count number of digits

6. Count number of special characters

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

2. Count number of words

3. Count number of vowels

4. Count number of lines

5. Count number of digits

6. Count number of special characters

7. Exit

enter choice1

The number of characters are : 27

menu

1. Count number of characters

2. Count number of words

3. Count number of vowels

4. Count number of lines

5. Count number of digits

6. Count number of special characters

7. Exit

enter choice2

The number of words are : 4

menu

1. Count number of characters

2. Count number of words

3. Count number of vowels

4. Count number of lines

5. Count number of digits

6. Count number of special characters

7. Exit
enter choice3

The number of vowels are : 0

menu

1. Count number of characters

2. Count number of words

3. Count number of vowels

4. Count number of lines

5. Count number of digits

6. Count number of special characters

7. Exit

enter choice4

The number of lines are 5

menu

1. Count number of characters

2. Count number of words

3. Count number of vowels

4. Count number of lines

5. Count number of digits

6. Count number of special characters

7. Exit

enter choice5

The number of digits are : 0

menu

1. Count number of characters

2. Count number of words

3. Count number of vowels

4. Count number of lines


5. Count number of digits

6. Count number of special characters

7. Exit

enter choice6

The number of special characters are : 4

menu

1. Count number of characters

2. Count number of words

3. Count number of vowels

4. Count number of lines

5. Count number of digits

6. Count number of special characters

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

for k,v in wdict.items():

print("The word ",k, "appears ",v, " times")

def mmword(file):

max = 0

min = 100

ch='1'

while(ch):

ch = file.readline()

wd = ch.split()

for w in wd:

if(len(w) > max):

max = len(w)

mxword = w

if(len(w)<min):

min = len(w)

mnword = w

print("The word with maximum length is : ",mxword)

print("The word with minimum length is : ",mnword)

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

1. Number of occurance of each word

2. Maximum and minimum length word

3. Word starting with uppercase

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

1. Number of occurance of each word

2. Maximum and minimum length word

3. Word starting with uppercase

4. Exit

enter choice1

The word Eid appears 1 times

The word Mubarak! appears 1 times

The word Sending appears 1 times

The word you appears 1 times

The word warm appears 1 times


The word wishes appears 1 times

The word and appears 2 times

The word happiness appears 1 times

The word on appears 1 times

The word the appears 1 times

The word occasion appears 1 times

The word of appears 1 times

The word Eid. appears 1 times

The word Remember appears 1 times

The word me appears 1 times

The word in appears 1 times

The word your appears 2 times

The word prayers. appears 1 times

The word In appears 3 times

The word every appears 3 times

The word smile appears 1 times

The word laughter; appears 1 times

The word silent appears 1 times

The word prayer appears 1 times

The word answered; appears 1 times

The word opportunity appears 1 times

The word that appears 1 times

The word knocks appears 1 times

The word door appears 1 times

The word – appears 1 times

The word May appears 1 times

The word Allah appears 1 times


The word bless appears 1 times

The word you! appears 1 times

menu

1. Number of occurance of each word

2. Maximum and minimum length word

3. Word starting with uppercase

4. Exit

enter choice2

The word with maximum length is : opportunity

The word with minimum length is : –

menu

1. Number of occurance of each word

2. Maximum and minimum length word

3. Word starting with uppercase

4. Exit

enter choice3

Eid

Mubarak!

Sending

Eid.

Remember

In

In

In

May

Allah

menu
1. Number of occurance of each word

2. Maximum and minimum length word

3. Word starting with uppercase

4. Exit

enter choice4

>>>

# PROGRAM 11

import pickle

def writefile(file):

admno = int(input("Enter the admission number :"))

name = input("Enter the name of student :")

age = int(input("Enter the age of student "))

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):

cutoff = int(input("Enter the cut off age :"))


while(1):

try:

data = pickle.load(file)

if(data[2]>=cutoff):

print(data)

except EOFError :

return

def search(file):

admno = int(input("Enter the admission number :"))

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

1. Write binary file

2. Read biary file

3. Above given age


4. Search by admission number

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):

print("Record not found")

elif (ch==5):

break

else:

print('invalid input')

Output

menu

1. Write binary file

2. Read biary file

3. Above given age


4. Search by admission number

5. Exit

enter choice1

Enter the admission number :123

Enter the name of student :babu

Enter the age of student 18

menu

1. Write binary file

2. Read biary file

3. Above given age

4. Search by admission number

5. Exit

enter choice2

(1000, 'sss', 16)

(123, 'babu', 18)

menu

1. Write binary file

2. Read binary file

3. Above given age

4. Search by admission number

5. Exit

enter choice3

Enter the cut off age :17

(123, 'babu', 18)

menu

1. Write binary file

2. Read binary file


3. Above given age

4. Search by admission number

5. Exit

enter choice4

Enter the admission number :1000

(1000, 'sss', 16)

menu

1. Write binary file

2. Read biary file

3. Above given age

4. Search by admission number

5. Exit

enter choice5

>>>

You might also like