python lab_mannual
python lab_mannual
10.Implement Stack.
stack = []
def Push():
print("Enter element to Push:")
ele = input()
stack.append(ele)
def Pop():
if len(stack) == 0:
print("Stack is underflow")
else:
print("Popped element =", stack.pop())
def Display():
if len(stack) == 0:
print("Stack is underflow")
else:
print(stack, ":top")
while (1):
print("1. Push\n2. Pop\n3. Display\n4. Exit")
n = int(input("Enter your choice: "))
if n == 1:
Push()
elif n == 2:
Pop()
elif n == 3:
Display()
elif n == 4:
break
else:
print("Invalid input")
# Write to file
filePtr = open("d:\\abc.txt", "w")
filePtr.write(str)
filePtr.close()
def validate_email(email):
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.fullmatch(pattern, email)
def validate_phone(phone):
pattern = r'^[6-9]\d{9}$'
return re.fullmatch(pattern, phone)
def validate_password(password):
pattern = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[\W_]).{8,}$'
return re.fullmatch(pattern, password)
def validate_date(date):
pattern = r'^(0[1-9]|[12][0-9]|3[01])[-/](0[1-9]|1[012])[-
/](19|20)\d\d$'
return re.fullmatch(pattern, date)
def validate_ip(ip):
pattern = r'^((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(25[0-
5]|2[0-4]\d|1\d\d|[1-9]?\d)$'
return re.fullmatch(pattern, ip)
data_samples = {
"Email": "example.user123@gmail.com",
"Phone": "9876543210",
"Password": "Strong@123",
"Date": "06-06-2025",
"IP": "192.168.1.1"
}
print("Validation Results:")
print("Email valid:", bool(validate_email(data_samples["Email"])))
print("Phone valid:", bool(validate_phone(data_samples["Phone"])))
print("Password valid:",
bool(validate_password(data_samples["Password"])))
print("Date valid:", bool(validate_date(data_samples["Date"])))
print("IP address valid:", bool(validate_ip(data_samples["IP"])))
import sqlite3
conn = sqlite3.connect('vvvqc.db')
conn.execute('''CREATE TABLE IF NOT EXISTS EMPLOYEE (
ID INT NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL);''')
print("Table created")
conn.execute("INSERT INTO EMPLOYEE (ID, NAME, AGE) VALUES (1,
'Raju', 32);")
conn.execute("INSERT INTO EMPLOYEE (ID, NAME, AGE) VALUES (2,
'Ajay', 30);")
print("Records Inserted")
cursor = conn.execute("SELECT ID, NAME, AGE FROM EMPLOYEE")
for row in cursor:
print("ID =", row[0])
print("NAME =", row[1])
print("AGE =", row[2])
print("---------------")
conn.commit()
conn.close()
6. create a GUI using Tkinter module.
from tkinter import *
top = Tk()
top.geometry("400x250") # Set window size
Label(top, text="Name").place(x=30, y=50)
Label(top, text="Email").place(x=30, y=90)
Label(top, text="Password").place(x=30, y=130)
e1 = Entry(top)
e1.place(x=80, y=50)
e2 = Entry(top)
e2.place(x=80, y=90)
e3 = Entry(top, show='*')
e3.place(x=95, y=130)
# Add Buttonsubmit = Button(top, text="Submit")
submit.place(x=110, y=160)
top.mainloop()
print("enter 2 nos")
a=int(input())
b=int(input())
try:
div=a/b
except:
print("you are trying to divide a number by zero")
else:
print("division =",div)
finally:
print("this is division of 2 number program")
8. Drawing Line chart and bar chart using Matplotlib.
import numpy as np
import matplotlib.pyplot as plt
x = np.array([2, 4, 6, 8, 10])
y = np.array([2, 4, 6, 8, 10])
plt.plot(x, y, marker='o')
plt.title("Line Chart")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.grid(True)
plt.show()
import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23, 17, 35, 29, 12]
plt.bar(langs, students, color='skyblue')
plt.title("Students in Programming Languages")
plt.xlabel("Languages")
plt.ylabel("Number of Students")
plt.show()
9. Drawing Histogram and pie chart using Matplotlib.
import matplotlib.pyplot as plt
marks = [45, 67, 89, 70, 56, 90, 78, 88, 55, 60]
bins = [40, 50, 60, 70, 80, 90, 100]
plt.figure(1)
plt.hist(marks, bins, color='skyblue', edgecolor='black')
plt.title("Student Marks Histogram")
plt.xlabel("Marks Range")
plt.ylabel("Number of Students")
subjects = ['Math', 'Physics', 'Chemistry', 'Biology']
scores = [80, 70, 60, 90]
colors = ['gold', 'lightgreen', 'lightcoral', 'lightskyblue']
plt.figure(2)
plt.pie(scores, labels=subjects, colors=colors, autopct='%1.1f%%',
startangle=140)
plt.title("Subject-wise Score Distribution")
plt.axis('equal') # Makes the pie a circle
plt.show()
10.create array using Numpy and perform operation on array.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array elements are:\n", arr)
print("Number of dimensions:", arr.ndim)
x = np.arange(12)
y = np.reshape(x, (4, 3))
print("One dimensional array:", x)
print("Converted to 2D:\n", y)
s = slice(2, 7, 2)
print("After slicing:", x[s])
import pandas as pd
df1 = pd.DataFrame({
'name': ['AAA', 'BBB', 'CCC', 'DDD', 'EEE'],
'age': [10, 20, 30, 40, 50]
})
df2 = pd.DataFrame({
'name': ['FFF', 'GGG', 'HHH', 'III', 'JJJ'],
'age': [15, 25, 35, 45, 55]
})
with pd.ExcelWriter('sample.xlsx') as writer:
df1.to_excel(writer, sheet_name='Sheet1', index=False)
df2.to_excel(writer, sheet_name='Sheet2', index=False)
df1 = pd.read_excel('sample.xlsx', sheet_name='Sheet1')
df2 = pd.read_excel('sample.xlsx', sheet_name='Sheet2')
print(df1)
print(df2)