0% found this document useful (0 votes)
104 views

Practical File

This document contains a computer practical file for a student named Ayush Gupta in class 12. It includes 20 programs related to Python programming and MySQL database connectivity. The programs cover topics like arithmetic operations, checking if a number is perfect or Armstrong, factorial calculation, Fibonacci series, palindrome checking, list operations, file handling, and basic CRUD operations on database tables.

Uploaded by

ayush gupta
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)
104 views

Practical File

This document contains a computer practical file for a student named Ayush Gupta in class 12. It includes 20 programs related to Python programming and MySQL database connectivity. The programs cover topics like arithmetic operations, checking if a number is perfect or Armstrong, factorial calculation, Fibonacci series, palindrome checking, list operations, file handling, and basic CRUD operations on database tables.

Uploaded by

ayush gupta
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/ 19

DPS, BALLABGARH

COMPUTER PRACTICAL FILE

NAME – AYUSH GUPTA

CLASS – XII NM

ROLL NO. – 17620143

SUBJECT CODE - 083


INDEX
S.NO DATE SIGNATURE
PROGRAM
1 Program to enter two numbers and print the arithmetic operations like
+,-,*, /, // and %.

Write a program to find whether an inputted number is perfect or not.


2
3 Write a Program to check if the entered number is Armstrong or not.

4 Write a Program to find factorial of the entered number.

Write a Program to enter the number of terms and to print the Fibonacci
5 Series.

Write a Program to enter the string and to check if it’s palindrome or not
6 using loop.

Write a Program to show the outputs based on entered list.


7
Write a Program to enter the numbers in a list using split () and to use all
8 the functions Related to list.

9 Write a Program to enter the number and print the Floyd’s Triangle in
decreasing order
Write a Program to find factorial of entered number using user-defined
10 module fact().
Write a Program to enter the numbers and find Linear Search, Binary
11 Search, Lowest Number and Selection Sort using list/array code.
Write a Program to read data from data file and show Data File Handling
12 related functions utility in python.
Write a Program to read data from data file in append mode and use
13 writeLines function utility in python.
Write a Program to read data from data file in read mode and count the
14 particular word occurrences in given string, number of times in python.
15 Write a Program to read data from data file in read mode and append the
words starting with letter ‘T’ in a given file in python.
Write a Program to show MySQL database connectivity in python.
16
Write a Python program to implement all basic operations of a stack, such
17 as adding element (PUSH operation), removing element (POP operation)
and displaying the stack elements (Traversal operation) using lists.
18 Write a program to display unique vowels present in the given word
using Stack.

19 Perform all the operations with reference to table ‘Employee’ through


MySQL-Python connectivity
Program 2: Write a program to find whether an inputted number is perfect or not.

Solution:

# To find whether a number is perfect or not

def pernum(num):

divsum=0

for i in range(1,num):

if num%i == 0:

divsum+=i

if divsum==num:

print('Perfect Number')

else:

print('Not a perfect number')

pernum(6)

pernum(15)
Program 3: Write a Program to check if the entered number is Armstrong or not.

Solution:

# Program to check if the entered number is Armstrong or not.

#An Armstrong number has sum of the cubes of its digits is equal to the number itself no=int(input("Enter any

number to check : "))

no1 = no sum

=0

while(no>0):

ans = no % 10;

sum = sum + (ans * ans * ans) no

= int (no / 10)

if sum == no1: print("Armstrong

Number")

else:

print("Not an Armstrong Number")


Program 15: Write a program to read data from data file in read mode and append the words starting with letter ‘T’
in a given file in python

Solution:
#Program to read data from data file in read mode and

#append the words starting with letter ‘T’ #in a

given file in python f=open("test.txt",'r')

read=f.readlines()

f.close()

id=[]

for ln in read:

if ln.startswith("T"):

id.append(ln)

print(id)
Program 16: Write a Program to show MySQL database connectivity in python.

Solution:

import mysql.connector

con=mysql.connector.connect(host='localhost',user='root',password='',db='school')

stmt=con.cursor()

query='select * from student;'

stmt.execute(query)

data=stmt.fetchone()

print(data)
Program 17: Write a Python program to implement all basic operations of a stack, such as adding
element (PUSH operation), removing element (POP operation) and displaying the stack elements
(Traversal operation) using lists.

Solution:

#Implementation of List as stack

s=[]

c="y"

while (c=="y"):

print ("1. PUSH")

print ("2. POP ")

print ("3. Display")

choice=int(input("Enter your choice:")) if

(choice==1):

a=input("Enter any number :")

s.append(a)

elif (choice==2):

if (s==[]):

print ("Stack Empty") else:

print ("Deleted element is : ",s.pop())

elif (choice==3):

l=len(s)

for i in range(l-1,-1,-1): #To display elements from last element to first print

(s[i])

else:

print("Wrong Input")

c=input("Do you want to continue or not? ")


Program 18: Write a program to display unique vowels present in the given word using
Stack.

Solution:

#Program to display unique vowels present in the

given word #using Stack

vowels =['a','e','i','o','u']

word = input("Enter the word to search for

vowels :") Stack = []

for letter in word:

if letter in vowels:

if letter not

in Stack:

Stack.appen

d(letter)

print(Stack)

print("The number of different vowels present in",word,"is",len(Stack))


Program 20: Perform all the operations with reference to table ‘Employee’ through
MySQL-Python connectivity.

Solution:

import MySQLdb

# Using connect method to connect database

db1 = MySQLdb.connect("localhost","root","","TESTDB" ) #

using cursor() method for preparing cursor

cursor = db1.cursor()

# Preparing SQL statement to create EMP table

sql = "CREATE TABLE EMP(empno integer primary key,ename varchar(25) not null,salary float);"

cursor.execute(sql)

# disconnect from server

db1.close()
Inserting a record in ‘emp’

import MySQLdb

db1 = MySQLdb.connect("localhost","root","","TESTDB" ) cursor =

db1.cursor()

# Prepareing SQL statement to insert one record with the given values sql = "INSERT

INTO EMP VALUES (1,'ANIL KUMAR',86000);"

try:

cursor.execute(sql ql)

db1.commit()

except:

db1.rollback()

db1.close()

Fetching all the records from EMP table having salary more than 70000.

import MySQLdb

db1 = MySQLdb.connect("localhost","root","","TESTDB" ) cursor =

db1.cursor()

sql = "SELECT * FROM EMP WHERE SALARY > 70000;"

try:

cursor.execute(sql)

#using fetchall() function to fetch all records from the table EMP and store in resultset

resultset = cursor.fetchall() for row in

resultset:

print (row) except:


print ("Error: unable to fetch data")

db1.close()

Updating record(s) of the table using UPDATE

import MySQLdb

db1 = MySQLdb.connect("localhost","root","","TESTDB" ) cursor =

db1.cursor()

#Preparing SQL statement to increase salary of all employees whose salary is less than 80000

sql = "UPDATE EMP SET salary = salary +1000 WHERE salary<80000;" try:

cursor.execute(sql)

db1.commit() except:

db1.rollback()

db1.close()
Deleting record(s) from table using DELETE

import MySQLdb

db1 = MySQLdb.connect("localhost","root","","TESTDB" ) cursor =

db1.cursor()

sal=int(input("Enter salary whose record to be deleted : ")) #Preparing SQL

statement to delete records as per given condition sql = "DELETE FROM EMP

WHERE salary =sal”

try:

cursor.execute(sql)

print(cursor.rowcount, end=" record(s) deleted ")

db1.commit()

except:

db1.rollback()

db1.close() Output:

>>> Enter salary whose record to be deleted: 80000 1 record(s)

deleted
This PDF document was edited with Icecream PDF Editor.

You might also like