Statistical Exp 8
Statistical Exp 8
Practical no.9
AIM: Implementation of database connectivity in python and perform CRUD
(Create, Retrieve, Update and Delete) operations on database.
SOURCECODE:
import sqlite3
conn = sqlite3.connect('test.db')
print("Opened database successfully")
# Step 1
query = """CREATE TABLE COMPANY_6 (ID INT PRIMARY KEY NOT NULL, NAME
TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL)"""
conn.execute(query)
print("Table created successfully")
# Step 2
query = """ INSERT INTO COMPANY_6(ID,NAME,AGE,ADDRESS,SALARY) VALUES
(1, 'Paul', 32, 'California', 21040.00 ) """
conn.execute(query)
conn.commit()
print("Records created successfully")
# Step 3
cursor = conn.execute("SELECT id, name, address, salary from COMPANY_6")
for row in cursor:
print("ID = ", row[0])
print("NAME = ", row[1])
print("ADDRESS = ", row[2])
print("SALARY = ", row[3])
print("Operation done successfully")
conn.close()
26
MMEC,MMDU
Harsh 11232640 3A1
# Step 4
cursor = conn.execute("DELETE FROM COMPANY_6 WHERE ID = 1")
print("Record delete successfully")
conn.close()
OUTPUT:
27
MMEC,MMDU
Harsh 11232640 3A1
Project no.1
Aim: To calculate Measure of Central Tendency and Dispersion.
SOURCE CODE:
def calculate_mean(data):
return sum(data) / len(data)
def calculate_median(data):
data.sort()
n = len(data)
if n % 2 == 1:
return data[n // 2]
else:
mid1 = data[n // 2 - 1]
mid2 = data[n // 2]
return (mid1 + mid2) / 2
def calculate_mode(data):
frequency = {}
for value in data:
if value in frequency:
frequency[value] += 1
else:
frequency[value] = 1
max_frequency = max(frequency.values())
modes = [value for value, freq in frequency.items() if freq == max_frequency]
if len(modes) == 1:
28
MMEC,MMDU
Harsh 11232640 3A1
return modes[0]
else:
return modes
def calculate_range(data):
return max(data) - min(data)
def calculate_variance(data):
mean = calculate_mean(data)
squared_diffs = [(x - mean) ** 2 for x in data]
return sum(squared_diffs) / len(data)
def calculate_std_dev(data):
variance = calculate_variance(data)
return variance ** 0.5
def main():
# Get user input
data = input("Enter a list of numbers separated by commas: ")
data = [float(x) for x in data.split(",")]
29
MMEC,MMDU
Harsh 11232640 3A1
std_dev = calculate_std_dev(data)
# Print results
print("Central Tendency:")
print(f"Mean: {mean}")
print(f"Median: {median}")
if isinstance(mode, list):
print(f"Mode: {mode} (multiple modes)")
else:
print(f"Mode: {mode}")
print("\nDispersion:")
print(f"Range: {range_}")
print(f"Variance: {variance}")
print(f"Standard Deviation: {std_dev}")
if __name__ == "__main__":
main ()
30
MMEC,MMDU
Harsh 11232640 3A1
OUTPUT/RESULT:
31
MMEC,MMDU
Harsh 11232640 3A1
Project no.2
Aim: To perform Correlation Analysis.
SOURCE CODE:
def calculate_mean(data):
return sum(data) / len(data)
def calculate_variance(deviation):
return sum([i**2 for i in deviation]) / len(deviation)
32
MMEC,MMDU
Harsh 11232640 3A1
33
MMEC,MMDU
Harsh 11232640 3A1
OUTPUT/RESULT:
34
MMEC,MMDU