Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views55 pages

COMPUTER SCIENCE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 55

INDEX

S. Contents
No.

Python Programs

1 Write a program in Python to input a number from the user and calculate if the given number is prime or
not.

2 Write a program in Python to input a string from the user and find out if the given string is palindrome or
not.

3 Write a program in Python, which inputs a list L of integers and displays the sum of all such integers from
the list L which end with the digit 3.

4 Write a program in Python, which input a list L of numbers and a number to be searched. If the number
exists, it is replaced by 0 and if the number does not exist, an appropriate message is displayed.

5 Write a Python program, which takes a dictionary Student as input, the dictionary Student contains
Name:(Phy,Chem,Math) as key:value pairs, program should display the average marks of all students
present in the dictionary.

6 Write a Python program to read a text file “POEM.TXT” and print the total number of vowels and
consonants separately present in the text file.

7 Write a Python code to find the size of the file “Notes.txt” in bytes, the number of lines, and number of
words and no. of character.

8 Write a Python program which reads the contents of a text file "BIOPIC.TXT" and displays the content of
the file with every occurrence of the word 'he' replaced by 'she'.

9 Write a Python program to count and display the number of lines starting with ‘A’ (Lower/Upper both
cases) present in the text file “Lines.txt”.

10 Write a Python program to count and display the number of lines that have exactly 5 words in it present
in the text file “Story.txt”.

11 Create a binary file “Student.Dat” with the name and roll number. Search for a given roll number and
display the name, if not found display appropriate message.

12 Create a binary file “Stud.dat” with roll number, name and marks. Input a roll number and update
details. Create a binary file with roll number, name and marks. Input a roll number and update details.

13 Write a program using the following functions :


A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].

1. Write a user defined function CreateFile() to input data for a record and add to Book.dat .

2. Write a function CountRec(Author) in Python which accepts the Author name as parameter and
count and return number of books by the given
Author are stored in the binary file “Book.dat”

14 Write a program to perform read and write


operation onto a “student.csv” file having fields
as roll number, name, stream and percentage.

15 Write a program to copy the data from “Data.csv” to “Temp.csv”.

16 Write a program to create a library on your name in python and import it in a program.

17 Write a program which adds any random five even numbers in a list that falls between the highest and
the lowest number. Both highest and lowest numbers are accepted from the user.

18 A list contains following record of customer:


[Customer_name, Room_Type]
Write a complete program using the following user defined function to perform given operations on the
stack named 'Hotel':
(a) Push_Cust(): To push customers' names of those who are staying in 'Delux' Room Type.
(b) Pop_Cust(): To pop the names of customers from the stack and display them. Also display
"Underflow" when there are no customers in the stack.

19 A school stores records of Class XII students using a list that contains multiple lists as its elements. The
structure of each such element is [Student_Name, Marks, MainSubject].
Crate a complete program using the user-defined functions to perform the operations as mentioned
below:
(a) Push_student(): To push the Student_Name and Marks of all those students, who have Science as
MainSubject, into a Stack StudentInfo
(b) Pop_student(): To delete all items (one at a time) from the stack StudentInfo in LIFO order and
display them. Also display "Empty Stack" when there are no items remaining in the stack.

20 Write a program to connect Python with MySQL using database connectivity and perform the following
operations on data in Database:
A) Create, B) Insert, C) Fetch, D) Update and E) Delete the data.

MySQL Queries

21 Consider the table ORDERS as given below

Note: The table contains many more records than shown here. Write the following queries:
(I) To display the total Quantity for each Product, excluding Products with total Quantity less than 5.
(II) To display the orders table sorted by total price in descending order.
(III) To display the distinct customer names from the Orders table.
(IV) Display the sum of Price of all the orders for which the quantity is null.

22 Rahul, who works as a database designer, has developed a database for a bookshop. This database
includes a table BOOK whose column (attribute) names are mentioned below: BCODE: Shows the unique
code for each book.
TITLE: Indicates the book’s title.
AUTHOR: Specifies the author’s name.
PRICE: Lists the cost of the book.
Table: BOOK

I. Write SQL query to display book titles in lowercase.


II. Write SQL query to display the highest price among the books.
III. Write SQL query to display the number of characters in each book title.
IV. Write SQL query to display the Book Code and Price sorted by Price in descending order.

23 Write SQL queries for (i) to (iv), which are based on the tables given below :
NOTE : All Dates are given in ‘YYYY-MM-DD’ format.
(i) To display total number of MALE and FEMALE Passengers
(ii) To display details of all Trains which Start from Pune Junction
(iii) To display details of all Passengers travelling in Trains whose TNO is 12030
(iv) To display the PNR, PNAME, GENDER and AGE of all Passengers whose AGE is above 50.

24 Write SQL queries for (i) to (iv), which are based on the tables.

i. To display details of all transactions of TYPE Deposit from Table TRANSACT.


ii. To display the ANO and AMOUNT of all Deposits and Withdrawals done in the month of October 2017
from table TRANSACT.
iii. To display the last date of transaction (DOT) from the table TRANSACT for the Accounts having ANO as
103.
iv. To display all ANO, ANAME and DOT of those persons from tables ACCOUNT and TRANSACT who have
done transactions less than or equal to 3000.

25 Write SQL queries for (i) to (iv), which are based on the tables :
(i) To display the CODE and NAME of all SALESPERSON having “I7” Item Type Code from the table
SALESPERSON.
(ii) To display all details from table SALESPERSON in descending order of SALARY.
(iii) To display the number of SALESPERSON dealing in each TYPE of ITEM.
(Use ITCODE for the same)
(iv) To display NAME of all the salespersons from the SALESPERSON table along with their corresponding
ITEMTYPE from the ITEM table.
PRACTICAL NO. -1

# Write a program in Python To input a number from the user and


calculate if the given number is prime or not .
OUTPUT:
PRACTICAL NO.-2

# Write a program in Python to input a string from the user and find out
if the given string is palindrome or not.
OUTPUT:
PRACITICAL NO.-3

# Write a program in Python, which inputs a list L of integers and


displays the sum of all such integers from the list L which end with the
digit 3.
OUTPUT:
PRACTICAL NO.- 4

# Write a program in Python, which input a list L of numbers and a


number to be searched. If the number exists, it is replaced by 0 and if
the number does not exist, an appropriate message is displayed.
OUTPUT:
PRACTICAL NO.- 5

# Write a Python program, which takes a dictionary Student as input, the


dictionary Student contains Name:(Phy,Chem,Math) as key:value pairs,
program should display the average marks of all students present in the
dictionary.
OUTPUT:
PRACTICAL NO.- 6

# Write a Python program to read a text file “POEM.TXT” and print the
total number of vowels and consonants separately present in the text
file.
OUTPUT:
PRACTICAL NO.- 7

# Write a Python code to find the size of the file “Notes.txt” in bytes, the
number of lines, and number of words and no. of character.
OUTPUT:
PRACTICAL NO.-8

# Write a Python program which reads the contents of a text file


"BIOPIC.TXT" and displays the content of the file with every occurrence
of the word 'he' replaced by 'she'.
OUTPUT:
PRACTICAL NO.- 9

# Write a Python program to count and display the number of lines


starting with ‘A’ (Lower/Upper both cases) present in the text file
“Lines.txt”.
OUTPUT:
PRACTICAL NO. - 10

# Write a Python program to count and display the number of lines that
have exactly 5 words in it present in the text file “Story.txt”.
OUTPUT:
PRACTICAL NO.- 11

# Create a binary file “Student.Dat” with the name and roll number.
Search for a given roll number and display the name, if not found display
appropriate message.
OUTPUT:
PRACTICAL NO.- 12

# Create a binary file “Stud.dat” with roll number, name and marks.
Input a roll number and update details. Create a binary file with roll
number, name and marks. Input a roll number and update details.

UPDATING STUDENT DETAILS -


OUTPUT:
PRACTICAL NO.- 13

# Write a program using the following functions :


A binary file “Book.dat” has structure [BookNo, Book_Name, Author,
Price].

3. Write a user defined function CreateFile() to input data for a


record and add to Book.dat .

4. Write a function CountRec(Author) in Python which accepts the


Author name as parameter and count and return number of books
by the given
Author are stored in the binary file “Book.dat”
OUTPUT:
PRACTICAL NO.- 14

# Write a program to perform read and write operation onto a


“student.csv” file having fields as roll number, name, stream and
percentage.

TO READ AND DISPLAY DATA FROM THE FILE-


OUTPUT:
PRACTICAL NO.- 15

# Write a program to copy the data from “Data.csv” to “Temp.csv”.


OUTPUT:
Data.csv (before running the program):

Temp.csv (after running the program):

· The program reads all the data from Data.csv.


· It then writes this exact data into Temp.csv.
· The content of Temp.csv will be identical to Data.csv after
running the program.
PRACTICAL NO. - 16

# Write a program to create a library on your name in python and import


it in a program.

SAVING IT AS mylibrary.py
OUTPUT:
PRACTICAL NO.- 17

# Write a program which adds any random five even numbers in a list
that falls between the highest and the lowest number. Both highest and
lowest numbers are accepted from the user.
OUTPUT:
PROGRAM NO.- 18

# A list contains following record of customer:


[Customer_name, Room_Type]
Write a complete program using the following user defined function to
perform given operations on the stack named 'Hotel':
(a) Push_Cust(): To push customers' names of those who are staying in
'Delux' Room Type.
(b) Pop_Cust(): To pop the names of customers from the stack and
display them. Also display "Underflow" when there are no customers in
the stack.
OUTPUT:
PRACTICAL NO.- 19

# A school stores records of Class XII students using a list that contains
multiple lists as its elements. The structure of each such element is
[Student_Name, Marks, MainSubject].
Crate a complete program using the user-defined functions to perform
the operations as mentioned below:
(a) Push_student(): To push the Student_Name and Marks of all those
students, who have Science as MainSubject, into a Stack StudentInfo
(b) Pop_student(): To delete all items (one at a time) from the stack
StudentInfo in LIFO order and display them. Also display "Empty Stack"
when there are no items remaining in the stack.
OUTPUT:
PRACTIAL NO. -20

# Write a program to connect Python with MySQL using database


connectivity and perform the following operations on data in Database:
A) Create, B) Insert, C) Fetch, D) Update and E) Delete the data.
OUTPUT:
PRACTICAL NO.- 21

# Consider the table ORDERS as given below

Note: The table contains many more records than shown here. Write the
following queries:
(I) To display the total Quantity for each Product, excluding Products
with total Quantity less than 5.
(II) To display the orders table sorted by total price in descending order.
(III) To display the distinct customer names from the Orders table.
(IV) Display the sum of Price of all the orders for which the quantity is
null.

QUERY-
(I) SELECT Product, SUM(Quantity) AS Total_Quantity
FROM ORDERS
GROUP BY Product
WHERE SUM(Quantity) >= 5;

(II) SELECT *, (Quantity * Price) AS Total_Price


FROM ORDERS
ORDER BY Total_Price DESC;

(III) SELECT DISTINCT (CustomerName)


FROM ORDERS;

(IV) SELECT SUM(Price) AS Total_Price


FROM ORDERS
WHERE Quantity IS NULL;
OUTPUT:
(I)
Product Total_Quantity
(no rows)

(II)
O_ID C_Name Product Quantity Total_price
1001 Jitender Laptop 1 12000
1002 Mustafa Smartphone 2 20000
1003 Dhwani Headphone 1 1500

(III)
C_Name
Jitender
Mustafa
Dhwani

(IV)
Total_Price
null
PRACTICAL NO. - 22

# Rahul, who works as a database designer, has developed a database


for a bookshop. This database includes a table BOOK whose column
(attribute) names are mentioned below: BCODE: Shows the unique code
for each book.
TITLE: Indicates the book’s title.
AUTHOR: Specifies the author’s name.
PRICE: Lists the cost of the book.
Table: BOOK

I. Write SQL query to display book titles in lowercase.


II. Write SQL query to display the highest price among the books.
III. Write SQL query to display the number of characters in each book
title.
IV. Write SQL query to display the Book Code and Price sorted by Price in
descending order.

QUERY-

(I) SELECT LOWER(TITLE) AS Lowercase_Title


FROM BOOK;

(II) SELECT MAX(PRICE) AS Highest_Price


FROM BOOK;

(III) SELECT TITLE, LENGTH(TITLE) AS Title_Length


FROM BOOK;

(IV) SELECT BCODE, PRICE


FROM BOOK
ORDER BY PRICE DESC;
OUTPUT:

(I)
Lowercase_Title
midnight’s children
the god of small things
a suitable boy
the white tiger
train to pakistan

(II)
Highest_Price
600

(III)
TITLE Title_lenght
midnight’s children 18
The god of small things 23
A suitable boy 15
The white tiger 15
train to pakistan 18

(IV)
B_Code Price
B003 600
B001 500
B002 450
B004 399
B005 350
PRACTICAL N0.-23

# Write SQL queries for (i) to (iv), which are based on the tables given
below :

NOTE : All Dates are given in ‘YYYY-MM-DD’ format.


(i) To display total number of MALE and FEMALE Passengers
(ii) To display details of all Trains which Start from Pune Junction
(iii) To display details of all Passengers travelling in Trains whose TNO is
12030
(iv) To display the PNR, PNAME, GENDER and AGE of all Passengers
whose AGE is above 50.

QUERY-
(I) SELECT GENDER, COUNT(*) AS Total FROM PASSENGER GROUP BY
GENDER;
(II) SELECT * FROM TRAIN WHERE STARTING = 'Pune Junction';
(III) SELECT * FROM PASSENGER WHERE TNO = 12030;
(IV) SELECT PNR, PNAME, GENDER, AGE FROM PASSENGER WHERE AGE
> 50;
OUTPUT:
(I)
Gender Total
Male 5
Female 4

(II)
TNO TNAME STATRT END

11096 Ahimsa express Pune junction Ahmedabad junction

1651 Pune Hbj special Pune junction Habibganj

(III)
PNR TNO PNAME GENDER AGE TRAVEL DATE
P004 12030 SK SAXENA MALE 42 2018-10-12
P005 12030 S SAXENA FEMALE 35 2018-10-12
P006 12030 P SAXENA FEMALE 12 2018-10-12
P008 12030 JK SHARMA MALE 65 2018-05-09
P009 12030 R SHARMA FEMALE 58 2018-05-09

(IV)
PNR PNAME GENDER AGE
P007 NS SINGH MALE 52
P008 JK SHARMA MALE 65
P009 R SHARMA FEMALE 58
PRACTICAL NO.- 24

# Write SQL queries for (i) to (iv), which are based on the tables.

i. To display details of all transactions of TYPE Deposit from Table


TRANSACT.
ii. To display the ANO and AMOUNT of all Deposits and Withdrawals
done in the month of October 2017 from table TRANSACT.
iii. To display the last date of transaction (DOT) from the table TRANSACT
for the Accounts having ANO as 103.
iv. To display all ANO, ANAME and DOT of those persons from tables
ACCOUNT and TRANSACT who have done transactions less than or equal
to 3000.

QUERY-
(I) SELECT * FROM TRANSACT
WHERE TYPE = 'Deposit';
(II) SELECT ANO, AMOUNT FROM TRANSACT
WHERE DOT BETWEEN '2017-10-01' AND '2017-10-31';
(III)SELECT MAX(DOT) AS LastTransactionDate FROM TRANSACT
WHERE ANO = 103;
(III) SELECT ACCOUNT.ANO, ACCOUNT.ANAME, TRANSACT.DOT
FROM ACCOUNT
INNER JOIN TRANSACT
ON ACCOUNT.ANO = TRANSACT.ANO
WHERE TRANSACT.AMOUNT <= 3000;
OUTPUT:
(I)
TRNO ANO AMOUNY TYPE DOT
T002 103 3000 DEPOSIT 2017-06-01
T004 103 1000 DEPOSIT 2017-10-22
T005 101 12000 DEPOSIT 2017-11-06

(II)
ANO AMOUNT
103 1000

(III)
LastTransitionDate
2017-10-22

(IV)
ANO ANAME DOT
T001 NIRJA SINGH 2017-12--21
T002 ALI REZA 2017-06-01
T003 ROHAN GUPTA 2017-05-12
T004 ALI REZA 2017-10-22
PRACTICAL NO.- 25

# Write SQL queries for (i) to (iv), which are based on the tables :

(i) To display the CODE and NAME of all SALESPERSON having “I7” Item
Type Code from the table SALESPERSON.
(ii) To display all details from table SALESPERSON in descending order of
SALARY.
(iii) To display the number of SALESPERSON dealing in each TYPE of
ITEM.
(Use ITCODE for the same)
(iv) To display NAME of all the salespersons from the SALESPERSON table
along with their corresponding ITEMTYPE from the ITEM table.

QUERY-
(I) SELECT CODE, NAME FROM SALESPERSON
WHERE ITCODE = 'I7';
(II) SELECT * FROM SALESPERSON
ORDER BY SALARY DESC;
(IV) SELECT ITCODE, COUNT(*) AS TotalSalesperson FROM SALESPERSON
GROUP BY ITCODE;
(V) SELECT SALESPERSON.NAME, ITEM.ITEMTYPE, ITEM.TURNOVER
FROM SALESPERSON
INNER JOIN ITEM
ON SALESPERSON.ITCODE = ITEM.ITCODE;
OUTPUT:
(I)
CODE NAME
1005 ANKOKHI RAJ
1004 TARANA SEN

(II)
CODE NAME SALARY ITCODE
1002 YOGRAJ SINHA 70000 15
1001 TANDEEP JHA 60000 12
1004 TARANA SEN 55000 17
1005 ANOKHI RAJ 50000 17
1003 TENZIN JACK 45000 12

(III)
IT CODE TotalSalesPerson
17 2
15 1
12 2

(IV)
NAME ITEMTYPE TURNOVER
TANDEEP JHA BAKERY 10090000
YOGRAJ SINGH STATIONARY 3400000
TENZIN JACK BAKERY 10090000
TARANA SEN HOSEIRY 6500000
ANOKHI RAJ HOSEIRY 6500000

You might also like