You are on page 1of 20

ARMY PUBLIC SCHOOL

DHAULA KUAN

COMPUTER SCIENCE
PRACTICAL FILE

SUBMITTED BY:
Anant Bansal
12 c
Roll Number: 7
INDEX
S.no PROGRAM

1 Write a function to print first n rows of Pascal Triangle.


2 Write the definition of a function Alter(A) in python, which should change all the
odd numbers in the list to 1 and even numbers as 0.
3 Write a function CHANGEO ,which accepts an list of integer and an integer as
parameters and changes all elements by remainder of initial elements when
divided by that integer.
4 Write a program that reads character from a text file one by one. All lower case
characters get store inside the file LOWER, all upper case characters get stored
inside the file UPPER and all other characters get stored inside OTHERS.
5 Write and then call a function to request name and admn. no. from user and
insert the records into STU.dat.
6 Write and then call a function read() to searches the name of a student from
admn. no. from STU.dat file.
7 Write a function to input Product name, PID and its cost and insert the records in
PRODUCT.csv
8 Write a program to input product name and quantity and display the total
amount of products bought referring PRODUCT.csv
9 Write addclient(clientname) and remove() methods in python to add new client
and delete existing client from a list "clientdetail".
10 Write a function pop() which remove name from stack named "MyStack".

11 Write a program to show sorting of elements of a list step-by-step.


12 Write a program to show sum of diagonals in 2D- square matrix.
13 Write a program to find and display the sum of all integers which are ending with
3.
14 Write a program to show MySQL CONNECTIVITY for inserting two tuples in
table:"student" inside database: "class12".
15 Write a program to display all pc_components from database school.
16 Write a program to insert new records in table pc_components of database
school.
17 Write a program to delete a record where serial number is 7 from table
pc_components in database school.
18 Write a program to update processor name as Intel Pentium Dual-Core in table
pc_components in database school.
19 Write a program to create a mysql database named webserver.

PROGRAMS
1) Write a function to print first n rows of Pascal Triangle.

Code:

OUTPUT:
2) Write the definition of a function Alter(A) in python,
which should change all the odd numbers in the list to 1 and
even numbers as 0. Take input as space separated integers.

Code:

OUTPUT:

3) Write a function CHANGEO ,which accepts an list of


integer and an integer as parameters and changes all elements
by remainder of initial elements when divided by that integer.
Code:

OUTPUT:

4) Write a program that reads character from a text file


one by one. All lower case characters get store inside the file
LOWER, all upper case characters get stored inside the file
UPPER and all other characters get stored inside OTHERS.
Code:

Output:

Task performed.

The contents of TEST.txt were:


Hello World!!

Then the contents of LOWER.txt: “elloorld”


UPPER.txt : “HW”
OTHERS.txt: “ !! “
5) Write and then call a function to request name and
admn. no. from user and insert the records into STU.dat.
Code:
Output:

6) Write and then call a function read() to searches the


name of a student from admn. no. from STU.dat file which was
created in previous question and print the name. If no record
exists display appropriate output.
Code:

OUTPUT:

7) Write a function to input Product name, PID and its cost


and insert the records in PRODUCT.csv .

Code:
OUTPUT:

8) Write a program to input product name and quantity and


display the total amount of products bought referring
PRODUCT.csv in previous code.
Code:

Output:

9) Write addclient(clientname) and remove() methods in


python to add new client and delete existing client from a list
"clientdetail", considering them to act as push and pop
operations of the stack.
Code:

Output:

10) Write a function pop() which remove name from stack


named "MyStack".
Code:

11) Write a program to show sorting of elements of a list


step-by-step. Take input as space separated integers.

Code:

Output:

12) Write a program to show sum of diagonals in 2D- square


matrix. Take input as space separated integers.

Code:
Output:

13) Write a program to find and display the sum of all


integers which are ending with 3. Take input as space
separated values.

Code:

Output:
14) Write a program to show MySQL CONNECTIVITY for
inserting two tuples in table:"student" inside database:
"class12".

Code:

import mysql.connector as m
db=m.connect(host="localhost",user="Admin",passwd="1234",database="class12")
cursor_obj=db.cursor()
cursor_obj.execute("INSERT INTO student(admno,name,class,sec,rno,address)
VALUES({},'{}','{}','{}',{},'{}')".format(1236,"kamala",11,'a',43,"NARELA"))
cursor_obj.execute("INSERT INTO
student(admno,name,class,sec,rno,address)VALUES({},'{}','{}','{}',
{},'{}')".format(1237,"kishore",12,'c',3,"NARELA"))
db.commit()
print("record inserted")
cursor_obj.execute("select * from student")
data=cursor_obj.fetchall()
for row in data:
print(row)

Output:
15) Write a program to display all pc_components from
database school.

Code:

import mysql.connector as m
conn=m.connect(host=’localhost’, user=’Admin’, passwd=’1234’, database=’school’)
cur=conn.cursor()
cur.execute(“select * from pc_components”)
r=cur.fetchall()
print(“-“*58)
print(“-5s %-22s %-5s”%(‘| Sno’ ,’| Name’, ‘| Model’))
print(“-“*58)
for i in r:
print(“%-5s %-22s %-4s”%(‘| ‘+str(i[0]), ‘| ‘+str(i[1]), ‘| ‘+str(i[2])))
print(“-“*58)

Output:
------------------------------------------------------------------------
| Sno | Name | Model
------------------------------------------------------------------------
| 1 | processor | AMD Ryzen 9 3950X
------------------------------------------------------------------------
| 2 | motherboard | ASUS ROG X570
------------------------------------------------------------------------
| 3 | graphics card | NVIDIA Geforce RTX 2080 Ti
------------------------------------------------------------------------
| 4 | Cabinet | Corsair Obsedion 500 D
------------------------------------------------------------------------
| 4 | ssd | Samsung 980Evo 2TB
------------------------------------------------------------------------

16) Write a program to insert new records in table


pc_components of database school.

Code:

import mysql.connector as m
conn=m.connect(host=’localhost’, user=’Admin’, passwd=’1234’, database=’school’)
cur=conn.cursor()
e=”insert into pc_components values(%s ,%s, %s)”
v=[(6,’psu’,’Corsair AX1000i’ ), (7,’hard disk’,’Segate Ironwolf Pro 16TB’ )]
cur.executemany(e, v)
conn.commit()
cur.execute(“select * from pc_components”)
r=cur.fetchall()
print(“-“*58)
print(“%-5s %-22s %-5s”%(‘| Sno’ ,’| Name’, ‘| Model’))
print(“-“*58)
for i in r:
print(“%-5s %-22s %-4s”%(‘|’;+str(i[0]), ’| ‘+str(i[1]), ‘| ‘str(i[2])))
print(“-“*58)

Output:
------------------------------------------------------------------------
| Sno | Name | Model
------------------------------------------------------------------------
| 1 | processor | AMD Ryzen 9 3950X
------------------------------------------------------------------------
| 2 | motherboard | ASUS ROG X570
------------------------------------------------------------------------
| 3 | graphics card | NVIDIA Geforce RTX 2080 Ti
------------------------------------------------------------------------
| 4 | Cabinet | Corsair Obsedion 500 D
------------------------------------------------------------------------
| 4 | ssd | Samsung 980Evo 2TB
------------------------------------------------------------------------
| 6 | psu | Corsair AX1000i
------------------------------------------------------------------------
| 7 | hard disk | Segate Ironwolf Pro 16TB
------------------------------------------------------------------------

17) Write a program to delete a record where serial number


is 7 from table pc_components in database school.

Code:

import mysql.connector as m
conn=m.connect(host=’localhost’, user=’Admin’, passwd=’1234’, database=’school’)
cur=conn.cursor()
s=”delete from pc_components where sno=7”
cur.execute(s)
conn.commit()
cur.execute(“select * from pc_components”)
r=cur.fetchall()
print(“-“*58)
print(“%-5s %-22s %-5s”%(‘| Sno’ ,’| Name’, ‘| Model’))
print(“-“*58)
for i in r:
print(“%-5s %-22s %-4s”%(‘| ‘+str(i[0]), ‘| ‘+str(i[1]), ‘| ‘+str(i[2])))
print(“-“*58)

Output:
------------------------------------------------------------------------
| Sno | Name | Model
------------------------------------------------------------------------
| 1 | processor | AMD Ryzen 9 3950X
------------------------------------------------------------------------
| 2 | motherboard | ASUS ROG X570
------------------------------------------------------------------------
| 3 | graphics card | NVIDIA Geforce RTX 2080 Ti
------------------------------------------------------------------------
| 4 | Cabinet | Corsair Obsedion 500 D
------------------------------------------------------------------------
| 4 | ssd | Samsung 980Evo 2TB
------------------------------------------------------------------------
| 6 | psu | Corsair AX1000i
------------------------------------------------------------------------

18) Write a program to update processor name as Intel


Pentium Dual-Core in table pc_components in database school.

Code:

import mysql.connector as m
conn=m.connect(host=’localhost’, user=’Admin’, passwd=’1234’, database=’school’)
cur=conn.cursor()
cur.execute(‘update pc_components set Model=”Intel Pentium Dual-Core” where
name=”processor” ‘)
conn.commit()
cur.execute(“select * from pc_components”)
r=cur.fetchall()
print(“-“*58)
print(“%-5s %-22s %-5s”%(‘| Sno’ ,’| Name’, ‘| Model’))
print(“-“*58)
for i in r:
print(“%-5s %-22s %-4s”%(‘| ‘+str(i[0]), ‘| ‘+str(i[1]), ‘| ‘+str(i[2])))
print(“-“*58)

Output:
------------------------------------------------------------------------
| Sno | Name | Model
------------------------------------------------------------------------
| 1 | processor | Intel Pentium Dual-Core
------------------------------------------------------------------------
| 2 | motherboard | ASUS ROG X570
------------------------------------------------------------------------
| 3 | graphics card | NVIDIA Geforce RTX 2080 Ti
------------------------------------------------------------------------
| 4 | Cabinet | Corsair Obsedion 500 D
------------------------------------------------------------------------
| 4 | ssd | Samsung 980Evo 2TB
------------------------------------------------------------------------
| 6 | psu | Corsair AX1000i
------------------------------------------------------------------------
| 7 | hard disk | Segate Ironwolf Pro 16TB
------------------------------------------------------------------------

19) Write a program to create a mysql database named


webserver.

Code:
import mysql.connector as m
conn=m.connect(host="localhost",user="Admin",passwd="1234")
cur=conn.cursor()
cur.execute("create database webserver")
print("Successfully completed")

Output:

Successfully completed

You might also like