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

Practical File

This document contains 15 programming questions and their solutions. It includes programs to find square, cube and square root of a number; count characters in a string; create a dictionary of student names and scores; read and analyze text files; calculate simple interest; store and retrieve student records from a binary file; check for palindromes; and swap the values of two variables. The programs cover basic input/output, conditional statements, loops, functions, files and dictionaries.

Uploaded by

Shikhar Bajpai
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)
42 views

Practical File

This document contains 15 programming questions and their solutions. It includes programs to find square, cube and square root of a number; count characters in a string; create a dictionary of student names and scores; read and analyze text files; calculate simple interest; store and retrieve student records from a binary file; check for palindromes; and swap the values of two variables. The programs cover basic input/output, conditional statements, loops, functions, files and dictionaries.

Uploaded by

Shikhar Bajpai
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/ 16

Name – Shikhar Bajpai

Class - XII-PCM(B)
Roll No. - 33
Sr. No. – 4096
Subject – Coumputer
Science

Teacher – Fuzail sir


1. Program to input a number and find
square, cube, and square root.
num=float(input(“Enter a number:”))
num_sq= num*num
num_cube= num*num*num
num_sqrt=num**0.5
print(“The square of” num “is”
num_sq)
print(“The cube of” num “is”
num_cube)
print(“The square root of” num “is”
num_sqrt)
2. Program that read a line and print its
statistics...

line=input(“enter a line:”)
lowercount=uppercount=0
digitcount=alphacount=0
for a in line:
if a.islower():
lowercount+=1
elif a.isupper():
uppercount+=1
elif a.isdigit():
digitcount+=1
if a.isalpha():
alphacount+=1
print(“Number of uppercase
letters:”,uppercount)
print(“Number of lowercase letters:”,
lowercount)
print(“Number of alphabets:”,
alphacount)
print(“Number of digit:”, digitcount)
3. Program to create a dictionary containing
name of student and number of wins...

n=int(input(“How many student ?”))


compwinner={}
for a in range(n):
key=input(“Name of the
student:”)
value=int(input(“Number of
competition won”))
compwinner[key]= value
print(“The dictionary now is:”)
print(compwinner)
4. Program to read a file named “story.txt”,
count and print total lines starting with
vowels in the file?

filepath = 'story.txt'
vowels="AEIOUaeiou"
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
if(line[0] in vowels):
#print(line)
print("Line {}: {}".
format(cnt, line.strip()))
cnt=cnt+1
line = fp.readline()
5. Program to take input for 3 numbers,
check and print the largest number?

a=int(input("Enter 1st no "))


b=int(input("Enter 2nd no "))
c=int(input("Enter 3rd no "))
if(a>b and a>c):
m=a
else:
if(b>c):
m=b
else:
m=c
print("Max no = ",m)
6. Program to take input for 2 numbers and
an operator (+ , – , * , / ). Based on the
operator calculate and print the result?
a=int(input("Enter 1st no "))
b=int(input("Enter 2nd no "))
op=input("Enter the operator (+,-,,/)
")
if(op=="+"):
c=a+b
print("Sum = ",c)
elif(op==""):
c=a*b
print("Product = ",c)
elif(op=="-"):
if(a>b):
c=a-b
else:
c=b-a
print("Difference = ",c)
elif(op=="/"):
c=a/b
print("Division = ",c)
else:
print("Invalid operator")
7. Program to read a file named “story.txt”,
count and print total alphabets in the file?

def count_alpha():
lo=0
with open("story.txt") as f:
while True:
c=f.read(1)
if not c:
break
print(c,end='')
if((c>='A' and c<='Z') or
(c>='a' and c<='z')):
lo=lo+1
print("total lower case
alphabets ",lo)
#function calling
count_alpha()
8. Program to read a file named “story.txt”,
count and print total lines starting with
vowels in the file?
filepath = 'story.txt'
vowels="AEIOUaeiou"
with open(filepath) as fp:
line = fp.readline()
cnt = 1
while line:
if(line[0] in vowels):
#print(line)
print("Line {}:
{}".format(cnt, line.strip()))
cnt=cnt+1
line = fp.readline()
9. Program for calculating simple interest.

print ("SIMPLE INTEREST CALCULATION\n")


print ("Enter the following details.")
p=int(input ("Amount: "))
n=int(input ("No. of Years: "))
r=int (input ("Rate of Interest: "))
i=(p*n*r)/100
print ("\n The interest is :” + str(i))
10. Program to input total number of
sections and stream name in class and
display all information on the output screen.

classxii=dict()
n=int(input("Enter total number of
section in xi class: "))
i=1
while i<=n:
a=input("Enter Section: ")
b=input("Enter stream name:")
classxi [a]=b
i=i+1
print ("class","\t","section", '\
t',"Stream name")
for i in classxii:
print ("XI", '\t',i,'\
t', classxi [i])
11. Program to accept username "Admin"
as default argument and password 123
entered by user to allow login into the
system.

def user_pass(password,username="
Admin"):
if password=='123':
print("You have logged into system")
else:
print("Password is incorrect!!!!!!")
password=input("Enter the password:")
user_pass(password)
12. Create a binary file client.dat to hold
records like ClientID, Client name, and
Address using the dictionary. Write
functions to write data, read them, and print
on the screen.
import pickle
rec={}
def file_create():
f=open("client.dat","wb")
cno = int(input("Enter Client ID:"))
cname = input("Enter Client Name:")
address = input("Enter Address:")
rec={cno:[cname,address]}
pickle.dump(rec,f)
def read_data():
f = open("client.dat","rb")
print("*"*78)
print("Data stored in File....")
rec=pickle.load(f)
for i in rec:
print(rec[i])
file_create()
read_data()
13. Check whether a given number is a
palindrome.
n=int(input("Enter any Number: "))
rev=0
ori=n
while(n>0):
d=n%10
rev=(rev*10)+d
n=n//10
print("Reverse of a given number is =
%d" %rev)
if(ori == rev):
print("%d is a Palindrome Number"
%ori)
else:
print("%d is not a Palindrome
Number" %ori)
14. Program to write student record into
binary file ‘student.dat’.

import pickle
lst =[]
while True:
roll = input("Enter roll number:")
name = input("Enter name of
student:")
student = {"roll" :roll,
"sname" :name}
lst.append(student)
choice = input("Want to add more
record(y/n):")
if (choice=='n'):
break
file = open("student.dat",'wb')
pickle.dump(lst,file)
file.close()
15. Program to Exchange the Values of Two
Numbers
def swap(x, y):
temp = x
x = y
y = temp
print("After swapping")
print("Value of first number :", x)
print("Value of second number :",
y)
num1=int(input("Enter first number :"))
num2=int(input("Enter second
number :"))
print("Before swapping")
print("Value of first number :",num1)
print("Value of second number :",num2)
swap(num1,num2)

You might also like