Nothing Special   »   [go: up one dir, main page]

Class 12 CS Practical File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Program 1

Write Pyhton program to fine the largest and smallest of ‘n’ input numbers.
n=int(input("Enter number of element you want to input"))
x=int(input("Enter number:"))
max=min=x
for i in range(1,n):
x=int(input("Enter number:"))
if x>max:
max=x
elif x<min:
min=x
print("Maximum=",max)
print("Minimum=",min)
Program 2
Program 3

Program 4
Program 5

Program 6
Program 7

Program 8
Program 9

Program 10
Program 11

Program 12
Program 13

Program 14
Program 15

Program 16
Write a program to implement STACK Push, Pop, Peep and Display operations.
stack=[]
top=None
def push(st,ele):
st.append(ele)
top=len(st)-1
print("Top=",top)
print("Stack=",st)
def pop(st):
if len(st)==0:
print("\nStack underflow!!")
else:
print("deleted element=",st.pop())
if len(st)==0:
top=None
else:
top=len(st)-1
print("Top=",top)
print("Stack=",st)
def peek(st):
if len(st)==0:
print("Stack is empty!!")
else:
top=len(st)-1
print("Top element=",st[top])
print("Top=",top)
print("Stack=",st)
def traverse(st):
if len(st)==0:
print("Stack is empty!!")
else:
for i in range(len(st)-1,-1,-1):
print(st[i],end="->")
while True:
print("\nStack Opeartions\n1.Push\n2.Pop\n3.Peek\n4.Traversing\n5.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
ele=int(input("Enter an element"))
push(stack,ele)
elif ch==2:
pop(stack)
elif ch==3:
peek(stack)
elif ch==4:
traverse(stack)
elif ch==5:
break
print("Final values of stack=",stack)

Program 17
Write a program to implement a stack for these book-details (book_no, book_name). Each
item node of the stack contains two types of information- a book number and its name
Implement Push, Pop and display operations.

def push():
book_no=int(input("Enter Book number"))
book_name=input("Enter Book name")
ele=[book_no,book_name]
stack.append(ele)
top=len(stack)-1
print("Now stack is=",stack)
print("Top=",top)
def pop():
if len(stack)==0:
print("Stack Underflow!!")
else:
print("\nDeleted element=",stack.pop())
top=len(stack)-1
print("Now stack is=",stack)
print("Top=",top)
def peek():
if len(stack)==0:
print("Stack is empty!!")
else:
top=len(stack)-1
print("Top most element=",stack[top])
def traverse():
if len(stack)==0:
print("Stack is empty!!")
else:
for i in range(len(stack)-1,-1,-1):
print(stack[i],end=",")
#Main Segment
stack=[]
top=None
while True:
print("Stack Operations:\n1.PUSH\n2.POP\n3.PEEK\n4.TRAVERSE\n5.Exit")
ch=int(input("Enter your choice:"))
if ch==1:
push()
elif ch==2:
pop()
elif ch==3:
peek()
elif ch==4:
traverse()
else:
break
print("Finally stack=",stack)
print("Top=",top)
Program 18
Program 19
Program 20
Program 21
Program 22
Program 23
Program 24
Program 25
SQL queries to implement Natural Join
When we combine rows of two or more tables based on a common column between them, this
operation is called joining. A natural join is a type of join operation that creates an implicit
join (Automatically combined) by combining tables based on columns with the same name
and data type.

Points to remember:
o There is no need to specify the column names to join.
o The resultant table always contains unique columns.
o It is possible to perform a natural join on more than two tables.
o Do not use the ON clause.

Syntax:
SELECT [column_names | *]
FROM table_name1
NATURAL JOIN table_name2;

Natural Join Example

Table descriptions:
In this image, we can see that column names id and account and its data types are the same that fulfil the
natural join criteria. Hence we can use natural join on them but this is not always possible to have primary key
with the same name thus it will be done on the basis of some other common field i.e. account in the above
example.

Execute the below statement for joining tables using natural join:

mysql> SELECT cust. customer_name, bal.balance


FROM customer AS cust
NATURAL JOIN balance AS bal;

We will get the following result:

Now, we will use (*) in the place of column names as follows:

mysql> SELECT * FROM customer NATURAL JOIN balance;


if we use the asterisk (*) in the place of column names, then the natural join automatically searches the same
column names and their data types and join them internally. Also, it does not display the repeated columns in the
output. Hence, we should get the below output after executing the above statement:

Natural Join with WHERE Clause

mysql> SELECT cust. customer_name, bal.balance


FROM customer AS cust
NATURAL JOIN balance AS bal
WHERE bal.balance > 50000;

Program 26
SQL queries to implement Equi Join

The process is called joining when we combine two or more tables based on some common columns and a join
condition. An equijoin is an operation that combines multiple tables based on equality or matching
column values in the associated tables.
We can use the equal sign (=) comparison operator to refer to equality in the WHERE clause. This joining
operation returns the same result when we use the JOIN keyword with the ON clause and then specifying the
column names and their associated tables.

Equijoin is a classified type of inner join that returns output by performing joining operations from two tables
based on the common column that exists in them. This join returns only those data that are available in both
tables based on the common primary field name. It does not display the null records or unmatchable data into the
result set.

Points to remember:
o There is no need to be the same column names.

o The resultant result can have repeated column names.

o We can also perform an equijoin operation on more than two tables.

Syntax:
The following are the basic syntax that illustrates the equijoin operations:

SELECT column_name (s)


FROM table_name1, table_name2, ...., table_nameN
WHERE table_name1.column_name = table_name2.column_name;
OR
SELECT (column_list | *)
FROM table_name1
JOIN table_name2
ON table_name1.column_name = table_name2.column_name;

Equi Join Example


Execute the below equijoin statement for joining tables:

mysql> SELECT cust. customer_name, bal.balance


FROM customer AS cust, balance AS bal
WHERE cust.account = bal.account_num;

We can also get the same result by using the below statement:

mysql> SELECT cust. customer_name, bal.balance


FROM customer AS cust
JOIN balance AS bal
WHERE cust.account = bal.account_num;
Program 27
Write Python program of database connectivity with database name “trynew” and password as “system” to read
all the records from database table “student”and print it on screen.

Program 28
Write Python program of database connectivity with database name “trynew” and password as “system” to
fetch a particular record on the basis of account number entered by the user and print it on screen from table
“student”
Program 29
Write Python program of database connectivity with database name “trynew” and password as “system” to
update the marks of a particular student in table “student”on the basis of admission number entered by the user
and print it on screen.

Program 30
Write Python program of database connectivity with database name “trynew” and password as “system” to alter
table structure of table “student” to add a new column with name Pincode of type varchar(10) with constraint
NOT NULL.

You might also like