Docsity

Computer Science – Practical File, Cheat Sheet of Computer science

Partial preview of the text

Download Computer Science – Practical File and more Cheat Sheet Computer science in PDF only on Docsity!

RYAN INTERNATIONAL SCHOOL

FARIDABAD

PRACTICAL FILE

PYTHON

All student's contribution This file describes and analyzes the Python and MySql programming languages,which includes Functions, Sorting, Searching , Random Module, Text file, Binary file, CSV files, Stacks and Database connectivity.

modification, search and display operation with Binary file. (^12) Write a menu based program to perform the operation on stack in python SQL Queries: 13 Queries using DISTINCT, BETWEEN, IN, LIKE, ISNULL, ORDERBY,GROUPBY,HAVING (^14) Queries for Aggregate functions- SUM(),AVG(),MIN(),MAX(),COUNT() 15 Write a^ program^ to^ connect Python with^ MySQL using database connectivity and display all databases name. 16 Write a^ program^ to^ connect Python with^ MySQL using database connectivity and create table student in students database. 17 Write a^ program^ to^ connect Python with^ MySQL using database connectivity and add data in table student of students database. 18 Write a^ program^ to^ connect Python with^ MySQL using database connectivity and update data in table student of students database. 19 Write a^ program^ to^ connect Python with^ MySQL using database connectivity and delete data from table student of students database. 20 Write a^ program^ to^ connect Python with^ MySQL using database connectivity and fetch data from table student of students database.

Practical No. 1 Objective Solution Output Write a python functions in (x,n) to calculate the value of sin(x) using its taylor series expansion upto n terms. #x^1/1!-x^3/3!+x^5/5!-x^7/7!...........x^n/n! x=int(input("Enter the value of x")) n=int(input("Enter the value of n")) sum= j= for i in range(1,n+1,2): fact= for k in range(1,i+1): fact=facti sum=sum+ ((xi )/fact )(-1)**j #how to make one value positive and another negative j=j+ print("sum of the series 'x^1/1!-x^3/3!+x^5/5!-x^7/7!...........x^n/n!'=",sum)

Practical No. 3 Objective Solution Output Write a program for binary search. list1=[5,8,7,6,9,4,3,2,1,11,89,0,23,16] list1.sort() print(list1) key=int(input("Enter the element to be searched")) low= high=len(list1)- 1 found=False while low<=high and not found: mid=(low+high)// if key==list1[mid]: found=True elif key>list1[mid]: low=mid+ else: high=mid- 1 if found==True: print("Key is found") else: print("Key is not found")

Practical No. 4 Objective Solution Output Write a program for bubble sort. l=[] n=int(input("Enter the number of terms in the list a")) for i in range (0,n): x=eval(input("Enter the number")) l.append(x) print (l) print("original list is:",l) n=len(l) for i in range(n): for j in range(0,n-i-1): #print("i and j",i,j) if l[j]>l[j+1]: l[j],l[j+1]=l[j+1],l[j] print("list after sorting:",l)

Output

Practical No. 6 Objective Solution Output Write a program to write those lines which have the character 'p' from one text file to another text file. fin=open("E:\book.txt","r") fout=open("E:\story.txt","a") s=fin.readlines() for j in s: if 'p' in j: fout.write(j) fin.close() fout.close()

Practical No. 8 Objective Solution Write a program to find the most common words in a file.

import collections

fin = open('D:\email.txt','r')

a= fin.read()

d={ }

L=a.lower().split()

for word in L:

word = word.replace(".","")

word = word.replace(",","")

word = word.replace(":","")

word = word.replace(""","")

word = word.replace("!","")

word = word.replace("&","")

word = word.replace("*","")

for k in L:

key=k

if key not in d:

count=L.count(key)

d[key]=count

n_print = int(input("How many most common words to print: "))

print("\nOK. The {} most common words are as follows\n".format(n_print))

word_counter = collections.Counter(d)

for word, count in word_counter.most_common(n_print):

print(word, ": ", count)

fin.close()

Output

Practical No. 10 Objective Solution Write a program to perform read and write operation with .csv file. import csv def createCSV1(): csvfile=open("student.csv","w",newline='') csvobj=csv.writer(csvfile) while True: Rno=int(input("Rno:")) Name=input("Name:") Marks=float(input("Marks:")) line=[Rno,Name,Marks] csvobj.writerow(line) ch=input("More(Y/N)?") if ch=="N": break csvfile.close() def createCSV2(): csvfile=open("student.csv","a",newline='') csvobj=csv.writer(csvfile) L=[] while True: Rno=int(input("Rno:")) Name=input("Name:") Marks=float(input("Marks:")) L.append([Rno,Name,Marks]) ch=input("More(Y/N)?") if ch=="N": break csvobj.writerows(L) csvfile.close()

def display(): csvfile=open("student.csv","r",newline='') csvobj=csv.reader(csvfile) for line in csvobj: print(line) csvfile.close() while True: choice=input("1.Create CSV file \n 2. Create CSV all lines \n 3.Display \n 4. quit \n Enter your choice(1,2,3,4) : ") if choice=="1": createCSV1() elif choice=="2": createCSV2() elif choice=="3": display() else: break

Practical No. 11 Objective Solution Write a program to perform write, delete, modification, search and display operation with Binary file. import sys import os import pickle dict={} def write_in_file(): myfile=open("student.dat","ab") no=int(input("Enter no of students")) for i in range(no): print("Enter student",i+1, "details") dict["roll"]=int(input("Enter the roll no")) dict["name"]=input("Enter the name") dict["eng"]=input("Enter the english marks") dict["phy"]=input("Enter the physics marks") dict["chem"]=input("Enter the chem marks") dict["maths"]=input("Enter the maths marks") dict["cs"]=input("Enter the cs marks") pickle.dump(dict,myfile) myfile.close() def display(): myfile=open("student.dat","rb") try: while True: data=pickle.load(myfile) print(data) except EOFError: pass myfile.close()

def search(): myfile=open("student.dat","rb") n=int(input("Enter roll number to be searched ")) found= try: while True: data=pickle.load(myfile) if data["roll"]==n: print ("Record found") print(data) found= break except EOFError: pass if found==0: print("Record not found") myfile.close() def update(): myfile=open("student.dat","rb") myfile1=open("temp.dat","ab") r=int(input("Enter the roll number to be updated")) try: while True: data=pickle.load(myfile) if data["roll"]==r: print("Enter student new details") data["roll"]=int(input("Enter the roll no")) data["name"]=input("Enter the name") data["eng"]=input("Enter the english marks") data["phy"]=input("Enter the physics marks") data["chem"]=input("Enter the chem marks") data["maths"]=input("Enter the maths marks") data["cs"]=input("Enter the cs marks") pickle.dump(data,myfile1) else: pickle.dump(data,myfile1) except EOFError: pass myfile.close() myfile1.close() os.remove("student.dat") os.rename("temp.dat","student.dat") myfile=open("student.dat","rb") try: while True: data=pickle.load(myfile) print(data)