Exp.
No: 1 ARITHMETIC OPERATIONS
Creating a menu driven python program to perform Arithmetic operations (+, -, *, /) based on the users
choice.
AIM:
To write a menu driven python program to perform Arithmetic operations (+, -, *, /) based on the users
choice.
SOURCE CODE:
print('1.Addition')
print('2.Subtraction')
print('3.Multiplication')
print('4.Division')
opt=int(input('Enter your choice:'))
a=int(input('Enter the first number:'))
b=int(input('Enter the second number:'))
if opt==1:
c=a+b
print('The addition of two numbers are:',c)
elif opt==2:
c=a-b
print('The subtraction of two numbers are:',c)
elif opt==3:
c=a*b
print('The multiplication of two numbers are:',c)
elif opt==4:
if b==0:
print('Enter any number other than 0')
else:
c=a/b
print('The division of two numbers are"',c)
else:
print('Invalid Option')
1
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice:1
Enter the first number:40
Enter the second number:60
The addition of two numbers are: 100
1.Addition
2.Subtraction
3.Multiplication
4.Division
Enter your choice:2
Enter the first number:68
Enter the second number:35
The subtraction of two numbers are: 33
Exp. No: 2 FIBONACCI SERIES
Create a python program to display fibonacci series.
AIM:
To write a python program to display fibonacci series upto n numbers.
SOURCE CODE:
first=0
second=1
no=int(input('How many fibonacci numbers you want?:'))
if no<=0:
print('Please enter a positive integer')
elif no==1:
print(first)
else:
print(first)
2
print(second)
for i in range(2, no):
third = first + second
first = second
second = third
print(third)
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
How many fibonacci numbers you want?:5
0
1
1
2
3
Exp. No: 3 RETURNING NUMBER FROM FUNCTION
Creating a python program to implement values from function.
AIM:
To write a python program to define a function check(no1, no2) that takes two numbers and returns a
number that has minimum ones digit.
SOURCE CODE:
def check(no1, no2):
if(no1%10)<(no2%10):
return no1
else:
return no2
a=int(input('Enter first number:'))
b=int(input('Enter second number:'))
r=check(a,b)
print("The number that has minimum one's digit is", r)
3
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
Enter first number:17
Enter second number:10
The number that has minimum one's digit is 10
Exp. No: 4 FACTORIAL USING FUNCTIONS
Creating a menu driven python program to find factorial and sum of list of numbers using functions.
AIM:
To write a menu driven python program to find factorial and sum of list of numbers using functions.
SOURCE CODE:
def factorial(no):
f=1
if no < 0:
print("Sorry, we cannot take factorial for negative numbers")
elif no == 0:
print("The factorial of 0 is", f)
else:
for i in range(1, no + 1):
f *= i
print("The factorial of", no, "is", f)
def sum_list(L):
total = 0
for i in range(len(L)):
total += L[i]
print("The sum of the list is:", total)
while True:
print('\n1. To find factorial')
print('2. To find sum of list of elements')
print('3. Exit')
opt = int(input('Enter your choice: '))
4
if opt == 1:
n = int(input('Enter a number to find factorial: '))
factorial(n)
elif opt == 2:
L=[]
n = int(input('Enter how many elements you want to store in list? '))
for i in range(n):
ele = int(input(f 'Enter element {i+1}: '))
L.append(ele)
sum_list(L)
elif opt == 3:
print("Exiting program.")
break
else:
print("Invalid choice. Please enter 1, 2, or 3.")
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
1. To find factorial
2. To find sum of list of elements
3. Exit
Enter your choice: 1
Enter a number to find factorial: 5
The factorial of 5 is 120
1. To find factorial
2. To find sum of list of elements
3. Exit
Enter your choice: 2
Enter how many elements you want to store in list? 4
Enter element 1: 65
Enter element 2: 98
Enter element 3: 96
Enter element 4: 32
The sum of the list is: 291
5
Exp. No: 5 MATHEMATICAL FUNCTIONS
Creating a menu driven python program to implement Mathematical functions.
AIM:
To write a menu driven python program to implement Mathematical functions.
SOURCE CODE:
import math
def square(num):
s=math.pow(num,2)
return s
def log(num):
s=math.log10(num)
return s
def quad(x,y):
s=math.sqrt(x**2 + y**2)
return s
while True:
print('\n1.To find a Square of a number')
print('2.To find a Log of a number')
print('3.To find a Quad of a number')
print('4.Exit')
opt=int(input('Enter your choice:'))
if opt==1:
n=int(input('Enter a number:'))
print('The square of a number is:',square(n))
elif opt==2:
n=int(input('Enter a number:'))
print('The log of a number is:',log(n))
elif opt==3:
a=int(input('Enter a:'))
b=int(input('Enter b:'))
print('The quad of a number is',quad(a,b))
elif opt==4:
print("Existing Program")
6
break
else:
print("Invalid Choice. Please enter 1, 2, 3 or 4.")
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
1.To find a Square of a number
2.To find a Log of a number
3.To find a Quad of a number
4.Exit
Enter your choice:1
Enter a number:5
The square of a number is: 25.0
1.To find a Square of a number
2.To find a Log of a number
3.To find a Quad of a number
4.Exit
Enter your choice:2
Enter a number:10
The log of a number is: 1.0
Exp. No: 6 TEXT FILE HANDLING - I
Write a python program to read a text file ‘file1.txt’ line by line and display each word separated by ‘#’.
AIM:
To write a python program to read a text file ‘file1.txt’ line by line and display each word separated by
‘#’.
SOURCE CODE:
f=open('file1.txt','r')
content=f.readlines()
for line in content:
words=line.split()
for i in words:
print(i+'#',end=' ')
7
f.close()
RESULT:
Thus the above python program has been executed and the output is verified successfully.
Left side:
Name of the text file created in notepad: file1.txt
Content of the file: Learning programs in python develops our knowledge.
OUTPUT:
Learning# programs# in# python# develops# our# knowledge.#
Exp. No: 7 TEXT FILE HANDLING - II
Write a python program to read a text file ‘file1.txt’ and display the number of vowels / consonants /
lowercase / uppercase characters in the file.
AIM:
To write a python program to read a text file ‘file1.txt’ and display the number of vowels / consonants /
lowercase / uppercase characters in the file.
SOURCE CODE:
f=open('file1.txt','r')
content=f.read()
vowels=0
consonants=0
lower_case=0
upper_case=0
for ch in content:
if ch in 'aeiouAEIOU':
vowels += 1
if ch not in 'aeiouAEIOU' and ch.isalpha():
consonants += 1
if ch.islower():
lower_case += 1
if ch.isupper():
upper_case += 1
f.close()
print('The total number of vowels in the file:', vowels)
print('The total number of consonants in the file:',consonants)
print('The total number of lowercase letters in the file:',lower_case)
8
print('The total number of uppercase letters in the file:',upper_case)
RESULT:
Thus the above python program has been executed and the output is verified successfully.
Left side:
Name of the text file created in notepad: file1.txt
Content of the file: Learning programs in python develops our knowledge.
OUTPUT:
The total number of vowels in the file: 15
The total number of consonants in the file: 29
The total number of lowercase letters in the file: 43
The total number of uppercase letters in the file: 1
Exp. No: 8 TEXT FILE HANDLING - III
Write a python program to read lines from a text file ‘sample.txt’ and copy those lines into another file
which are starting with the alphabet ‘a’ or ‘A’.
AIM:
To write a python program to read lines from a text file ‘sample.txt’ and copy those lines into another file
which are starting with the alphabet ‘a’ or ‘A’.
SOURCE CODE:
f1=open('sample.txt','r')
f2=open('new.txt','w')
while True:
line=f1.readline()
if line == '':
break
if line[0] == 'a' or line[0] == 'A':
f2.write(line)
print("All lines which are starting with character 'a' or 'A' \nhas been successfully copied into 'new.txt'")
f1.close()
f2.close()
9
RESULT:
Thus the above python program has been executed and the output is verified successfully.
Left side:
Name of the text file created in notepad: sample.txt
Content of the file:
Alex eagerly looks at outer space with his telescope.
The balloon popped.
Artists play guitars loudly during music shows.
The bat hung upside down in the tree.
OUTPUT:
All lines which are starting with character 'a' or 'A'
has been successfully copied into 'new.txt'
Name of the text file created in notepad: new.txt
Content of the file:
Alex eagerly looks at outer space with his telescope.
Artists play guitars loudly during music shows.
Exp. No: 9 TEXT FILE HANDLING - IV
Write a method Disp( ) in Python to read lines from sample.txt and display those words which are less than
5 characters.
AIM:
To write a method Disp( ) in Python to read lines from sample.txt and display those words which are less
than 5 characters.
SOURCE CODE:
def Disp():
f=open('sample.txt')
s=f.read()
w=s.split()
print("The following words are less than 5 characters:")
for i in w:
10
if len(i)<5:
print(i,end=' ')
f.close()
Disp()
RESULT:
Thus the above python program has been executed and the output is verified successfully.
Left side:
Name of the text file created in notepad: sample.txt
Content of the file:
Alex eagerly looks at outer space with his telescope.
The balloon popped.
Artists play guitars loudly during music shows.
The bat hung upside down in the tree.
OUTPUT:
The following words are less than 5 characters:
Alex at with his The play The bat hung down in the
Exp. No: 10 RECORDS IN BINARY FILE - I
Write a program in python to create a binary file with roll number and name, search for given roll number
and display the name. If not found, display appropriate message.
AIM:
To write a program in python to create a binary file with roll number and name, search for given roll
number and display the name. If not found, display appropriate message.
SOURCE CODE:
import pickle
def Create():
f = open('students.dat','ab')
opt = 'y'
while opt.lower() == 'y':
Roll_No = int(input('Enter the Roll Number:'))
Name = input('Enter the name:')
11
L = [Roll_No,Name]
pickle.dump(L,f)
opt = input('Do you want to add another student details (Y/N):')
f.close()
def Search():
f = open('students.dat','rb')
no = int(input('Enter the roll number of the student to be searched:'))
found = 0
try:
while True:
s = pickle.load(f)
if s[0] == no:
print('The searched Roll_No is found and details are', s)
found = 1
break
except:
f.close()
if found == 0:
print('The searched Roll_No is not found')
Create()
Search()
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
Enter the Roll Number:1
Enter the name: Abi
Do you want to add another student details (Y/N):Y
Enter the Roll Number:2
Enter the name: Raj
Do you want to add another student details (Y/N):y
Enter the Roll Number:3
Enter the name: Raja
12
Do you want to add another student details (Y/N): n
Enter the roll number of the student to be searched:1
The searched Roll_No is found and details are [1, 'Abi']
Enter the Roll Number:4
Enter the name: Krish
Do you want to add another student details (Y/N): N
Enter the roll number of the student to be searched: 6
The searched Roll_No is not found
Exp. No: 11 RECORDS IN BINARY FILE - II
Write a program in python to create a binary file with roll number and name, mark and update / modify
the mark for given roll number.
AIM:
To write a program in python to create a binary file with roll number and name, mark and update / modify
the mark for given roll number.
SOURCE CODE:
import pickle
def Create():
f = open('marks.dat','ab')
opt = 'y'
while opt.lower() == 'y':
Roll_No = int(input('Enter the Roll Number: '))
Name = input('Enter the name: ')
Mark = int(input('Enter the Mark: '))
L = [Roll_No,Name,Mark]
pickle.dump(L,f)
opt = input('Do you want to add another student details (Y/N): ')
f.close()
def Update():
f = open('marks.dat','rb+')
no = int(input('Enter the roll number of the student to be modify: '))
found = 0
13
try:
while True:
pos = f.tell()
s = pickle.load(f)
if s[0] == no:
print('The searched Roll_No is found and details are', s)
s[2] = int(input('Enter new mark to be updated:'))
f.seek(pos)
pickle.dump(s,f)
found = 1
f.seek(pos)
print('Mark updated successfully and details are:',s)
break
except:
f.close()
if found == 0:
print('The searched Roll_No is not found')
Create()
Update()
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
Enter the Roll Number:1
Enter the name: Raja
Enter the Mark: 89
Do you want to add another student details (Y/N):Y
Enter the Roll Number: 2
Enter the name: Amir
Enter the Mark: 98
Do you want to add another student details (Y/N): n
Enter the roll number of the student to be modify: 1
14
The searched Roll_No is found and details are [1, 'Raja', 89]
Enter new mark to be updated: 87
Mark updated successfully and details are: [1, 'Raja', 87]
Enter the Roll Number: 3
Enter the name: Raju
Enter the Mark: 65
Do you want to add another student details (Y/N): n
Enter the roll number of the student to be modify: 4
The searched Roll_No is not found
Exp. No: 12 CSV FILE HANDLING
Creating a Python program to create and search employee’s record in CSV file.
AIM:
To write a Python program to create a CSV file to store Empno, Name, Salary and search any Empno and
display Name, Salary and if not found display appropriate message.
SOURCE CODE:
import csv
def Create():
f = open('Emp.csv','a',newline = '')
w = csv.writer(f)
opt = 'y'
while opt.lower() == 'y':
No = int(input("Enter the Employee Number: "))
Name = input("Enter the Employee Name: ")
Sal = float(input("Enter the Employee Salary: "))
L = [No,Name,Sal]
w.writerow(L)
opt = input("Do you want to continue (Y/N)?: ")
f.close()
def Search():
f = open('Emp.csv', 'r', newline = '\r\n')
No = int(input("Enter the Employee Number to Search: "))
found = 0
15
row = csv.reader(f)
for data in row:
if data[0] == str(No):
print('\nEmployee Details are: ')
print('========================')
print('Name: ',data[1])
print('Salary: ',data[2])
print('========================')
found = 1
break
if found == 0:
print('The searched Employee Number is not found')
f.close()
Create()
Search()
RESULT:
Thus the above python program has been executed and the output is verified successfully.
OUTPUT:
Enter the Employee Number: 1
Enter the Employee Name: Somu
Enter the Employee Salary: 50000
Do you want to continue (Y/N)?: y
Enter the Employee Number: 2
Enter the Employee Name: Sundar
Enter the Employee Salary: 60000
Do you want to continue (Y/N)?: n
Enter the Employee Number to Search: 2
The searched Employee Number is not found
Employee Details are:
========================
Name: Sundar
16
Salary: 60000.0
========================
Enter the Employee Number: 3
Enter the Employee Name: Rakesh
Enter the Employee Salary: 800000
Do you want to continue (Y/N)?: n
Enter the Employee Number to Search: 4
The searched Employee Number is not found
17