0% found this document useful (0 votes)
24 views26 pages

Practical File 2023-24

Uploaded by

surangamapoonia
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)
24 views26 pages

Practical File 2023-24

Uploaded by

surangamapoonia
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/ 26

Program 1: Input any number from user and calculate factorial of a number

# Program to calculate factorial of entered number


num = int(input("Enter any number :"))
fact = 1
n = num # storing num in n for printing
while num>1: # loop to iterate from n to 2
fact = fact * num
num-=1

print("Factorial of ", n , " is :",fact)

OUTPUT
Enter any number :6
Factorial of 6 is : 720

Program 2: Input any number from user and check it is Prime no. or Composite

#Program to input any number from user


#Check it is Prime number of not
import math
num = int(input("Enter any number :"))
isPrime=True
for i in range(2,int(math.sqrt(num))+1):
if num % i == 0:
print("## Number is composite ##")
break
else:
print("## Number is Prime ##")

OUTPUT
Enter any number :117
## Number is composite ##
>>>
Enter any number :119
## Number is composite ##
>>>
Enter any number :113
## Number is Prime ##
>>>

Page :
Enter any number :7
## Number is Prime ##
>>>
Enter any number :19
## Number is Prime ##

Program 3: To write a menu driven Python Program to perform Arithmetic


operations (+,-*, /) based on the user’s choice.

SOURCE CODE:

Page :
SAMPLE OUTPUT:

Python Program Executed Output:

Program 4: To write a Python Program to display Fibonacci Series up to ‘n’ numbers.

Python Executed Program Output:

Page :
Program 5: Program to search any word in given string/sentence using a
function with two arguments- the string and the word to search.
#Program to find the occurrence of any word in a
string
def countWord(str1,word):
s=
str1.split()
count=0
for w in s:
if w==word:
count+=1
return count

str1 = input("Enter any sentence :")


word = input("Enter word to search in sentence :")
count = countWord(str1,word)
if count==0:
print("## Sorry! ",word," not present ")
else:
print("## ",word," occurs ",count," times ## ")

OUTPUT

Enter any sentence :my computer your computer our computer everyones
computer Enter word to search in sentence :computer
## computer occurs 4 times ##

Page :
Enter any sentence :learning python is
fun Enter word to search in sentence
:java
## Sorry! java not present

Program 6: To write a menu driven Python Program to find Factorial and sum of
list of numbers using function.

Page :
Python Executed Program Output:

Program 7 : To write a Python program to implement python mathematical functions to find:


(i) To find Square of a Number.
(ii) To find Log of a Number(i.e. Log10)
(iii) To find Quad of a Number

Output

Page :
Program 8 :
To write a Python program to generate random number between 1 to 6 to simulate
the roll of a dice.

Output

Program 9 : Program to read and display file content line by line with each word
separated by ‘#’.

f = open("mytext.txt")
for line in f:
words = line.split()
for w in words:
print(w+'#',end='')
print()
f.close()
Page :
NOTE : if the original content of file is:
India is my
country I love
python
Python learning is fun

OUTPUT
India#is#my#country#
I#love#python#
Python#learning#is#fun
#

Program 10: Program to read the content of file and display the total number of
consonants, uppercase, vowels and lower case characters.

f = open("mytext.txt")
v=c=u=l=o=0
data = f.read()
vowels=['a','e','i','o','u'] # vowels= ‘aeiou’
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
else:
o+=1
print("Total Vowels in file:",v)
print("Total Consonants in file :",c)
print("Total Capital letters in file:",u)
print("Total Small letters in file:",l)
print("Total Other than letters :",o)
f.close()
NOTE : if the original content of file is:
India is my country
I love python
Python learning is fun 123@

Output
Total Vowels in file: 16
Total Consonants in file : 30
Total Capital letters in file: 3
Total Small letters in file: 43
Page :
Total Other than letters : 16

Program 11: Program to create binary file to store Rollno and Name, Search any
Rollno and display name if Rollno found otherwise “Rollno not found”

Page :
Program 12: To write a python program to read lines from a text file "Sample.txt" and copy those lines
into another file which are starting with an alphabet 'a' or 'A'.

Page :
Program 13: To write a Python Program to Create a binary file with roll number, name, mark and
update/modify the mark for a given roll number.

Page :
Output:

Page :
Program 14: To write a Python Program to Create a binary file with roll number, name, mark and
update/modify the mark for a given roll number.

Page :
OUTPUT
Program 15:
To write a Python program Create a CSV file to store Empno, Name, Salary and
search any Empno and display Name, Salary and if not found display appropriate
message.

Output:

Page :
Program 16:
To write a Python program to implement Stack using a list data-structure.

Page :
SOURCE CODE:

OUTPUT:

Page :
Page :
Program 17:
To write a Python Program to integrate MYSQL with Python by inserting records to Emp table and display
the records.

Page :
Page :
OUTPUT

MySQL OUTPUT

Program 18:
To write a Python Program to integrate MYSQL with Python to search an Employee
Page :
using EMPID and display the record if present in already existing table EMP, if not
display the appropriate message.

OUTPUT

Page :
Program 19:
To write a Python Program to integrate MYSQL with Python to search an Employee
using EMPID and update the Salary of an employee if present in already existing
table EMP, if not display the appropriate message.

Output:

Page :
Program 20:
SQL COMMANDS EXERCISE – 1
Table Name : STU
Rollno Name Gender Age Dept DOA Fees
1 Arun M 24 COMPUTER 1997-01-10 120
2 Ankit M 21 HISTORY 1998-03-24 200
3 Anu F 20 HINDI 1996-12-12 300
4 Bala M 19 NULL 1999-07-01 400
5 Charan M 18 HINDI 1997-09-05 250
6 Deepa F 19 HISTORY 1997-06-27 300
7 Dinesh M 22 COMPUTER 1997-02-25 210
8 Usha F 23 NULL 1997-07-31 200

(a) Write a Query to Create a new database in the name of "STUDENTS"

mysql> CREATE DATABASE STUDENTS;

(b) Write a Query to Open the database "STUDENTS"

mysql> USE STUDENTS;

(c) Write a Query to create the above table called: STU

mysql> CREATE TABLE STU(Rollno int Primary key,Name varchar(10),Gender varchar(3),


Age int,Dept varchar(15),DOA date,Fees int);

(d) Write a Query to list all the existing database names.


Sol:
mysql> SHOW DATABASES;
Output:

Page :
(e) Write a Query to List all the tables that exists in the current database.
Sol:
mysql> SHOW TABLES;
Output:

(f) Write a Query to insert all the rows of above table into STU table.
INSERT INTO STU VALUES (1,'Arun','M', 24,'COMPUTER','1997-01-10',
120);
INSERT INTO STU VALUES (2,'Ankit','M', 21,'HISTORY','1998-03-24',
200);
INSERT INTO STU VALUES (3,'Anu','F', 20,'HINDI','1996-12-12', 300);
INSERT INTO STU VALUES (4,'Bala','M', 19, NULL,'1999-07-01', 400);
INSERT INTO STU VALUES (5,'Charan','M', 18,'HINDI','1997-06-27', 250);
INSERT INTO STU VALUES (6,'Deepa','F', 19,'HISTORY','1997-06-27',
300);
INSERT INTO STU VALUES (7,'Dinesh','M', 22,'COMPUTER','1997-02-25',
210);
INSERT INTO STU VALUES (8,'Usha','F', 23, NULL,'1997-07-31', 200);

(g) Write a Query to display all the details of the Employees from the above table 'STU'.

mysql> SELECT * FROM STU;

Output:

(h) Write a query to Rollno, Name and Department of the students from STU table.
mysql> SELECT ROLLNO,NAME,DEPT FROM STU;

Page :
Program 21:
SQL COMMANDS EXERCISE –
2

To write Queries for the following Questions based on the given table:

Rollno Name Gender Age Dept DOA Fees


1 Arun M 24 COMPUTER 1997-01-10 120
2 Ankit M 21 HISTORY 1998-03-24 200
3 Anu F 20 HINDI 1996-12-12 300
4 Bala M 19 NULL 1999-07-01 400
5 Charan M 18 HINDI 1997-09-05 250
6 Deepa F 19 HISTORY 1997-06-27 300
7 Dinesh M 22 COMPUTER 1997-02-25 210
8 Usha F 23 NULL 1997-07-31 200

(a) Write a Query to select distinct Department from STU table.


Sol:
mysql> SELECT DISTICT(DEPT) FROM STU;

Output:

(b) To show all information about students of History department.


Sol:

mysql>SELECT * FROM STU WHERE DEPT='HISTORY';

Output:

(c) Write a Query to list name of female students in Hindi Department.


Sol:

mysql> SELECT NAME FROM STU WHERE DEPT='HINDI' AND GENDER='F';

Output:

Page :
(d) Write a Query to list name of the students whose ages are between 18 to 20.
Sol:

mysql> SELECT NAME FROM STU WHERE AGE BETWEEN 18 AND 20;

Output:

(e) Write a Query to display the name of the students whose name is starting with 'A'.
Sol:

mysql> SELECT NAME FROM STU WHERE NAME LIKE 'A%';

Output:

(f) Write a query to list the names of those students whose name have second alphabet
'n' in their names.
Sol:

mysql> SELECT NAME FROM STU WHERE NAME LIKE '_N%';

Page 26 of 26

You might also like