Aakarshit Gupta Practical File 12th B

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

BRAHAMRISHI BAWRA SHANTI

VIDYA PEETH

NAME : ARYAN SHARMA


Aakarshit Gupta

CLASS : 12TH ‘B’

ROLL NO : 13
SUBJECT : COMPUTER SCIENCE

TEACHER NAME : MR.ANKUR


INDEX
1. Write a program to read a file line by line and display each word
separated by hash.

2. Write a program to read a file and display no. of vowels,


consonants, lowercase, and uppercase & characters in the file.

3. Remove all the lines that contain the character “a” in a file and
write it into another file.

4. Create a binary file with name & Roll no. Search for a given roll no.
& display the name, if not found display appropriate message.

5. Create a binary file with roll no. , name, marks. Input a roll no. &
update the marks.
6. Write a random no. generator that generates random no. between 1&
6 [must be integers] both included.

7. Write a program to implement a stack using list.

8. Write a program to perform insertion sort..

9. Write a program to perform bubble sort..

10. Write a program to perform linear search..

11. Write a program to perform all arithmetic operations taking


input from the user.

12. Write a program to find largest & second largest element from the
numeric list entered by the user.

13. Write a program to print the fibonacci series..

14. Write a program to print the pattern.

15. Write a program to check for palindrome.


SQL COMMANDS QUESTION

. Taking student table as reference, write sql commands


accordance with the task given:

1. Alter table to add new attribute/modify datatype/ draw attribute.


2. Update table to modify data
3. Order by to display in ascending order/descending order.
4. Descending order
5. Group by and find the minimum, maximum, sum, count and
average.
6. Max command
7. Min command
8. Sum command
9. Average command
10. Create two table employee and salary. Add data into these tables
and create 4 SQL queries including joins. .
11. Equi Join
12. Right outer join
13. Natural join
14. Left outer join
15. Integrate SQL &
16. (a) WAP to integrate python with sql using python. Retrieve those
record from student table whose date of birth is greater than
31/12/1995.
(b) Add a new table student to display those record whose name
start with ‘A’ and end with ‘n’.
17. WAP to display Name and employee of all employees whose job
assigned is Manager order employer code in descending order.
1. Write a program to read a file line by line and display
each word separated by hash.

#myfile.txt has following data


"""
welcome to python class
welcome to CBSE class 12 program 15
School programming
"""
fh=open(r"myfile.txt","r")
item=[]
a=""
while True:

a=fh.readline()

words=a.split()
for j in words:
item.append(j)
if a =="":
break
print("#".join(item))

Output:
2. Write a program to read a file and display no. of vowels,
consonants, lowercase, and uppercase & characters in the file.

Output:
3. Remove all the lines that contain the character “a” in a file and
write it into another file.

Output:
4. Create a binary file with name & Roll no. Search for a given roll no. & display the
name, if not found display appropriate message.

Output:
5. Create a binary file with roll no. , name, marks. Input a roll no. & update the marks.

import pickle
def Write():
f = open("Studentdetails.dat", 'wb')
while True:
r =int(input ("Enter Roll no : "))
n = input("Enter Name : ")
m = int(input ("Enter Marks : "))
record = [r,n,m]
pickle.dump(record,f)
ch = input("Do you want to enter more ?(Y/N)")
if ch in 'Nn':
break
f.close()
def Read():
f = open("Studentdetails.dat",'rb')
try:
while True:
rec=pickle.load(f)
print(rec)
except EOFError:
f.close()
def Update():
f = open("Studentdetails.dat", 'rb+')
rollno = int(input("Enter roll no whose marks you want to update"))
try:
while True:
pos=f.tell()
rec = pickle.load(f)
if rec[0]==rollno:
um = int(input("Enter Update Marks:"))
rec[2]=um
f.seek(pos)
pickle.dump(rec,f)
#print(rec)
except EOFError:
f.close()
Write()
Read()
Update()
Read()

Output:
6. Write a random no. generator that generates random no. between 1& 6 [must be
integers] both included.

7. Write a program to imp

Output:
7. Write a program to implement a stack using list.

Output:
8. Write a program to perform insertion sort..

def insertionSort(arr):
n = len(arr) # Get the length of the array

if n <= 1:
return # If the array has 0 or 1 element, it is already sorted, so return

for i in range(1, n): # Iterate over the array starting from the second element
key = arr[i] # Store the current element as the key to be inserted in the right position
j = i-1
while j >= 0 and key < arr[j]: # Move elements greater than key one position ahead
arr[j+1] = arr[j] # Shift elements to the right
j -= 1
arr[j+1] = key # Insert the key in the correct position

# Sorting the array [12, 11, 13, 5, 6] using insertionSort


arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print(arr)

Output:

Sorted array is:


[5, 6, 11, 12, 13]
9. Write a program to perform bubble sort..

# Creating a bubble sort function


def bubble_sort(list1):
# Outer loop for traverse the entire list
for i in range(0,len(list1)-1):
for j in range(len(list1)-1):
if(list1[j]>list1[j+1]):
temp = list1[j]
list1[j] = list1[j+1]
list1[j+1] = temp
return list1

list1 = [5, 3, 8, 6, 7, 2]
print("The unsorted list is: ", list1)
# Calling the bubble sort function
print("The sorted list is: ", bubble_sort(list1))

Output:

The unsorted list is: [5, 3, 8, 6, 7, 2]


The sorted list is: [2, 3, 5, 6, 7, 8]
10. Write a program to perform linear search..

def linear_Search(list1, n, key):

# Searching list1 sequentially


for i in range(0, n):
if (list1[i] == key):
return i
return -1

list1 = [1 ,3, 5, 4, 7, 9]
key = 7

n = len(list1)
res = linear_Search(list1, n, key)
if(res == -1):
print("Element not found")
else:
print("Element found at index: ", res) )

Output:

Element found at index: 4


11. Write a program to perform all arithmetic operations taking input from the
user.

num1 = int(input('Enter First number: '))


num2 = int(input('Enter Second number '))
add = num1 + num2
dif = num1 - num2
mul = num1 * num2
div = num1 / num2
floor_div = num1 // num2
power = num1 ** num2
modulus = num1 % num2
print('Sum of ',num1 ,'and' ,num2 ,'is :',add)
print('Difference of ',num1 ,'and' ,num2 ,'is :',dif)
print('Product of' ,num1 ,'and' ,num2 ,'is :',mul)
print('Division of ',num1 ,'and' ,num2 ,'is :',div)
print('Floor Division of ',num1 ,'and' ,num2 ,'is :',floor_div)
print('Exponent of ',num1 ,'and' ,num2 ,'is :',power)
print('Modulus of ',num1 ,'and' ,num2 ,'is :',modulus)

Output:
12. Write a program to find largest & second largest element from the numeric list
entered by the user.

def find_largest_and_second_largest(list1):
largest = list1[0]
second_largest = list1[1]
for i in range(2, len(list1)):
if list1[i] > largest:
second_largest = largest
largest = list1[i]
elif list1[i] > second_largest:
second_largest = list1[i]
return largest, second_largest

list1 = [10, 20, 4, 5, 6, 7, 8, 9]


largest, second_largest = find_largest_and_second_largest(list1)
print("Largest element is:", largest)
print("Second largest element is:", second_largest)

Output:

Largest element is: 20


Second largest element is: 10
13. Write a program to print the fibonacci series..

# Program to display the Fibonacci sequence up to n-th term

nterms = int(input("How many terms? "))

# first two terms


n1, n2 = 0, 1
count = 0

# check if the number of terms is valid


if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1

Output:

How many terms? 7


Fibonacci sequence:
0
1
1
2
3
5
8
14. Write a program to print the pattern.

# outer loop for ith rows


for i in range(0,6):
# inner loop for jth columns
for j in range(0,i+1):
char = chr(asciichr)
print(char,end="")
asciichr += 1
print()

Output:

A
BC
DEF
GHIJ
KLMNO
PQRSTU
15. Write a program to check for palindrome.

Num = int(input("Enter a value:"))


Temp = num
Rev = 0
while(num > 0):
dig = num % 10
revrev = rev * 10 + dig
numnum = num // 10
if(temp == rev):
print("This value is a palindrome
number!")
else:
print("This value is not a palindrome
number!")

Output:

Enter the value: 2551


This value is not a palindrome number!

Enter the value: 1221


This value is a palindrome number!
SQL COMMANDS QUESTION

Q1. Taking student table as reference, write sql commands


accordance with the task given:

1. Alter table to add new attribute/modify datatype/


draw attribute.
Ans.

Output
2. Update table to modify data

Output

3. Order by to display in ascending order/descending order.

Output
4. Descending order

5. Group by and find the minimum, maximum, sum, count


and average.

Max command

Min command.
Sum command

Average command

6. Create two table employee and salary. Add data into these
tables and create 4 SQL queries including joins.
.

Equi join
Right outer join

Natural join

Left outer join


7. Integrate sql and python

8. (a) WAP to integrate python with sql using python. Retrieve


those record from student table whose date of birth is greater
than 31/12/1995.

(b) Add a new table student to display those record whose


name start with ‘A’ and end with ‘n’.

Output
9. WAP to display Name and employee of all employees whose job assigned
is Manager order employer code in descending order.

You might also like