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

Computer Science Practical File 2

Uploaded by

aryanrana0701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Computer Science Practical File 2

Uploaded by

aryanrana0701
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Computer Science

Practical File

Made by:-
Name: Aryan Rana
Class: XII-A
CBSE Roll No.:
Session: 2024-25
Acknowledgement

I would like to express a deep sense of thanks and


gratitude to my teacher and guide Mrs Neha
Bhattacharjee for guiding me immensely through the
course of this practical file. Her constructive advice and
motivation are responsible for the successful
completion of this file.
My sincere thanks go to Mrs Sudha Sadangi, the Head
of School, for her coordination in extending every
possible support for the completion of this file.
I also thank my parents for their timely support and
motivation during the course of this file.

NAME : ARYAN RANA


CLASS- 12TH A
ROLL NO-
Certificate

This is to certify that Aryan Rana of Class XII-A


of ShreeRam World School, New Delhi has
completed and submitted his Computer Science
Practical file under my guidance. The report is
found worthy of acceptance as the final
submission.

INTERNAL EXTERNAL HEAD OF SCHOOL


EXAMINER EXAMINER
Index
S. No. Topic Teacher’s Sign
Python
1. Input three numbers and display the largest /
smallest number
2. Patterns using nested loops
3. Largest/smallest number in a list/tuple
4. Swap elements at the even location with the
elements at the odd location.
5. Search for a given element in the list/tuple.
6. Conditions on dictionary.
7. Looping statements.
8. Removing all duplicate values from a list.
9. Finding the sum of digits
10. Reading a file and changing separator
11. Counting vowels, uppercase etc.
12. Reading a file and transferring sentences with ‘a’
13. Reading and writing a binary file.
MySQL
# Basic MySQL commands
1. Two tables-based queries
2. Single table-based queries
MySQL and Python connectivity programs
1. Updating a table
2. Reading a table
3. Inserting into a table
Python Programs
1. Input three numbers and display the largest / smallest
number
Input:
a=int(input('Enter first number:'))
b=int(input('Enter second number:'))
c=int(input('Enter third number:'))
if (a==(c or b)) or (b==(a or c)) or (c==(a or b)):
print('Two or more numbers are same, re-enter three different
numbers.')
else:
if (a>(b and c)):
print(a,'is greater than',b,'and',c)
elif (b>(a and c)):
print(b,'is greater than',a,'and',c)
else:
print(c,'is greater than',b,'and',a)

Output:
2. Generate the following patterns using nested loops

i. Pattern-1
Input:
for a in range(1,6):
for i in range(1,a+1):
print('*',end='')
print()
ii. Pattern-2
Input:
for a in range(6,1,-1):
for i in range(1,a):
print(i,end='')
print()

iii. Pattern-3
Input:
for i in range(0,5):
for a in range(i+1):
print(chr(a+65),end='')
print()
Output:

3. Find the largest/smallest number in a list/tuple.


Input:
a=input("enter 'l' for list and 't' for tuple:")
b=input('enter the values of list/tuple: ')
if a=='l':
c=list(b)
m1=max(c)
n1=min(c)
print(m1,'is largest number and',n1,'is the smallest')
elif a=='t':
d=tuple(b)
m2=max(d)
n2=min(d)
print(m2,'is largest number and',n2,'is the smallest')
else:
print('invalid choice')
Output:

4. Input a list of numbers and swap elements at the even


location with the elements at the odd location.
Input:
a=list(input('enter a list: '))
print('the list is: ',a)
b=len(a)
if b%2==0:
b-=1
else:
b-=1
for i in range(0,b,2):
a[i],a[i+1]=a[i+1],a[i]
print('new list is:',a)
Output:
5. Input a list/tuple of elements, search for a given element
in the list/tuple.
Input:
a=input(“enter ‘l’ for list and ‘t’ for tuple: ”)
if a==‘l’:
b=list(input(‘enter the elements in list: ’ ))
print(‘the list is: ‘,b)
sub1=input(‘Enter the element to find: ’)
c=b.index(sub1)
print(sub1,’in the given list is at the index of: ’,c)
elif a==‘t’:
d=tuple(input(‘enter the elements in tuple: ’))
print(‘the tuple is: ‘,d)
sub2=input(‘enter the element to find: ’)
e=d.index(sub2)
print(sub2,’in the given tuple is at the index of: ’,e)
else:
print(‘invalid choice.’)
Output:
6. Create a dictionary with the roll number, name and
marks of n students in a class and display the names of
students who have marks above 75.
Input:
a=int(input('Total number of students: '))
s={}
for i in range(1,a+1):
n=input('Name: ')
r=int(input('Roll no.: '))
m=int(input('Marks: '))
s[r] = [n, m]
print(s)
for i in s:
if s[i][1]>=75:
print('student(s) who got 75 and above: ', s[i])
Output:
7. Write the following programs using functions:
● Print Fibonacci series
Input:
def fib(x):
a,b=0,1
l=[]
for i in range (x+1):
c=a+b
a=b
b=c
l.append(c)
return l
a=int(input('enter a no. upto which fibonacci series req: '))
s=fib(a)
print('fibonacci series is: ',s)

Output:
● Calculate the factorial of an integer
Input:
def fact(x):
f=1
for i in range(1,x+1):
f*=i
return f
a=int(input('enter a no. for factorial : '))
f=fact(a)
print('factorial of',a,'is: ',f)

Output:

● Test if the given string is a palindrome or not


Input:
def palindrome(x):
if x[::-1]==x:
print(x,'is palindrome.')
else:
print(x,'is not palindrome.')
a=input('enter a string to check: ')
palindrome(a)
Output:

8. WAP to remove all duplicate values from a list.


Input:
def remove_duplicates(lst):

unique_list = []
for item in lst:
if item not in unique_list:
unique_list.append(item)
return unique_list

my_list = [1, 2, 3, 2, 4, 1, 5]
result = remove_duplicates(my_list)
print(result)
Output:
9. WAP to calculate the sum of the digits of a random
three-digit number.
Input:
import random

def sum_of_digits(num):

sum_of_digits = 0
while num > 0:
digit = num % 10
sum_of_digits += digit
num //= 10
return sum_of_digits
random_number = random.randint(100, 999)

result = sum_of_digits(random_number)
print("Random number:", random_number)
print("Sum of digits:", result)

Output:
10. WAP to read a text file line by line and display each
word separated by a #.
Input:
def read_and_display_words(file_path):

try:
with open('C:/Users/ADMIN/OneDrive/Documents/Aryan
Python/FileWords.txt', 'r') as file:
for line in file:
words = line.split()
for word in words:
print(word, end='#')
print()
except FileNotFoundError:
print("File not found.")

file_path = "your_file.txt"
read_and_display_words(file_path)

Output:
11. WAP to read a text file and display the number of
vowels/consonants/uppercase/lowercase letters in the file.
Input:
def UpperLower(file_path):
vowels = 'aeiouAEIOU'
consonants =
'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'

vowel_count = 0
consonant_count = 0
uppercase_count = 0
lowercase_count = 0
try:
with open('C:/Users/ADMIN/OneDrive/Documents/Aryan
Python/FileWords.txt', 'r') as file:
for line in file:
for char in line:
if char in vowels:
vowel_count += 1
elif char in consonants:
consonant_count += 1
if char.isupper():
uppercase_count += 1
elif char.islower():
lowercase_count += 1
print("Vowel count:", vowel_count)
print("Consonant count:", consonant_count)
print("Uppercase count:", uppercase_count)
print("Lowercase count:", lowercase_count)

except FileNotFoundError:
print("File not found.")

file_path = "your_file.txt"
analyze_text_file(file_path)

Output:

12. WAP to find all the lines that contain the character 'a'
in a file and write to another file.
a = open('xyz.txt', 'r')
print(a.read())
a.seek(0)
b = open('new.txt', 'w')
for line in a:
if 'a' in line:
b.write(line)
a.close()
b.close()
x=open('new.txt','r')
print("Lines containing 'a' are: ",x.read())
Output:

13. WAP to create a binary file with names and roll


numbers. Search for a given roll number and display the
name. If not found, display appropriate message.
import pickle
a = open('students.dat', 'wb')

s= {1: 'A', 2: 'B', 3: 'C'}


pickle.dump(s, a)
a.close()
b = open('students.dat', 'rb')
c = pickle.load(b)
rn= int(input("Enter roll number to search: "))
if rn in c:
print(f"Name: {c[rn]}")

else:
print("Roll number not found.")
MY SQL:
1. CREATING DATABASE

2. SHOW DATABASES

3. CREATING TABLES

4. INSERT VALUES TO THE TABLE


5. DELETING A VALUE FROM TABLE

6. MODIFYING TABLE

7. COLUMN ALIASES
ALTER TABLE:

1. ADD A COLUMN TO TABLE

2. DELETE A COLUMN

3. CHANGING COLUMN NAME


TABLE BASED QUEIRES:
1. Consider the table club given below and write the output of SQL
queries that follow:

i) mysql> select employee id, name, jobid, jobtitle from


employee, job where job.jobid = employee.jobid;
ii) mysql>select name, sales, jobtitles from employee, job where
job.jobid = employee.jobid and sales >= 1300000;
iii) mysql> select name, jobtitles from employee, job where
job.jobid = employee.jobid and name like ‘%SINGH%’;

2. Predict the output of the following code:

Sol: a) mysql> select name from student where grade1 = “C” or


grade2 = “C”;
b) mysql> select distinct game from student;
c) mysql> select name, supw from students where name like
“A%”;
CONNECT MYSQL TO PYTHON:
AFTER WE CONNECT MYSQL TO OUR SPYDER USING
MYQSL.CONNECTOR() COMMAND WE CAN USE MYSQL
COMMANDS IN SPYDER AND HERE IS AN CODE FOR YOUR
REFERENCE:
1. MySQL connectivity program and updating table.
import mysql.connector as sqltor
mycon=sqltor.connect(host='localhost',user='root',passwd='srws@123
',database='SRWS',charset='utf8')
if mycon.is_connected():
print('connected')
c=mycon.cursor()
a=int(input('enter first value: '))
b=int(input('enter second value: '))
st="update employee set id={} where id={}".format(a,b)
c.execute(st)
mycon.commit()
c.execute('select * from employee')
data=c.fetchall()
print(data)
2. Reading data from a table using python
Input:
import mysql.connector as sql
import time
mycon = sql.connect(host='localhost', user='root',
passwd='srws@123', charset ='utf8',
database = 'srws')
if mycon.is_connected():
print('connected')
cursor = mycon.cursor()
cursor.execute("select * from student;")
data = cursor.fetchall()
for row in data:
print(row)
mycon.commit()

Output:
3. Inserting into table.
Input:
import mysql.connector as sql
import time
mycon = sql.connect(host='localhost', user='root', passwd='srws@123',
charset ='utf8', database = 'srws')
if mycon.is_connected():
print('connected')
cursor = mycon.cursor()
cursor.execute("select * from 12a;")
data = cursor.fetchall()
print("Before")
print('------------ ')
for row in data:
print(row)
print('------------ ')
no = int(input("no : "))
name = input("name : ")
birthdate = input("birthdate : ")
age = input("age : ")
perc=float(input('10th % : '))
string= 'insert into humans values({},"{}","{}","{}","{}");'
cursor.execute(string.format(no , name, birthdate , age, perc))
cursor.execute("select * from humans;")
data = cursor.fetchall()
print(' ')
print("After")
for row in data:
print(row)
print('------------')

Output:

You might also like