You are on page 1of 26

PROGRAMS ON FUNCTION , DATA FILE HANDLING,

STACK , SQL

Name Anmol Mishra


Class XII-B
Roll no -9
.INDEX
1) Write a program function using a function to print factorial number series
from ‘n’ to ‘m’
2) Write the program to accept the user name admin as a default argument
and password 123 enter by the user to allow login into the system
3) Write a program to demonstrate the concept of variable length argument
to calculate the product and power of the first ‘10’ natural number
4) Read a text file line by line and display each word separated by a’#’
5) Read a text file and display the number of vowels consonants uppercase,
lowercase characters in the file
6) Remove all the line that contain a character small ‘a’ in a file and write into
another file,
7) Create a binary file with name and roll name search if not found display an
appropriate message
8) Create a binary file with roll no, name and marks . input roll no and
update mark.
9) Write a random number generator that generates random number
between 1 and 6
10) Create a csv file by entering user id and password read and search the
password for given user id
11) Write a python program to implement stack operations
# Write a program function using a function to print
factorial number series from ‘n’ to ‘m’

‘’’ PROGRAM ‘’’

def factorial(x):
if x == 1:
return 1
else:
return (x * factorial(x-1))

n=int(input("enter value for n:"))


m=int(input("enter value for m:"))
if m>n:
for i in range(n,m+1):
print(factorial(i))
else:
t=n
n=m
m=t
for i in range(n,m+1):
print(factorial(i))
‘’’OUTPUT’’’
#Write the program to accept the user name admin as a default
argument and password 123 enter by the user to allow login
into the system
‘’’ PROGRAM’’’
def login(password, username="Admin"):

if password =="123" :
print ( "You have logged into system " )
else:
print ( " incorrect password " )

password = input ( " Enter the password: " )


username = input ( " Enter the User name: " )

if username == "Admin" or "admin" :


login( password )
else:
login( password, username )
‘’’OUTPUT’’’
#Write a program to demonstrate the concept of variable
length argument to calculate the product and power of the first
‘10’ natural number

def power(x, n):


pow = 1
for i in range(n):
pow = pow * x
return pow

n=2
for i in range(1,11):
x=i
print(power(x,n))
‘’’OUTPUT’’’
# Read a text file line by line and display each word separated
by a’#’
‘’’ PROGRAM ‘’’
file=open("sarojini.txt","r")
r=file.readlines()
for i in range(len(r)):
a=r[i].split()
print("#".join(a))
file.close()
‘’’OUTPUT’’’
# Read a text file and display the number of vowels consonants
uppercase, lowercase characters in the file
‘’’ PROGRAM ‘’’

file=open("sarojini.txt","r")
v=c=u=l=0
b=file.read()
for i in b:
if i.isalpha():
if i in["a","e","i","o","u","A","E","I","O","U"]:
v+=1
else:
c+=1
if i.isupper():
u+=1
elif i.islower():
l+=1

print("no of vowels",v)
print("no of consonants",c)
print("no of upper case",u)
print("no of lower case",l)
‘’’ OUTPUT ‘’’
# Remove all the line that contain a character small ‘a’ in a file
and write into another file.
‘’’ PROGRAM ‘’’

file=open("sarojini.txt","r+")
file1=open("sarojinicopy.txt","w")
a=file.readlines()
l=a
for i in range(len(a)):
if "a" in a[i]:
file1.write(a[i])
l[i]=""
file.seek(0)
file.writelines(l)
file.close()
file1.close()
‘’’ OUTPUT ‘’’
# Create a binary file with name and roll name search if not
found display an appropriate message
‘’’ PROGRAM ‘’’
import pickle
file=open("binary.txt","wb+")
a=()
f=False
for i in range(7):
roll=int(input("enter roll num"))
name=input("enter name")
a["roll"]=roll
a["name"]=name
picke.dump(a,file)

file.seek(0)
b=int(input("enter a roll o which you want to search"))
try:
while True:
c=pickle.load(file)
if c["rollno"]==c:
print(c)
f=True
except EOFError:
if f:
file.close()
else:
print("student detail not found")
file.close()
‘’’ OUTPUT ‘’’
# Create a binary file with roll no, name and marks . input roll
no and update mark.
‘’’ PROGRAM ‘’’
import pickle
e1={"rollno":1,"name":"student1","Marks":90}
e2={"rollno":2,"name":"student2","Marks":91}
e3={"rollno":3,"name":"student3","Marks":92}
e4={"rollno":4,"name":"student4","Marks":93}
e5={"rollno":5,"name":"student5","Marks":94}
e6={"rollno":6,"name":"student6","Marks":95}
e7={"rollno":7,"name":"student7","Marks":96}
e8={"rollno":8,"name":"student8","Marks":97}
file=open("record.txt","wb")
pickle.dump(e1,file)
pickle.dump(e2,file)
pickle.dump(e3,file)
pickle.dump(e4,file)
pickle.dump(e5,file)
pickle.dump(e6,file)
pickle.dump(e7,file)
pickle.dump(e8,file)
print("successful")
file.close()
‘’’ OUTPUT’’’
# Write a random number generator that generates random
number between 1 and 6
‘’’ PROGRAM ‘’’

import random
a="y"
while a=="y":
print("random number",random.randint(1,6))
a=input("would you like to continue y/n")
‘’’ OUTPUT’’’
#Create a csv file by entering user id and password read and
search the password for given user id
‘’’ PROGRAM ‘’’
import csv

List = [["aryan", "aryan2004"],


["utkarsh", "utkarsh2004"],
["anirudh", "anirudh2004"],
["MC headshot","174896"],
["Paradox", "174899"],
["Mc Square","174888"]]

f1=open("UserInformation.csv", "w", newline="\n")


writer=csv.writer(f1)
writer.writerows(List)
f1.close()

f2=open("UserInformation.csv", "r")
rows=csv.reader(f2)
userId = input("Enter the user-id: ")
fl = True
for record in rows:
if record[0]==userId:
print("The password is: ", record[1])
fl = False
break
if fl:
print("User-id not found")
‘’’ OUTPUT ‘’’
#Write a python program to implement stack operations
def isEmpty(stk):
if stk == []:
return True
else:
return False

def add(stk,item):
stk.append(item)
top = len(stk)-1

def remove(stk):
if(stk==[]):
print("Stack empty;UNderflow")
else:
print("Deleted student is :",stk.pop())

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 OPERATION:")
print("1.ADD student")
print("2.Display stack")
print("3.Remove student")
print("4.Exit")
ch = int(input("Enter your choice(1-4):"))
if ch==1:
rno = int(input("Enter Roll no to be inserted :"))
sname = input("Enter Student name to be inserted :")
item = [rno,sname]
add(stack,item)
input()
elif ch==2:
display(stack)
input()
elif ch==3:
remove(stack)
input()
elif ch==4:
break
else:
print("Invalid choice ")
input()
‘’’ OUTPUT ‘’’

You might also like