Stack:
- Write a Python program to implement a stack using list.
stack=[]
c='y'
while (c=='y'):
print("1. PUSH")
print("2. POP")
print("3. Display")
choice=int(input("Enter your choices: "))
if (choice==1):
a=input("Enter any number: ")
stack.append(a)
elif (choice==2):
if (stack==[]):
print("Stack Empty")
else:
print("Deleted element is :", stack.pop())
elif (choice==3):
l=len(stack)
for i in range(l-1,-1,-1):
print(stack[i])
else:
print("wrong input")
c=input("Do you want to continue or not? ")
- Write a program to reverse a string using stack.
st=input("Enter the string to reverse ")
ls=list(st)
stack=[]
rvLs=[]
for i in ls:
stack.append(i)
for i in range(len(stack)):
rvLs.append(stack.pop())
str=""
for i in rvLs:
str+=i
print("The string entered: ",st)
print("The string reversed: ",str)
- Write a program to create a Stack for storing only odd numbers out of all the numbers entered by the user. Display the content of the Stack along with the largest odd number in the Stack.
val=input("Enter numbers seprated by space")
val=val.split(" ")
for i in range(len(val)):
val[i]=int(val[i])
stack=[]
for j in val:
if j%2!=0:
stack.append(j)
print("The stack of odd numbers are: ",stack)
ref=stack[0]
for i in stack:
if ref<i:
ref=i
print("The maximum of the odd stack is: ",ref)
SQL Queries:
- Create a student table with the student id, class, section, gender, name, dob, andmarks as attributes where the student id is the primary key.
CREATE TABLE student(studentid int(4) primary key, class char(2), section char(1), gender char(1), name varchar(20) dob date, marks decimal(5,2));
- Insert the following data in the student table
INSERT INTO student VALUES
(1101,'XI','A','M','Aksh','2005/12/23',88.21),
(1102,'XI','B','F','Moksha','2005/03/24',77.90),
(1103,'XI','A','F','Archi','2006/04/21',76.20),
(1104,'XI','B','M','Bhavin','2005/09/15',68.23),
(1105,'XI','C','M','Kevin','2005/08/23',66.33),
(1106,'XI','C','F','Naadiya','2005/10/27',62.33),
(1107,'XI','D','M','Krish','2005/01/23',84.33),
(1108,'XI','D','M','Ayush','2005/04/23',55.33),
(1109,'XI','C','F','Shruti','2005/06/01',74.33),
(1110,'XI','D','F','Shivi','2005/10/19',72.30);
- Delete the details of a student in the above table.
DELETE FROM student WHERE studentid=1105;
- Use the select command to get the details of the students with marks more than 80.
SELECT * FROM student WHERE marks>80;
- Find the min, max, sum, and average of the marks in a student marks table.
SELECT max(marks), min(marks), sum(marks), avg(marks) FROM student;
- Write a SQL query to order the (student ID, marks) table in descending order of the marks.
SELECT * FROM student ORDER BY marks DESC;
Python - SQL Connectivity.
- Write python code to connect to mysql databases.
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root")
if connection:
print("Connection Established")
else:
print("Connection failed")
print(connection)
connection.close()
- Write python code to create a table studentInf given below in mysql database.
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root", database="test")
if connection:
mycrr=connection.cursor()
mycrr.execute("CREATE TABLE studentInf(std_code varchar(5), Name varchar(20), school varchar(20), location varchar(20), course_id varchar(6))")
else:
print("Connection failed")
print(connection)
connection.close()
- Write a python progran to insert the following data in the studentInf table.
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root", database="test")
mycursor = connection.cursor()
mycursor.execute("INSERT INTO studentInf values('S01', 'Amit','Lpeak','Shillong','C001')")
mycursor.execute("INSERT INTO studentInf values('S02', 'Meenakshi','Aliganj','Lucknow','C001')")
mycursor.execute("INSERT INTO studentInf values('S03', 'Deep','Happy valley','Shillong','C002')")
mycursor.execute("INSERT INTO studentInf values('S04', 'Shikha','NIT','Agartala','C003')")
mycursor.execute("INSERT INTO studentInf values('S05', 'john','KV','Aizawl','C004')")
mycursor.execute("commit")
connection.close()
- Write a python program to display the data stored in studentInf table.
import mysql.connector as con
connection=con.connect(host="localhost", user="root", password="root", database="test")
mycursor = connection.cursor()
mycursor.execute("SELECT * from studentInf")
result=mycursor.fetchall()
for i in result:
print(i)
connection.close()