0% found this document useful (0 votes)
2 views28 pages

Practical File XII

The document is a practical file for Computer Science students at the Scottish International School, containing various Python programs and SQL queries. It includes programs for calculating factorials, sorting arrays, checking for palindromes, and managing databases for students, employees, books, orders, and patients. Additionally, it covers MySQL connectivity for inserting, fetching, updating, and deleting records in a database.

Uploaded by

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

Practical File XII

The document is a practical file for Computer Science students at the Scottish International School, containing various Python programs and SQL queries. It includes programs for calculating factorials, sorting arrays, checking for palindromes, and managing databases for students, employees, books, orders, and patients. Additionally, it covers MySQL connectivity for inserting, fetching, updating, and deleting records in a database.

Uploaded by

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

SCOTTISH INTERNATIONAL SCHOOL

(Panipat Road, Shamli)

Practical File
(COMPUTER SCIENCE)
(Subject Code: 083)

Submitted To: Submitted By:


Mr. Atul Kumar Verma Student Name:
Student Class:
Section:
Python
Programs
1. Write a Program to calculate and Print Factorial of a Number

def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
num = int(input("Enter a number: "))
print("Factorial of", num, "is", factorial(num))
2. Write a program to sort an array using Bubble Sort and print it.

def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
arr = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(arr)
print("Sorted array is:", arr)
3. Write a program to check the given number is Palindrome or not.

def is_palindrome(s):
return s == s[::-1]
s = input("Enter a string: ")
print("Is palindrome:", is_palindrome(s))
4. Write a Program to print Matrix Multiplication of two Matrix.

def matrix_multiplication(X, Y):


result = [[0]*len(Y[0]) for i in range(len(X))]
for i in range(len(X)):
for j in range(len(Y[0])):
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
return result
X = [[1, 2], [3, 4]]
Y = [[5, 6], [7, 8]]
print("Matrix multiplication is:", matrix_multiplication(X, Y))
5. Write a program to print Fibonacci Series upto n number.

def fibonacci(n):
a, b = 0, 1
series = []
while b <= n:
series.append(b)
a, b = b, a + b
return series
num = int(input("Enter a number: "))
print("Fibonacci series up to", num, "is", fibonacci(num))
6. Write a program to check the given number is Prime Number or Not.

def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
print("Is prime:", is_prime(num))
7. Write a program to check the given number is Armstrong Number or
Not.

def is_armstrong(n):
num_str = str(n)
power = len(num_str)
s = sum(int(digit)**power for digit in num_str)
return s == n
num = int(input("Enter a number: "))
print("Is Armstrong number:", is_armstrong(num))
8. Write a program to calculate Sum of Digits of given number and print it.

def sum_of_digits(n):
return sum(int(digit) for digit in str(n))
num = int(input("Enter a number: "))
print("Sum of digits:", sum_of_digits(num))
9. Write a program to calculate HCF and print it.

def hcf(a, b):


while b:
a, b = b, a % b
return a
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("HCF is:", hcf(num1, num2))
10. Write a program to calculate LCM and print it.

def lcm(a, b):


def hcf(a, b):
while b:
a, b = b, a % b
return a
return abs(a * b) // hcf(a, b)
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("LCM is:", lcm(num1, num2))
11. Write a program to convert Binary to Decimal and print it.

def binary_to_decimal(binary):
decimal = 0
for i in range(len(binary)):
decimal += int(binary[i]) * (2**(len(binary)-i-1))
return decimal
binary = input("Enter a binary number: ")
print("Decimal form is:", binary_to_decimal(binary))
12. Write a program Decimal to Binary Conversion and print it.

def decimal_to_binary(n):
return bin(n)[2:]
num = int(input("Enter a decimal number: "))
print("Binary form is:", decimal_to_binary(num))
13. Write a program to Counting Vowels in a String and print count.

def count_vowels(s):
vowels = "aeiouAEIOU"
return sum(1 for char in s if char in vowels)
s = input("Enter a string: ")
print("Number of vowels:", count_vowels(s))
14. Write a program to calculate Simple Interest and print it.

def simple_interest(principal, rate, time):


return (principal * rate * time) / 100
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time in years: "))
print("Simple Interest is:", simple_interest(principal, rate, time))
15. Write a program to Finding Maximum and Minimum in a List and print
it.

def find_max_min(lst):
return max(lst), min(lst)
lst = [int(x) for x in input("Enter numbers separated by space: ").split()]
max_value, min_value = find_max_min(lst)
print("Maximum value:", max_value, "Minimum value:", min_value)
SQL Query
Sets
1. Write a program to create a table and insert some entries and find Student
marks database
in SQL.

--create table
CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Mathematics INT,
Science INT,
English INT
);

-- Insert some records


INSERT INTO Students (ID, Name, Mathematics, Science, English)
VALUES
(1, 'Ravi', 80, 75, 85),
(2, 'Aisha', 90, 65, 75),
(3, 'Suresh', 70, 80, 70);

-- Query to fetch students with more than 75 marks in Mathematics.


SELECT Name FROM Students WHERE Mathematics > 75;

-- Query to find average marks in Science.


SELECT AVG(Science) AS Avg_Science FROM Students;
2. Write a query in database and create a table and insert some entries and
find the Employee salary in database.

-- Create table
CREATE TABLE Employees (
ID INT PRIMARY KEY,
Name VARCHAR(50),
Department VARCHAR(50),
Salary INT,
Years_of_Service INT
);

-- Insert some records


INSERT INTO Employees (ID, Name, Department, Salary,
Years_of_Service) VALUES
(1, 'John', 'IT', 60000, 3),
(2, 'Jane', 'HR', 50000, 6),
(3, 'Doe', 'Finance', 45000, 2);

-- Query to find employees with salary greater than 50000.


SELECT Name FROM Employees WHERE Salary > 50000;

-- Query to find average salary in each department.


SELECT Department, AVG(Salary) AS Avg_Salary FROM Employees
GROUP BY Department;
3. Write a query for making Library Management database:

-- Create table
CREATE TABLE Books (
BookID INT PRIMARY KEY,
Title VARCHAR(100),
Author VARCHAR(50),
Year INT
);

-- Insert some records


INSERT INTO Books (BookID, Title, Author, Year) VALUES
(1, 'Python Programming', 'John Doe', 2015),
(2, 'Data Structures', 'Jane Smith', 2018),
(3, 'Machine Learning', 'Alex Johnson', 2020);

-- Query to fetch books published after 2016.


SELECT Title FROM Books WHERE Year > 2016;

-- Query to update the year of a specific book.


UPDATE Books SET Year = 2022 WHERE Title = 'Python Programming';
4. Write query in Sql for making E-commerce Order Management database.

-- Create table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderAmount DECIMAL(10, 2),
OrderDate DATE
);

-- Insert some records


INSERT INTO Orders (OrderID, CustomerID, OrderAmount, OrderDate)
VALUES
(1, 101, 1500.50, '2025-01-01'),
(2, 102, 2500.00, '2025-01-02'),
(3, 103, 3000.75, '2025-01-03');

-- Query to fetch orders with amount greater than 2000.


SELECT OrderID, OrderAmount FROM Orders WHERE OrderAmount >
2000;

-- Query to calculate the total order amount by each customer.


SELECT CustomerID, SUM(OrderAmount) AS Total_Amount FROM
Orders GROUP BY CustomerID;
5. Write query to create Hospital Management database in SQL.

-- Create table
CREATE TABLE Patients (
PatientID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Disease VARCHAR(50)
);

-- Insert some records


INSERT INTO Patients (PatientID, Name, Age, Disease) VALUES
(1, 'Arun', 45, 'Flu'),
(2, 'Bhavna', 32, 'Diabetes'),
(3, 'Chirag', 29, 'Asthma');

-- Query to fetch patients older than 30.


SELECT Name FROM Patients WHERE Age > 30;

-- Query to count patients for each disease.


SELECT Disease, COUNT(*) AS Patient_Count FROM Patients GROUP
BY Disease;
Python-
MySQL
Connectivity
Programs
1. Insert Record into MySQL Table.

import mysql.connector

# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)

cursor = conn.cursor()

# Insert a record
query = "INSERT INTO Employees (ID, Name, Department, Salary,
Years_of_Service) VALUES (%s, %s, %s, %s, %s)"
values = (4, 'Sarah', 'Marketing', 55000, 4)
cursor.execute(query, values)

conn.commit()
print(cursor.rowcount, "record inserted.")

# Close the connection


cursor.close()
conn.close()
```
2. Fetch and Display Data from MySQL Table.

import mysql.connector

# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)

cursor = conn.cursor()

# Fetch data
query = "SELECT * FROM Employees"
cursor.execute(query)

result = cursor.fetchall()

# Display data
for row in result:
print(row)

# Close the connection


cursor.close()
conn.close()
3. Update Record in MySQL Table.

import mysql.connector

# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)

cursor = conn.cursor()

# Update a record
query = "UPDATE Employees SET Salary = %s WHERE ID = %s"
values = (60000, 2)
cursor.execute(query, values)

conn.commit()
print(cursor.rowcount, "record(s) updated.")

# Close the connection


cursor.close()
conn.close()
4. Delete Record from MySQL Table.

import mysql.connector

# Establish connection
conn = mysql.connector.connect(
host="localhost",
user="your_username",
password="your_password",
database="your_database"
)

cursor = conn.cursor()

# Delete a record
query = "DELETE FROM Employees WHERE ID = %s"
values = (3,)
cursor.execute(query, values)

conn.commit()
print(cursor.rowcount, "record(s) deleted.")

# Close the connection


cursor.close()
conn.close()

You might also like