personal file 2.0
personal file 2.0
personal file 2.0
( C.B.S.E )
SESSION: 2024-25
OF
COMPUTER SCIENCE (083)
PROGRAMS IN
CONCEPT OF PYTHON AND SQL
"YASH LATHER
Student of Class
XII-B COMMERCE
has successfully completed their
COMPUTERS Pracital File
On
"CONCEPTS OF PYTHON
AND SQL"
Under the guidence of
"MRS. ANITA SINGH"
OUTPUT
#4 Program to find if a string is a palindrome or not
Sol:
def isPalindrome(str):
for i in range(0, int(len(str)/2)):
if str[i] != str[len(str)-i-1]:
return False
return True
inputString = input("enter any string: ")
result = isPalindrome(inputString)
if (ans):
print("Yes, it is a palindrome")
else:
print("No, not a palindrome")
OUTPUT
#5 Program to print the terms of Fibonacci series to
a limit
Sol:
limit =int(input("enter the limit:"))
x=0
y=1
z=1
print("Fibonacci series \n")
print(x, y, end= " ")
while(z<= limit):
print(z, end=" ")
x=y
y=z
z=x+y
OUTPUT
#6 Program to perform linear search on list
Sol:
def linear_Search(list1, n, key):
for i in range(0, n):
if (list1[i] == key):
return i
return -1
list1 = [1 ,3, 5, 4, 7, 9, 7]
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
#7 Program to perform selection sort on list
Sol:
def selectionSort(array, size):
for i in range(size):
min_index = i
for j in range(i+ 1, size):
if array[j] < array[min_index]:
min_index = j
(array[i], array[min_index]) = (array[min_index], array[i])
arr = [-29, -29, 42, 59, 69, 49, 79]
size = len(arr)
selectionSort(arr, size)
print('The array after sorting in Ascending Order by selection sort is:')
print(arr)
OUTPUT
#8 Program to perform binary search on list
Sol:
arr = [1,2,3,4,5,6,7,8,9,10]
def binary_search(list1, n):
low = 0
high = len(list1) - 1
mid = 0
OUTPUT
#9 Creation of a text file using python
Sol:
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
file1.seek(0)
file1.seek(0)
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
OUTPUT
OUTPUT
#12 Count no. of uppercase and lowercase letters
Sol:
def count_case_in_file():
with open("demofile3.txt", 'r') as file:
content = file.read()
# Initialize counters
uppercase_count = sum(1 for char in content if
char.isupper())
lowercase_count = sum(1 for char in content if
char.islower())
try:
# Establish the connection
connection = mysql.connector.connect(
host='localhost',
database='demo_database',
user='root',
password=mysql_password
)
if connection.is_connected():
print("Connected to MySQL server")
# Create a cursor object
cursor = connection.cursor()
# SQL statement to create a table
create_table_query = """
CREATE TABLE EMP(
empno integer primary key,
ename varchar(25) not null,
salary float
);
"""
# Execute the query
cursor.execute(create_table_query)
print("Table 'employees' created successfully!")
except Error as e:
print("Error while connecting to MySQL", e)
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
OUTPUT
except Error as e:
print("Error while connecting to MySQL", e)
if connection.is_connected():
connection.rollback()
print("Transaction rolled back!")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
OUTPUT
# UPDATING RECORD(S) OF A TABLE USING
UPDATE
try:
# Establish the connection
connection = mysql.connector.connect(
host='localhost',
database='demo_database',
user='root',
password=mysql_password
)
if connection.is_connected():
print("Connected to MySQL server")
# Create a cursor object
cursor = connection.cursor()
# SQL statement to create a table
update_query = """
UPDATE EMP SET salary = salary +1000 WHERE
salary<100000;
"""
# Execute the query
cursor.execute(update_query)
# Commit the transaction
connection.commit()
print("Record updated successfully!")
except Error as e:
print("Error while connecting to MySQL", e)
if connection.is_connected():
connection.rollback()
print("Transaction rolled back!")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
OUTPUT
#DELETE RECORD(S) FROM A TABLE
try:
# Establish the connection
connection = mysql.connector.connect(
host='localhost',
database='demo_database',
user='root',
password=mysql_password
)
if connection.is_connected():
print("Connected to MySQL server")
# Create a cursor object
cursor = connection.cursor()
# SQL statement to create a table
delete_query = """
DELETE FROM EMP
WHERE salary < 90000;
"""
# Execute the query
cursor.execute(delete_query)
# Commit the transaction
connection.commit()
print("Record deleted successfully!")
except Error as e:
print("Error while connecting to MySQL", e)
if connection.is_connected():
connection.rollback()
print("Transaction rolled back!")
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection is closed")
OUTPUT