0% found this document useful (0 votes)
4 views

DOC CS FILE

Uploaded by

Vivek Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DOC CS FILE

Uploaded by

Vivek Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

PROGRAM 6

Q. Write a program to read a text file and print only the sum of all the
digits from the file.

fr = open('numbers.txt', 'r')
content = fr.readlines()
sm = 0

for line in content:


for i in line:
if i.isdigit() == True:
sm += int(i)

print("The sum is:", sm)

OUTPUT
PROGRAM 7

Q. Write a program to copy a file ‘source.txt’ to a file ‘target.txt’


except those lines starts with a letter ‘A’.

def filter(source, target):


Finput = open(source, "r")
Foutput = open(target, "w")
while True:
text=Finput.readline()
if len(text) == 0:
break
if text[0]=="A":
Foutput.write(text)

Finput.close()
Foutput.close()

source=input("Enter source file")


target=input("Enter target file")
print(filter(source, target))

OUTPUT
PROGRAM 8

Q. Write a program to create a function ‘STATISTICS()’ which will read a text file and
display the number of vowels, consonants, uppercase and lowercase letters in file.

def STATISTICS():
filename = input("Enter the file to check with proper extension: ").strip()
fr = open(filename, "r")
cons = set()
text = fr.read().split()

countV = 0
countC = 0
for letter in text:
if letter.isalpha():
if letter in "AEIOUaeiou":
countV += 1
else:
countC += 1

print("The number of Vowels is: ",countV,"\nThe number of consonants is: ",countC)

Ucase=0
for U in text:
if U.isupper():
Ucase+=1

Lcase=0
for L in text:
if L.islower():
Lcase+=1

print("The number of Lower Case letters are: ",Lcase,"\nThe number of Upper Case
letters are: ",Ucase)

STATISTICS()
OUTPUT
PROGRAM 9

Q. Program to get roll numbers and marks of the students of a class


and store these details in a file called ‘Marks.txt”.

fw = open("Marks.txt", "w")

while True:
ch=input("Do you want to enter student details(Y/N)")
if ch=='Y':
roll_no=input("Enter roll number of student")
name=input("Enter name of student:")
mark=input("Enter marks of student")
fw.write(roll_no)
fw.write(name)
fw.write(mark)
print("data written successfully")
else:
break

fw.close()
OUTPUT
PROGRAM 10

Q. Program to write roll number and name in a binary file


student.dat.

import pickle

fw=open("student.dat", "wb")

while True:
ch=input("do you want to enter student details(Y/N)")
if ch=='Y':
roll_no=int(input("Enter roll number of student "))
name=input("Enter name of student ")
data=[roll_no, name]
pickle.dump(data, fw)
print("data recorded sucessfully")
else:
break

fw.close()

OUTPUT
PROGRAM 11

Q. Program to create a CSV file by storing some information then


display all information on screen.

import csv

fw=open("student.csv", "w")
cw=csv.writer(fw)
cw.writerow(['RollNo', 'Name', 'Marks'])

for i in range(3):
print("Record of Student", (i+1))
roll=int(input("Enter roll number of student"))
name=input("Enter name of student")
marks=float(input("Enter marks of student"))
data=[roll,name,marks]
cw.writerow(data)

fw.close()

print("data written successfully")

fr=open("student.csv", "r")
cr=csv.reader(fr)
for row in cr:
print(row)

fr.close()
OUTPUT
PROGRAM 12

Q. Program to create a function to calculate factorial of a number


passed as an argument.

def Factorial(num):
res=1

for i in range(1, num+1):


res=res*i
return res

N=int(input("Enter a number"))
fac=Factorial(N)

print("The factorial of " , N , "is: " , fac)

OUTPUT
PROGRAM 13

Q. Write a function that takes two numbers and returns the number
that has minimum One’s digit.

def two_no(n1,n2):
rem1=n1%10
rem2=n2%10
if rem1<rem2:
return n1
elif rem1>rem2:
return n2

num1=int(input("Enter a number"))
num2=int(input("Enter another number"))

print(two_no(num1,num2))

OUTPUT
PROGRAM 14

Q. Program to implement a stack & perform & PUSH & POP


operation.

stack=[]
c='y'

while(c=='y'):
print("1 Push")
print("2 Pop")
choice=int(input("Enter your choice"))
if (choice==1):
N=input("Enter a number")
stack.append(N)
elif (choice==2):
if (stack==[]):
print(" Stack is empty")
else:
print("Deleted element is: " , stack.pop())
else:
print("Wrong input")
c=input("Do you want to continue(y/n)")
OUTPUT
PROGRAM 15

Q. Write Sql query to find the minimum, maximum, sum and average
of the marks in a student table.
PROGRAM 16

Q. Write Sql query to find the total number of customers from each
country in the table customer(customer ID, customer name, country)
using group by.
PROGRAM 17

Q. Write a program to connect Python with MySQL using database


connectivity and perform the following operations: Create a table
STUDENT in Database SCHOOL with following structure

Stu_no int Primary Key

Sname varchar(25)

Stream varchar(10)

Percent float(4,2)

After creating the following table insert a record in the table.


PROGRAM 18

Q. Write a function in python Push(Arr),Where Arr is the list of


numbers divisible by 5 into a stack implemented by using a list.
Display the stack if it has at least one element, otherwise display
appropriate message.

def Push(Arr):
stack = []
for num in Arr:
if num % 5 == 0:
stack.append(num)
if stack:
print("Stack elements are:")
for element in stack:
print(element)
else:
print("No elements divisible by 5 found.")

lst=input("enter list")
print(Push(lst))

OUTPUT
PROGRAM 19

Q. In a database, there are two tables given below: Table Employee


& Table Job

Write SQL queries for the following

(i) To display employee ids, names of employees, job ids with


corresponding jobtitles.

(ii) To display names of employees,sales and corresponding job


titles who have achieved sales more than 1300000.
(iii) To display names and corresponding job titles of those
employees who have Singh (anywhere) in there names.

(iv) Identify foreign key in the table.

JOBID

(v) Write SQL command to change the JOBID to 104 of the


Employee with ID as E4 in the table Employee .
PROGRAM 20

Q 20) In the given table ‘student’ write SQL queries to perform


following operations on Table : Student

(i) Add a new field (column name school_name )of datatype


varchar(50)

(ii) Update the table by updating school _name as ‘ABC Public


School’

(iii) Display the all the records by increasing order of class


(iv) Delete all the records where game is ‘basketball’
.

(v) Display total number of students of every game individually


PROGRAM 21

. Q 21) Consider the following tables. Write SQL commands for the
statements (i) to (v).

Table : SENDER
SenderID SenderName SenderAddress SenderCity
ND01 R Jain 2, ABC Appts New Delhi
MU02 H Sinha 12, Newtown MumbaI
MU15 S Jha 27/A, Park Street Mumbai
ND50 T Prasad 122-K, SDA New Delhi

Table :RECIPIENT
RecID SenderID RecName RecAddress RecCity
KO05 ND01 R Bajpayee 5, Central Avenue Kolkata
ND08 MU02 S Mahajan 116, A Vihar New Delhi
MU19 ND01 H Singh 2A, Andheri East Mumbai
MU32 MU15 P K Swamy B5, C S Terminus MumbaI
ND48 ND50 S Tripathi 13, B1 D, Mayur Vihar New Delhi

(i) To display the names of all Senders from Mumbai.


(ii) To display the RecID, SenderName, SenderAddress,
RecName, RecAddress for every Recipient.

(iii) To display Recipient details in ascending order of RecName.

(iv) To display number of Recipients from each City.

(v) To display the detail of recipients who are in Mumbai.


PROGRAM 22

Q. In the given table ‘student’ write SQL queries to perform following


operations.

i. To display all game names only once

ii. To display name of all the students whose names starts with ‘A’

iii. To display the names of students who are getting a grade ‘C’ in
either Game or Supw

iv. To insert the following record in table student


(16,9,’Bipluv’,’Cricket’,’A’,’Cooking’,’A’)
v. To add a column Wing varchar(10) in table student
PROGRAM 23

Q. Find output for SQL queries (i) to (iv), which are based on tables
TRANSPORT and TRIP.

Table : TRANSPORT

Table : TRIP

(i) SELECT SUM(KM) WHERE TRIP.TCODE>=104;


(ii) SELECT COUNT(*), TCODE FROM TRIP GROUP BY
TCODE HAVING count(*)>1;

(iii) SELECT DISTINCT(TCODE) FROM TRIP;

(iv) SELECT A.TCODE,NAME,TTYPE FROM TRIP A,


TRANSPORT B WHERE A.TCODE=B.TCODE AND
KM<90;
(v) (v) SELECT NAME, KM*PERKM FROM TRIP A,
TRANSPORT B WHERE A.TCODE=B.TCODE AND
A.TCODE=105;
PROGRAM 24

Q. Program to connect Python with MySQL using database


connectivity and perform the following operations: Display all records
or individual record of table STUDENT in Database SCHOOL with
following structure

Stu_no int Primary Key

Sname varchar(25)

Percent int

import mysql.connector as msql

db=msql.connect(host="localhost", user="root", passwd="1234", database="school")


mycrsr=db.cursor()

mycrsr.execute("SELECT * FROM STUDENT")


data=mycrsr.fetchall()

for row in data:


print(row)

OUTPUT
PROGRAM 25

Q. Program to connect python to mysql using database connectivity


and update Sname of table.

import mysql.connector as msql


db=msql.connect(host="localhost", user="root", passwd="1234", database="school")

mycrsr=db.cursor()

mycrsr.execute("UPDATE STUDENT SET Sname='Modi' WHERE Stu_No=100")


db.commit()
PROGRAM 26

Q. Program to connect python to mysql using database connectivity


and delete record of a student From table STUDENT in database
SCHOOL.

import mysql.connector as msql

db=msql.connect(host="localhost", user="root", passwd="1234", database="school")


mycrsr=db.cursor()

mycrsr.execute("DELETE FROM STUDENT WHERE Stu_No=103")


db.commit()

You might also like