Python Practical Ex1 EX14
Python Practical Ex1 EX14
Aim:
statement.
Algorithm:
Step 4: Write the formula for addition, subtraction, multiplication, division, floor
division and exponentiation with constant (a) and user input (b) variable.
Step 5: Print the result for addition, subtraction, multiplication, division, floor
a=5
add=a+b
sub=a+b
mul=a*b
div=a/b
fdiv=a//b
exp=a**b
print("\na\t\t\t:",a)
print("\nb\t\t\t:",b)
print("\n1.Addition\t\t\t:",add)
print("\n2.Subtraction\t\t:",sub)
print("\n3.Multiplication\t:",mul)
print("\n4.Division\t\t\t:",div)
print("\n5.Floor Division\t:",fdiv)
print("\n6.Exponentiation\t:",exp)
Output:
Result:
Thus the python program for arithmetic calculation from constant and user
Aim:
To find the biggest and smallest among three numbers using relational and
logical operator.
Algorithm:
Step 2: Get the three user input and store it in a, b and c variable.
if((a>b)and(a>c)):
elif((b>a)and(b>c)):
else:
if((a<b)and(a<c)):
elif((b<a)and(b<c)):
else:
Thus the program for finding biggest and smallest among three numbers
Aim:
Programming.
Algorithm:
Step 4: Check if English, Tamil, Maths, Science and Social Marks are greater
than or equal to 35
Step 5: If all the marks are greater than or equal to 35 Check the percentage.
i)If it is greater than 85 then print the result as “First Class with
Distinction”
ii)If it is greater than 60 and Less than 85 then print the result as “First
Class”
Step 6: If all the marks are less than 35 then print the result as “Fail”
Tot=Eng+Tam+Mat+Sci+SS
Per=Tot/5
if(Per>85):
elif((Per>60)and(Per<=85)):
else:
else:
Thus the program for student mark list using conditional statements has been
executed successfully.
FIBONACCI SERIES AND MULTIPLICATION TABLE
Aim:
To print fibonacci series and multiplication table using for loop in python
programming.
Algorithm:
Step 4: Step3 is repeated till the number of terms using for loop
Step 5: Get the multiplication table and number of terms from the user.
Step 6: The multiplication table is printed using for loop till the number of terms
is reached.
print('\n\t\tFibonacci sequence\n\n')
FV=0
SV=1
SR=FV+SV
print(FV,end='\t')
FV=SV
SV=SR
for i in range(1,t+1):
Thus the python program for finding fibonacci series and multiplication using
Aim:
To find prime numbers from starting range to ending range using jump
Algorithm:
Step 2: Get the starting range and ending range from the user.
as prime number.
for i in range(SR,ER):
c=2
for j in range(2,i):
if(i%j==0):
break
else:
c=c+1
if(c==i):
Thus the python program for finding prime numbers for range of numbers
Aim:
Algorithm:
Step 6: Find the square root of first matrix and second matrix.
Step 7: Print the square root of first matrix and second matrix.
import numpy as np
mat1 = np.array(m1).reshape(r1,c1)
mat2 = np.array(m2).reshape(r2,c2)
print(mat1)
print(mat2)
print(mat1+mat2)
print('\n\tSubtraction of two matrix\n\t')
print(mat1-mat2)
print(mat1*mat2)
print(np.sqrt(mat1))
print(np.sqrt(mat2))
Output:
Result:
Thus the python program for matrix operations using array has been executed
successfully.
STRING MANIPULATION
Aim:
To find the length, vowel count and palindrome checking of the string in
python programming.
Algorithm:
Step 2: Get the string from the user. Find the length of string and print it.
Step 4: Find the count of vowels in user input by incrementing the counter
Step 5: Store the user input in two variables S1 and S2 and the counter variable is
initialized as 0.
Step 6: The string S1 is checked from beginning index and string S2 is checked
vowels="aeiouAEIOU"
c=0
for i in range(0,len(S1)):
for j in range(0,len(vowels)):
if(S1[i]==vowels[j]):
c=c+1
S2=S1
c=0
for i in range(0,len(S1)):
if(S1[i]==S2[len(S1)-i-1]):
c=c+1
if(c==len(S1)):
print(S1+' is a palindrome')
else:
Thus the python program to find String manipulation has been successfully
executed.
EMPLOYEE PAY BILL DETAILS USING FUNCTION
Aim:
To find the employee pay bill detail using functions in python programming.
Algorithm:
Step 1: Start the python program by import statement for declaration of array.
Step 2: Employee Name, ID and Basic Salary are get as input from user.
Step 3: HRA, DA, PF and Gross Salary are calculated for all employee details
using function.
i)HRA=20%*BA
ii)DA=50%*BA
iii)PF=12%*BA
Step 4: All employee details Name, ID, Basic Salary, HRA, DA, PF and Gross
def display(empname,empid,BA):
HRA=BA*0.20
DA=BA*0.50
PF=BA*0.12
GS=BA+HRA+DA-PF
print('\n*******Employee Details**********\n')
for x in range(n):
print('\n**************************************\n')
print('\n DA :',DA[x])
print('\n PF :',PF[x])
print('\n**************************************\n')
m1=list(map(str,input().split()))
empname=np.array(m1)
print('Enter all employee IDs separated by space')
m2=list(map(str,input().split()))
empid=np.array(m2)
m3=list(map(int,input().split()))
BA=np.array(m3)
display(empname,empid,BA)
Output
Result:
Thus the python program for employee paybill details using function has been
executed successfully.
AREA OF SHAPES USING MODULES
Aim:
Algorithm:
Shapes.py
def circle(r):
area=3.14*r*r
def rectangle(l,b):
area=l*b
def square(a):
area=a*a
def triangle(b,h):
area=0.5*b*h
Main.py
import Shapes
i=1
while(i==1):
print('\n1.Area of Circle')
print('\n2.Area of Rectangle')
print('\n3.Area of Square')
print('\n4.Area of Triangle')
if(n==1):
Shapes.circle(r)
elif(n==2):
Shapes.rectangle(l,b)
elif(n==3):
Shapes.square(a)
else:
Shapes.triangle(b,h)
Thus the area of circle, rectangle, square and triangle using module python
Aim:
Algorithm:
Step 3: Factorial function is called for each array element using for loop.
import numpy as np
def fact(n):
if(n<=0):
return 0
elif(n==1):
return 1
else:
return n*fact(n-1)
m=list(map(str,input().split()))
n=np.array(m)
for i in range(0,len(n)):
t=int(n[i])
Aim:
Algorithm:
Step 3: Append a new element into list and print the list.
mylist=[2.0,4.67,"Ram",'R',1,1]
print('\nList1:',mylist)
mylist.append("HELLO")
print('\nAppended List1:',mylist)
mylist1=[2.7,4,"Anu"]
print('\nList2:',mylist1)
mylist1.remove(4)
print('\nDeleted List2:',mylist1)
mylist2=mylist+mylist1
print('\nList3:',mylist2)
mynestlis=[[1,2,3],[4.0,6,9.2],['ram',2]]
print('\n Nested ist:',mynestlis)
print("\nExtracting element in nested ist",mynestlis[0][1])
print("\nExtracting element in nested ist",mynestlis[1][2])
print("\nExtracting element in nested ist",mynestlis[2][0])
print('\nLength of tuple:',len(mynestlis))
print('\nMaximum value:',max(mynestlis[0]))
print('\nMinimum vaue:', min(mynestlis[1]))
print('\nCount of 1 in List1:', mylist.count(1))
print('\nIndex of 1 in List1:', mylist.index(1))
Output:
Result:
Thus the python program to find manipulation of the list has been executed
successfully.
TUPLE MANIPULATION
Aim:
Algorithm:
mytup=(2.0,4.67,"Ram",'R',1,1)
print('\nTuple1:',mytup)
mytup1=(2.7,4,"Anu")
print('\nTuple2:',mytup1)
mytup2=mytup+mytup1
print('\nTuple3:',mytup2)
mynestup=((1,2,3),(4.0,6,9.2),('ram',2))
print(mynestup[0][1])
print(mynestup[1][2])
print(mynestup[2][0])
print('\nLength of tuple:',len(mynestup))
print('\nMaximum value:',max(mynestup[0]))
Thus the python program for manipulation of tuple has been executed
successfully.
DICTIONARY MANIPULATION
Aim:
Algorithm:
Step6: Remove dictionary element using key and print the dictionary.
places=dict()
for i in range(0,6,2):
places[a.split()[i]]=a.split()[i+1]
print(places)
print(len(places))
print(places["TN"])
places["KR"]="TIRUVANDAPURAM"
print(places)
places["KK"]="BANGALORE"
print(places)
places["AN"]="HYDERABAD"
print(places)
del places["AN"]
print(places)
Output:
Result:
Thus the python program for manipulation of dictionary has been executed
successfully.
FILE HANDLING
Aim:
Algorithm:
Step 3: Write contents to the file using write function and print the contents using
read function.
Step 4: Close the file pointer and open the same file in read mode.
Step 5: Using seek function insert the content at start,current position and end of
the file.
file1=open("E:\renuga.txt","w+")
file1.write('FILE WRITING')
print(file1.read())
file1.close()
file2=open("E:\renuga.txt","r+")
print(file2.read())
file2.seek(6)
print(file2.tell())
print(file2.read())
file2.seek(0,0)
file2.write("welcome")
file2.seek(0,1)
file2.write("welcome")
file2.seek(0,2)
file2.write("welcome")
file2.seek(0)
print(file2.read())
file2.close()
Output:
Result:
Thus the python program for file handling has been executed successfully.