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

Python Practical Ex1 EX14

The document describes several Python programs involving variables, constants, input/output, arithmetic calculations, finding the biggest/smallest of three numbers, student mark lists, Fibonacci series, prime numbers, and matrix operations. The key steps are to initialize variables, get user input, perform calculations, and output results. Functions are used to modularize code for employee pay bill details.

Uploaded by

renuga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Python Practical Ex1 EX14

The document describes several Python programs involving variables, constants, input/output, arithmetic calculations, finding the biggest/smallest of three numbers, student mark lists, Fibonacci series, prime numbers, and matrix operations. The key steps are to initialize variables, get user input, perform calculations, and output results. Functions are used to modularize code for employee pay bill details.

Uploaded by

renuga
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 63

VARIABLE, CONSTANTS, INPUT AND OUTPUT

Aim:

To develop a python program using variable, constant, input and output

statement.

Algorithm:

Step 1: Start the python program.

Step 2: Initialize a variable with constant value.

Step 3: Get the user input and store it in b variable.

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

division and exponentiation.

Step 6: End the program


Program:

a=5

b=int(input("\n\nEnter the number:"))

add=a+b

sub=a+b

mul=a*b

div=a/b

fdiv=a//b

exp=a**b

print('\t\t\tCalculation for Constant (a) and Input Variable (b)\n')

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

input has been executed successfully.


BIGGEST AND SMALLEST AMONG THREE NUMBER

Aim:

To find the biggest and smallest among three numbers using relational and

logical operator.

Algorithm:

Step 1: Start the python program.

Step 2: Get the three user input and store it in a, b and c variable.

Step 3: Check if a is greater than b and c then print “a is bigger”.

Step 4: Check if b is greater than a and c then print “b is bigger”.

Step 5: Otherwise print “c is greater”.

Step 6: Check if a is lesser than b and c then print “a is smaller”.

Step 7: Check if b is lesser than a and c then print “b is smaller”.

Step 8: Otherwise print “c is smaller”

Step 9: End the program


Program:

print('\n\t\tBiggest and Smallest among Three Numbers\n')

a=int(input("\nEnter the number1:"))

b=int(input("\nEnter the number2:"))

c=int(input("\nEnter the number3:"))

print('\n A value is :',a)

print('\n B value is :',b)

print('\n C value is :',c)

if((a>b)and(a>c)):

print('\nBiggest Number: ',a)

elif((b>a)and(b>c)):

print('\nBiggest Number: ',b)

else:

print('\nBiggest Number: ',c)

if((a<b)and(a<c)):

print('\nSmallest Number: ',a)

elif((b<a)and(b<c)):

print('\nSmallest Number: ',b)

else:

print('\nSmallest Number: ',c)


Output:
Result:

Thus the program for finding biggest and smallest among three numbers

using operator has been executed successfully.


STUDENT’S MARK LIST

Aim:

To develop a student’s mark list using conditional statements in python

Programming.

Algorithm:

Step 1: Start the python program.

Step 2: Get the student details as user input

Step 3: Find the total and percentage of all five subjects.

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”

iii)Otherwise print the result as “Second Class”

Step 6: If all the marks are less than 35 then print the result as “Fail”

Step 7: End the program


Program:

print('\n\t\t To Check the result is PASS or FAIL')

Nm=input("Enter the Name")

Rg=int(input("Enter the Register Number:"))

Eng=int(input("Enter the English Mark:"))

Tam=int(input("Enter the Tamil Mark::"))

Mat=int(input("Enter the Maths Mark::"))

Sci=int(input("Enter the Science Mark::"))

SS=int(input("Enter the Social Science Mark::"))

Tot=Eng+Tam+Mat+Sci+SS

Per=Tot/5

print('\n\t\t Name : ',Nm)

print('\n\t\t RegNo : ',Rg)

print('\n\t\t English : ',Eng)

print('\n\t\t Tamil : ',Tam)

print('\n\t\t Maths : ',Mat)

print('\n\t\t Science : ',Sci)

print('\n\t\t SocialScience : ',SS)

print('\n\t\t Total : ',Tot)

print('\n\t\t Percentage : ',Per)


if((Eng>=35)and(Tam>=35)and(Mat>=35)and(Sci>=35)and(SS>=35)):

if(Per>85):

print('Your Result is First Class with Distinction')

elif((Per>60)and(Per<=85)):

print('Your Result is First Class')

else:

print('Your Result is Second Class')

else:

print('Your Result is Fail')


Output:
Result:

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 1: Start the python program.

Step 2: i)Get the number of terms from user.

ii)Initialize the variable FV=0 and SV=1.

Step 3: Add FV and SV and store it in SR and FV value is printed.

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.

Step 7: End the program.


Program:

num = int(input("Enter the number of terms: "))

print('\n\t\tFibonacci sequence\n\n')

FV=0

SV=1

for i in range(0, num):

SR=FV+SV

print(FV,end='\t')

FV=SV

SV=SR

n=int(input("\n\nWhich multiplication table do you want to print? "))

t=int(input("\nEnter the number of terms: "))

for i in range(1,t+1):

print('\n', n,'\t*\t', i,"\t=\t", n*i)


Output:
Result:

Thus the python program for finding fibonacci series and multiplication using

for loop has been executed successfully.


PRIME NUMBERS USING JUMP STATEMENT

Aim:

To find prime numbers from starting range to ending range using jump

statement in python programming.

Algorithm:

Step 1: Start the python program.

Step 2: Get the starting range and ending range from the user.

Step 3: The number is initialized from starting range to ending range.

Step 4: The number is divided from 2 to number-1.

Step 5: If there is no remainder for division from 2 to number-1 then it is printed

as prime number.

Step 6: The step2 to step4 is repeated till the ending range.

Step 7: End the program.


Program:

SR=int(input('Enter the starting range to find prime number'))

ER=int(input('Enter the ending range to find 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):

print(i,'is a prime number')


Output:
Result:

Thus the python program for finding prime numbers for range of numbers

using jump statement has been executed successfully.


MATRIX OPERATIONS USING ARRAY

Aim:

To perform matrix operations using arrays in python programming.

Algorithm:

Step 1: Start the python program.

Step 2: Get the two matrices as input from user.

Step 3: Print the two matrices given by the user.

Step 4: Perform addition, subtraction, multiplication with two matrices.

Step 5: Print addition, subtraction, multiplication of two matrices.

Step 6: Find the square root of first matrix and second matrix.

Step 7: Print the square root of first matrix and second matrix.

Step 8: End the program.


Program:

import numpy as np

print('Enter the data for matrix1')

r1=int(input('Enter the number of rows '))

c1=int(input('Enter the number of columns'))

print('Enter elements separated by space ')

m1= list(map(int, input().split()))

mat1 = np.array(m1).reshape(r1,c1)

print('Enter the data for matrix2')

r2=int(input('Enter the number of rows'))

c2=int(input('Enter the number of columns'))

print('Enter elements separated by space')

m2= list(map(int, input().split()))

mat2 = np.array(m2).reshape(r2,c2)

print('\n\t Matrix1 \n\t')

print(mat1)

print('\n\t Matrix2 \n\t')

print(mat2)

print('\n\tAddition of two matrix\n\t')

print(mat1+mat2)
print('\n\tSubtraction of two matrix\n\t')

print(mat1-mat2)

print('\n\tMultiplication of two matrix\n\t')

print(mat1*mat2)

print('\n\tSquare root of matrix1\n\t')

print(np.sqrt(mat1))

print('\n\tSquare root of matrix2\n\t')

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 1: Start the python program.

Step 2: Get the string from the user. Find the length of string and print it.

Step 3: Store the vowels in a variable.

Step 4: Find the count of vowels in user input by incrementing the counter

variable for each vowel in user input.

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

from ending index.

Step 7: If S1 equals S2 count is incremented by 1.

Step 8: If the counter variable equals length of S1 then String is printed as

palindrome otherwise it is printed as not palindrome.

Step 9: End the program.


Program:

S1=input('Enter the string to check palindrome: ')

print('Length of the String: ' ,len(S1))

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

print('Vowel Count in the String: ' ,c)

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:

print(S1+' is not a palindrome')


Output:
Result:

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

iv)Gross Salary= Basic Salary+HRA+DA-PF

Step 4: All employee details Name, ID, Basic Salary, HRA, DA, PF and Gross

Salary are displayed.

Step 5: End of the Program.


Program:

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 Name :',empname[x])

print('\n Basic Salary :',BA[x])

print('\n HRA :',HRA[x])

print('\n DA :',DA[x])

print('\n PF :',PF[x])

print('\n Gross Salary :',GS[x])

print('\n**************************************\n')

n=int(input("Enter the number of employees:"))

print('Enter all employee names separated by space')

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)

print('Enter all employee Basic Salary separated by space')

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:

To find the area of shapes using modules in python programming.

Algorithm:

Step 1: Start the python program.


Step 2: Create a module named shape.py and define function for calculating area of circle,
rectangle, square and triangle.
Step 3: Import the module shape in main program.
Step 4: If the user chooses
1. Area of Circle
i) Get the radius as user input
ii) In module shape, circle function is called.
iii) Area of circle is printed
2. Area of Rectangle
i) Get the length and breadth as user input
ii) In module shape, rectangle function is called.
iii) Area of Rectangle is printed
3. Area of Square
i) Get the side as user input
ii) In module shape, square function is called.
iii) Area of square is printed
4. Area of Triangle
i) Get the base and height as user input
ii) In module shape, triangle function is called.
iii) Area of Triangle is printed.
Step 5: This is repeated until user wishes to calculate area
Step 6: End of the Program.
Program:

Shapes.py

def circle(r):

area=3.14*r*r

print('\nArea of the circle: ',area)

def rectangle(l,b):

area=l*b

print('\nArea of the rectangle: ',area)

def square(a):

area=a*a

print('\nArea of the square: ',area)

def triangle(b,h):

area=0.5*b*h

print('\nArea of the triangle: ',area)

Main.py

import Shapes

i=1

while(i==1):

print('\n Which Shape do you want to calculate area?\n')

print('\n1.Area of Circle')
print('\n2.Area of Rectangle')

print('\n3.Area of Square')

print('\n4.Area of Triangle')

n=int(input("\nEnter the choice : "))

if(n==1):

r=float(input("\nEnter the radius :"))

Shapes.circle(r)

elif(n==2):

l=float(input("\nEnter the length :"))

b=float(input("\nEnter the breadth :"))

Shapes.rectangle(l,b)

elif(n==3):

a=float(input("\nEnter the Side :"))

Shapes.square(a)

else:

b=float(input("\nEnter the base :"))

h=float(input("\nEnter the height :"))

Shapes.triangle(b,h)

print('\n Do you want to calculate area? 1.Yes 2.No\n')

i=int(input("\nEnter the choice : "))


Output
Result:

Thus the area of circle, rectangle, square and triangle using module python

program has been executed successfully.


FACTORIAL USING RECURSION

Aim:

To find factorial using recursion in python programming.

Algorithm:

Step 1: Start the program.

Step 2: Get the array of number from the user.

Step 3: Factorial function is called for each array element using for loop.

Step4: In factorial function

i)if n<=0 then return 0

ii)if n==1 then return 1

iii)otherwise return n*fact(n-1)

Step 5: End of the Program.


Program:

import numpy as np

def fact(n):

if(n<=0):

return 0

elif(n==1):

return 1

else:

return n*fact(n-1)

print('Enter numbers separated by space')

m=list(map(str,input().split()))

n=np.array(m)

for i in range(0,len(n)):

t=int(n[i])

print('\nFactorial of the number',t,'is :',fact(t))


Output
Result:

Thus the python program has been executed successfully.


LIST MANIPULATION

Aim:

To create a python program for list manipulation.

Algorithm:

Step 1: Start the program.

Step 2: Initialize the list with different types of elements.

Step 3: Append a new element into list and print the list.

Step 4: Delete an element and print the list.

Step 5: Concatenate two list and create a new list.

Step 6: Create a nested list and extract elements in the list.

Step 7: Find length, maximum, minimum of the list elements.

Step 8: Find the count of element and index of an element.

Step 9: End of the Program.


Program

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:

To create a python program for tuple manipulation.

Algorithm:

Step 1: Start the program.

Step 2: Initialize two tuples.

Step 3: Create a new tuple by concatenating two tuples.

Step 4: Create a nested tuple and extract the element of tuple.

Step 5: Find length, minimum and maximum of tuple.

Step 6: Find Count and index position of an tuple element.

Step 7: End of the Program.


Program:

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]))

print('\nMinimum vaue:', min(mynestup[1]))

print('\nCount of 1 in tuple1:', mytup.count(1))

print('\nIndex of 1 in tuple1:', mytup.index(1))


Output:
Result:

Thus the python program for manipulation of tuple has been executed

successfully.
DICTIONARY MANIPULATION

Aim:

To create a python program for dictionary manipulation.

Algorithm:

Step 1: Start the program.

Step 2: Initialize a dictionary by getting user input.

Step 3: Print the dictionary.

Step 4: Print the value of dictionary using key.

Step 5: Create a new dictionary element and print the dictionary.

Step6: Remove dictionary element using key and print the dictionary.

Step7: End of the Program.


Program:

places=dict()

a=input("Enter Key & Value Separated by space: ")

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:

To create a python program for file handling.

Algorithm:

Step 1: Start the program.

Step 2: Open a new file in write mode.

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.

Step6: Print the contents of file

Step7: End of the Program.


Program:

file1=open("E:\renuga.txt","w+")

file1.write('WELCOME TO FILE HANDLING')

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.

You might also like