PROGRAM FILE
Q1) Write a python program which includes
following user defined functions:
(i) to print factorial of a given number
Code:
def fact(n):
fact=1
for i in range(n,1,-1):
fact*=i
print(fact)
n=int(input("Enter a number: "))
fact(n)
Output:
(ii) to print Fibonacci series
Code:
def fibonacci(a):
first=0
second=1
print(first)
print(second)
for i in range(1,a):
third=first+second
print(third)
first,second=second,third
n=int(input("Enter a number: "))
fibonacci(n)
Output:
Q2) Write a program to count total number of
lines and total number of lines which start with
either a, b or c from the file.
Code:
count = 0
file=open('C:\\python programs\\story.txt','r')
lst = file.readlines()
for i in lst :
if i[0] in ['A','B','C','a','b','c'] :
print(i)
count += 1
print()
total = len(lst)
print("Total Number of Lines : ",total)
print("So for number of sentences started with
A,B,C : ",count)
file.close()
Output:
Q3) Write a program to check whether a number is
divisible by 2 or 3 using nested if.
Code:
x=int(input("Enter a number: "))
if x%2==0 or x%3==0:
if x%2==0:
print("The number",x,"is divisible by 2")
if x%3==0:
print("The number",x,"is divisible by 3")
else:
print("The number",x,"is not divisible by 2
or 3")
Output:
Q4) Make simple calculator with the use of user
defined functions.
Code:
import math
def add(x,y):
return x+y
def subtract(x,y):
return x-y
def multiply(x,y):
return x*y
def divide(x,y):
return x/y
def reciprocal(x):
return 1/x
def square_root(x):
return math.sqrt(x)
def square(x):
return x**2
def cube(x):
return x**3
print(''' *** PRESS ACCORDING TO YOUR CHOICE
***
1.ADDITION
2.SUBTRACTION
3.DIVISION
4.MULTIPLICATION
5.RECIPROCAL
6.SQAURE ROOT
7.SQAURE
8.CUBE''')
input_taken = int(input('Enter HERE : '))
if input_taken > 8 :
print('*** ENTER VALID NUMBER ***')
if input_taken <=4 :
x = int(input("ENTER FIRST NUMBER : "))
y = int(input("ENTER SECOND NUMBER : "))
if input_taken == 1:
print(add(x,y))
if input_taken == 2:
print(subtract(x,y))
if input_taken == 3:
print(divide(x,y))
if input_taken == 4:
print(multiply(x,y))
if input_taken>=5 and input_taken<=8:
x = int(input("ENTER NUMBER : "))
if input_taken == 5:
print(reciprocal(x))
if input_taken == 6:
print(square_root(x))
if input_taken == 7 :
print(square(x))
if input_taken == 8:
print(cube(x))
Output:
Q5) Write a program to replace all spaces from
text with ‘-‘ from the file.
Code:
file = open('C:\\python programs\\spaces.txt','r')
file_open = file.read()
lis = list(file_open.split(' '))
output = '-'.join(lis)
print(output)
Output:
Q6) Write a program to display all prime numbers
between 1-200.
Code:
for n in range (0,201):
for i in range(2,n):
if n%i==0:
j=n/i
break
else:
print(n)
break
Output:
Q7) Write a python program to count number of
occurrence of a specific character in a string.
Code:
n=input("Enter any statement: ")
d=input("Enter character to check the number of
times repeated: ")
h=n.split(d)
a=len(h)
print(a-1)
Output:
Q8) Create a binary file with name and roll number
and search for given roll number and display
appropriate message.
Code:
import pickle
f=open("C:\\python programs\\student.dat",
"wb")
pickle.dump(["dev", 19], f)
pickle.dump(["Tanishq", 33], f)
pickle.dump(["rishi", 32], f)
pickle.dump(["presha", 9], f)
pickle.dump(["manan", 29], f)
f.close()
f=open("C:\\python programs\\student.dat", "rb")
n=int(input("Enter the Roll Number: "))
flag = False
while True:
try:
x=pickle.load(f)
if x[1]==n:
print("Name: ", x[0])
flag = True
except EOFError:
break
if flag==False:
print("This Roll Number does not exist")
Q9) Write a random number generator that
generates number between 1 and 6.
Code:
import random
s=random.uniform(1,6)
print(s)
Output:
Q10) Create a CSV file by entering user ID and
password. Read and search the password for given
user ID.
Code:
import csv
f=open('D:\\program files\\password.csv','w')
d=['id','password']
id1=csv.writer(f)
id1.writerow(d)
ask=int(input('How many enteries to make?: '))
for i in range(ask):
i_d=int(input("Enter id number: "))
password=input("Enter password: ")
l=[i_d,password]
id1.writerow(l)
f.close()
with open('D:\\program files\\
passwor.csv','r',newline='\r\n') as k:
st=csv.reader(k)
s=[]
ask2=input("Enter id to searc for password: ")
for i in st:
for j in i:
if j==ask2:
s.append(j)
print("Password of",ask2,'is',i[1])
if ask2 not in s:
print("Invalid id")
Output:
Q11) Write a program using user defined functions
to check whether given number is odd or even.
Code:
def oddoreven (n):
if n%2==0:
print("Number",n,'is even')
else:
print('Number',n,'is odd')
p=int(input("Enter a number: "))
oddoreven(p)
Output:
Q12) Write a program to create mirror of the given
string without using inbuilt function.
Code:
a=input("Enter a string: ")
print(a[::-1])
Output:
Q13) Write a program to generate values from 1 to
10 and then remove all the odd numbers from it.
Code:
lst=[1,2,3,4,5,6,7,8,9,10]
for i in lst:
if i%2==1:
lst.remove(i)
print(lst)
Output:
Q14) Write a program to accept a string and print
the number of uppercase, lowercase, vowels,
consonants and spaces in the given string.
Code:
upper=0
lower=0
vowels=0
consonants=0
spaces=0
vowel=['a','e','i','o','u','A','E','I','O','U']
s=input('Enter any string:')
for i in s:
if i[0].isupper()==True:
upper+=1
elif i[0].islower()==True:
lower+=1
elif i[0].isspace()==True:
spaces+=1
print("Uppercase are",upper)
print("Lowercase are",lower)
print("Spaces are",spaces)
for j in s:
if j[0] in vowel:
vowels+=1
else:
consonants+=1
print("Vowels are",vowels)
print("Consonants are",consonants-spaces)
Output:
Q15) Write a program using python to get ten
player’s name and their score. Write the input in
CSV file. Accept a player name using python. Read
the CSV file to display the name and the score. If
the player name is not found give an appropriate
message.
Code:
import csv
f=open('D:\\program filles\\players.csv','w')
d=['Name','score']
score1=csv.writer(f)
score1.writerow(d)
ask=int(input("How many enteries to make?"))
for i in range(ask):
name=input("Enter name of player: ")
score=int(input("Enter score of the player: "))
r=[name,score]
score1.writerow(r)
f.close()
with open('D:\\program files\\
players.csv','r',newline='\r\n') as k:
st=csv.reader(k)
s=[]
ask2=input("Enter name of player to search
score: ")
for i in st:
for j in i:
if j==ask2:
s.append(j)
print("Score of",ask2,'is',i[1])
if ask2 not in s:
print("Invalid player's name")
Q16) Write a program to implement a stack for
book details (Book number, book name). That is
now each item node of stack contains 2 types of
information book number and it’s name. Now
implement push pop and display value.
Code:
def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push(stk,item):
stk.append(item)
top = len(stk)-1
def Pop(stk):
if stk==[]:
print('Underflow')
else:
i=stk.pop()
if len(stk)==1:
top=None
else:
top=len(stk)-1
def Display(stk):
if isEmpty(stk):
print('Stack Empty')
else:
top = len(stk)-1
print(stk[top],'<-- top')
for a in range(top-1,-1,-1):
print(stk[a])
Stack=[]
top = None
while True:
print('Stack operations')
print('1.Push')
print('2.Display Stack')
print('3.Pop')
print('4.Exit')
ch=int(input('Enter your choice: '))
if ch==1:
bno=int(input('Enter book number: '))
bname=input('Enter book nname: ')
item=[bno,bname]
Push(Stack,item)
elif ch==2:
Display(Stack)
elif ch==3:
Pop(Stack)
elif ch==4:
break
else:
print('Invalid choice')
Output:
Q17) Create a database school create a student
table with the specifications roll number integer,
name character of length 10. And execute the
following:
(i) Insert 5 records
(ii)Display the contents of the table
Code and Output:
(i)
(ii)
Q18) Consider the following movie table and write
the SQL queries based on it:
Movie ID Movie name Type Release date Production cost Business cost
M001 Kashmir files Action 28/01/2022 1245300 1300000
M002 Attack Action 28/01/2022 1120000 1250000
M003 Loop Lapeta Thriller 01/02/2022 250000 300000
M004 Badhaai Do Drama 04/02/2022 720000 635000
M005 Shabaash Biograph 04/02/2022 10,00,000 830000
Mithu y
M006 Gehraiya Romance 11/02/2022 150000 120000
(i)Display all information from movie.
(ii)Display type of movie.
(iii)Display movie ID and movie name, total
earning. Calculate the business done by movie
using the difference of production cost and
business cost.
(iv)Display movie ID movie name in production
cost for all movies with production cost higher
than 1,50,000 and less than 10,000.
(v)Display movie of type action and romance.
(vi)Display the list of movies which were going to
be released in February 2022
Q19) Write the following queries:
●Write a query to display cube of 5.
●Write a query to display the number
563.854741 rounding off to the 100th place.
●Write a query to display ‘Put’ from
‘computer’
●Write a query to display today’s date in
DD/MM/YYYY format
●Write a query to display Dia from media.
●Write a query to display movie name- type
from the table movie.
●Write a query to display first 4 digits of
production cost from movie table.
●Write a query to display weekday of release
dates.
Q20) Suppose your school management has
decided to conduct cricket matches between
students of class 11 and class 12. Students of each
class are asked to join any one of the 4 teams the 4
teams are Titan, rockers, magnet and hurricane.
●Create database Sports.
●Create table Team with the following
specifications Team ID and team name
●Team ID storing an integer value between 1
to 9 which refers to unique identification of
a team.
●Team name should be a string of length not
less than 10 characters.
Q21) Insert 5 rows in team table.
Show the content of the table using dml
statement.
Q22) Create a match detail table as per following
specification:
Match ID, match date, first team ID, second team
ID, ft score, St score.
Enter any 5 records
Display all details from table who scored more
than 70 in first inning
Display unique team names.
Display match ID and match date played by
M005(match Id)
Q23) Consider the following table stock table to
answer the queries.
Itemno Item dcode Qty Unit_price Stock_data
S005 Ballpen 102 100 10 2018/04/22
S003 Gelpen 101 150 15 2018/03/18
S002 Pencile 102 125 5 2018/02/25
S006 Eraser 101 200 3 2018/01/12
S001 Sharpner 103 210 5 2018/06/11
5S004 Compass 102 60 35 2018/05/10
S009 A4 paper 102 160 5 2018/07/17
●Display all the ascending order of
stock_date.
●Display maximum price of items from each
delivery individually as per dcode from
stock.
●Display all the items in descending order of
itemsname.
●Display average price of items from each
delivery individually as per dcode from
stock which has average price is more than
5
●Display the sum of Qty from dcode.
Q24)With reference to the following relations
personal and job answer the questions that follow:
Create following tables such as emp number and
serial number are not null and unique, date of
birth is after 12th Jan 1960, name is never blank,
area and native place is valid, hobby, debt is not
empty, salary is between 4000 and 10000.
●Show employee number, name and salary of
those who have sports as hobby.
●Show number of employee area wise.
●Shoe the youngest employees from each native
place.
●Show additional burden on the company in
case salary of employees having hobby are
sports is increased by 10%.
●Show those employee name and date of birth
who have served more than 17 years as on
date.
●Your names of those who on more than all of
the employees of sales department.
●Erase all the records of employee from job
table whose hobby is not Sports.
10. Write a mysql connectivity program in Python
to create a database School. Create a table
students with specifications roll number.