Professional Documents
Culture Documents
Name:
Section:
Python Basics (10 questions)
PROGRAM 1
Aim: Write a program to check if a number is negative, positive or zero.
Program:
Output:
PROGRAM 2
Aim: write a program to find the area of a triangle
Program:
Output:
PROGRAM 3
Aim: Write a program to find the largest among three numbers.
Program:
Output:
PROGRAM 4
Aim: Write a program to find the sum of n natural numbers using a while loop.
Program:
Output:
PROGRAM 5
Aim: Program to check whether a number is divisible by 2 or 3 using nested if.
Program:
Output:
PROGRAM 6
Aim: Menu based program to find sum, subtraction, multiplication and division of
values.
Program:
Output:
PROGRAM 7
Aim: Program to find factorial of a number.
Program:
Output:
PROGRAM 8
Aim: Program to print following pattern.
*
**
***
****
*****
Program:
Output:
PROGRAM 9
Aim: Program to print results depending upon the percentage.
Program:
Output:
PROGRAM 10
Program:
Output:
Python Functions programs
PROGRAM 1
Aim: Program to find largest among two numbers using a user defined function.
Program:
PROGRAM 2
Aim: Program to find simple interest using a user defined function with parameters
and with return value.
Program:
PROGRAM 3
Aim: Program to use default arguments in a function.
Program:
PROGRAM 4
Aim: Program to find sum of two numbers using a user defined function with
parameters.
Program:
PROGRAM 5
Aim: Python program to find H.C.F of two numbers
Program:
PROGRAM 6
Aim: Python program to print factors of a number
Program:
PROGRAM 7
Aim: Python program to calculate LCM
Program:
PROGRAM 8
Exception Handling
Aim: Python program to implement exception handling
Program:
Python Text file programs
PROGRAM 1
Aim: Program to read and display the contents in a file.
Program:
PROGRAM 2
Aim: Program to read and display those lines from a file that start with the alphabet
‘T’.
Program:
PROGRAM 3
Aim : Program to read and display those lines from file that start with alphabet ‘T’.
Program:
PROGRAM 4
Aim : Program to count number of words in data file data.txt.
Program:
PROGRAM 5
Aim : program to create a text file and add data
Program:
PROGRAM 6
Aim : Program to write rollno, name and marks of a student in a data file Marks.dat
Program:
count=int(input('How many students are there in the class'))
fileout=open("Marks.dat","a")
for i in range(count):
rollno=int(input("Enter rollno:"))
name=input("name")
marks=float(input('marks'))
rec=str(rollno)+","+name+","+str(marks)+"\n"
fileout.write(rec)
fileout.close()
PROGRAM 7
Aim : Write a Program to read data from data file in append mode and use
writeLines function utility in python.
Program:
PROGRAM 8
Aim :Program to read and display contents of file Marks.dat.
Program:
fileinp=open("Marks.dat","r")
while str:
str=fileinp.readline()
print(str)
fileinp.close()
PROGRAM 9
Aim : Program to read the content of file and display the total number of consonants,
uppercase, vowels and lower case characters.
Program:
PROGRAM 10
Aim : Program to read and display file content line by line with each word separated
by “ #”
Program:
PROGRAM 11
Aim : Program to read the content of a file line by line and write it to another file
except for the lines containing “a '' letters in it.
Program:
PROGRAM 12
Aim : Program to read the content of a file line by line and write it to another file
except for the lines containing “a '' letters in it.
Program:
Python Binary file programs
PROGRAM 1
f = open('C:\myimg.png', 'rb') # opening a binary file
content = f.read() # reading all lines
print(content) #print content
f.close() # closing file object
PROGRAM 2
import pickle
# write the object data into binary file using the dump() method
pickle.dump(data, fileobj)
Program:
import pickle
def write():
D={}
f=open("Studentdetails.dat","wb")
while True:
D['Roll No'] = r
D['Name'] = n
pickle.dump(D,f)
ch = input("More ? (Y/N)")
if ch in 'Nn':
break
f.close()
def Search() :
found = 0
f = open("Studentdetails.dat", "rb")
try:
while True:
rec = pickle.load(f)
if rec['Roll No']==rollno:
print(rec['Name'])
found = 1
break
except EOFError:
if found == 0:
f.close()
write()
Search()
OUTPUT:
Enter Roll no : 1
More ? (Y/N)y
Enter Roll no : 2
More ? (Y/N)y
Enter Roll no : 3
Enter Name : vyom
More ? (Y/N)N
display :3
vyom
PROGRAM 4
Aim : Unpickling data in python
Program:
import pickle
fileobject=open("mybinary.dat","rb")
objectvar=pickle.load(fileobject) fileobject.close()
print(objectvar)