Computer Science Practical File 2
Computer Science Practical File 2
Practical File
Made by:-
Name: Aryan Rana
Class: XII-A
CBSE Roll No.:
Session: 2024-25
Acknowledgement
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:
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:
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:
else:
print("Roll number not found.")
MY SQL:
1. CREATING DATABASE
2. SHOW DATABASES
3. CREATING TABLES
6. MODIFYING TABLE
7. COLUMN ALIASES
ALTER TABLE:
2. DELETE A COLUMN
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: