CS FILE 12
CS FILE 12
CS FILE 12
OUTPUT:
QUES 31:Create a CSV file by entering user-id and password,
read and search the password for given user- id.
ANS:
import csv
with open("user_info.csv", "w") as obj:
fileobj = csv.writer(obj)
fileobj.writerow(["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
fileobj.writerow(record)
x = input("press Y/y to continue and N/n to terminate the
program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("user_info.csv", "r") as obj2:
fileobj2 = csv.reader(obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
Break
OUTPUT:
QUES 32:Wap to read all content of “student.csv” and display
record of only those student who scored more than 80
marks.record stored in student is in format: roll no,name,marks.
Ans:
Import csv
F=open(“student.csv”,”r”)
d=csv.reader(f)
next(f)
print("student scored more than 80")
print()
for i in d:
if int(i[2])>80:
print("roll no=",i[0])
print("name=",i[1])
print("mark=",i[2])
print("----------------")
f.close()
QUES 33:Write a function that takes a number n and then
returns a randomly generated number having exactly n digits
(not starting with zero) e.g., if n is 2 then function can randomly
return a number 10-99 but 07, 02 etc. are not valid two digit
numbers.
ANS:
import random
def generate_number(n):
lower_bound = 10 ** (n - 1)
upper_bound = (10 ** n) - 1
return random.randint(lower_bound, upper_bound)
n = int(input("Enter the value of n:"))
random_number = generate_number(n)
print("Random number:", random_number)
OUTPUT:
QUES 34:Write a function that takes two numbers and returns
the number that has minimum one's digit.
[For example, if numbers passed are 491 and 278, then the
function will return 491 because it has got minimum one's digit
out of two given numbers (491's 1 is < 278's 8)].
ANS:
def min_ones_digit(num1, num2):
ones_digit_num1 = num1 % 10
ones_digit_num2 = num2 % 10
if ones_digit_num1 < ones_digit_num2:
return num1
else:
return num2
num1 = int(input("Enter first number:"))
num2 = int(input("Enter second number:"))
result = min_ones_digit(num1, num2)
print("Number with minimum one's digit:", result)
OUTPUT:
QUES 35:WAP to demonstrate the concept of variable length
Argument to calculate sum and product of the first 10 no.
ANS:
def sum10(*n):
total=0
for i in n:
total=total+i
print("sum of first 10 no:",total)
def product10(*n):
pr=1
for i in n:
pr=pr*i
print("product of first 10 no.:",pr)
sum10(1,2,3,4,5,6,7,8,9,10)
product10(1,2,3,4,5,6,7,8,9,10)
OUTPUT:
OUTPUT:
QUES 40:Write a program to create a Stack for storing only odd
numbers out of all the numbers entered by the user. Display the
content of the Stack along with the largest odd number in the
Stack.
ANS:
def push(stack, item):
stack.append(item)
def pop(stack):
if stack == [ ]:
return
return stack.pop()
def oddStack(num):
if num % 2 == 1:
push(stack, num)
def GetLargest(stack):
elem = pop(stack)
large = elem
while elem != None:
if large < elem:
large = elem
elem = pop(stack)
return large
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"D
EPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"
%10 s"%row[3])
elif choice==0:
con.close()
print("## Bye!! ##")
Output:
cur = con.cursor()
print("#"*40)
print("EMPLOYEE SEARCHING FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO SEARCH :"))
query="select * from employee where
empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO","%20s"%"NAME","%15s"
%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2]
,"%10s"%row[3])
ans=input("SEARCH MORE (Y) :")
Output: