PYTHON LAB PROGRAMS
1. Python program for Calculation of Simple Interest and Compound Interest
Algorithm:
STEP1:Read the values of principal amount, no of years and rate of interest
STEP 2: Calculate the simple interest using (principal*time*rate)/100
STEP3:Calculate the compound interest using principal*((1+rate/100)**time -1)
STEP 4: Display the two values
STEP5:Stop
Program:
principal=float(input('Enteramount:'))
time = float(input('Enter time: '))
rate = float(input('Enter rate: '))
simple_interest=(principal*time*rate)/100
compound_interest=principal*((1+rate/100)**time-1)
print('Simple interest is:',simple_interest)
print('Compound interest is:' , compound_interest)
Output:
Enter amount:20000
Enter time:5
Enter rate:6.7
Simple interest is:6700.0
Compound interest is:7659.994714602134
2. Python programs Biggest of three numbers
Algorithm:
Step1: Ask the user to enter three integer values.
Step2: Read the three integer values in num1,num2,andnum3.
Step 3: Check if num1 is greater than num2.
If true, then check if num1 is greater than num3.
If true, then print ‘num1’as the greatest number.
Step4: If false,then print‘num3’as the greatest number.
If false, then check ifnum2 isgreater than num3.
If true, then print ‘num2’ as the greatest number.
If false, then print ‘num3’asthe greatest number
Step5:Stop
Program:
num1 = float(input("Enter first number: "))
num2=float(input("Entersecondnumber:"))
num3 = float(input("Enter third number: "))
if(num1>num2)and(num1>num3):
largest = num1
elif(num2>num1)and(num2>num3):
largest = num2
else:
largest=num3
print("The largest number is",largest)
Output:
Enter first number: 55
Enter second number: 64
Enter third number: 34
Thelargestnumberis64.0
3. Python program for to check the given number is Perfect Number or Not
Algorithm:
Step 1: Start
Step2:Readn
Step 3: Initialize s=0
Step4: fori=1 ton do
if(n%i)==0,then
s=s+i
Step 5: if s==n
Then Print"GivenNumberisPerfectNumber".GotoStep7
Step 6: Print "Given Number is Not a Perfect Number"
Step 7: Stop
Program:
Number=int(input("Please Enter any Number: "))
Sum = 0
For I in range(1,Number):
if(Number % i == 0):
Sum = Sum + i
if(Sum ==Number):
print("%d is a Perfect Number"%Number)
else:
print("%d is not a Perfect Number"%Number)
Output:
Please Enter any Number:6
6 is a Perfect Number
4. Write python program to print different patterns in Python
Algorithm:
STEP1:Read the row and column values
STEP2:Using nested loops, print the required pattern
STEP 3: Stop
Program:
num_rows=int(input("Enter the number of rows"));
for i in range(0, num_rows):
for j in range(0,num_rows-i-1):
print(end="")
for j in range(0,i+1):
print("*", end="")
print()
Output:
Enter the number of rows: 5
*
**
***
****
*****
5. Write a Python program to print Pascals Triangle
Algorithm:
STEP 1: Read the number of rows to be printed
STEP 2: Using nested loop, print the spaces needed
STEP3: Using binomial coefficient,print the values
STEP 4: Stop
Program 1:
n=int(input("Enter the rows:"))
for i in range(1, n+1):
for j in range(0,n-i+1):
print('', end='')
c =1
for j in range(1, i+1):print('',C,sep='',end='')
C=C*(i-j)//j
print()
Output:Enter the rows:5
1
11
121
1331
14641
Program 2:
Algorithm:
STEP 1: Read the number of rows to be printed
STEP 2: Using nested loop, print the spaces needed
STEP3: Using binomial coefficient,print the values
STEP 4: Stop
#upward pyramid
for i in range(n):
For j in range(n-i-1):
print('', end='')
for j in range(2 * i + 1):
print(chr(65+j),end='')
print()
for i in range(n-1):
for j in range(i+1):
print('', end='')
for j in range(2*(n-i- 1)-1):
print(chr(65+j),end='')
print()
Output:
A
ABC
ABCDE
ABCDEFG
ABCDEFGHI
ABCDEFG
ABCDE
ABC
A
6. Write a python program to print Prime numbers in a range
Algorithm:
Step 1: Start
Step2:Read lower and upper range
Step3:Using a for loop,iterate on all the numbers in the range
Step 4 : For each number, check if its prime or not
Step5:If the number is prime,print else skip
Step 6 : Stop
Program:
lower=900
upper=1000
print("Prime numbers between",lower,"and",upper,"are:")
for num in range(lower, upper + 1):
if num >1:
for I in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Output:
Prime numbers between 900 and 1000 are:
907
911
919
929
937
941
947
953
967
971
977
983
991
997
7. Write a python Program to search student record using dictionary
Algorithm:
1. Createalistto storethestudent details
2. Readthedatasuch asrno,name, marks
3. Calculatethetotal,average
4. Convertthelist to the dictionary
5. Print thestudent information’s
6. Stop theProgram.
Program:
n=int(input("Pleaseenternumberofstudents:"))
all_students = []
for i in range(0, n):
stud_name=input('Enterthenameofstudent:')
print (stud_name)
stud_rollno=int(input('Entertherollnumberofstudent:'))
print (stud_rollno)
mark1=int(input('Enterthemarksinsubject1:'))
print (mark1)
mark2=int(input('Enterthemarksinsubject2:'))
print (mark2)
mark3=int(input('Enterthemarksinsubject3:'))
print (mark3)
total=(mark1+mark2+mark3) print("Total is: ", total)
average=total/ 3
print("Averageis:", average)
all_students.append({
'Name': stud_name,
'Rollno':stud_rollno,
'Mark1': mark1,
'Mark2': mark2,
'Mark3': mark3,
'Total': total,
'Average':average
})
for student in all_students: print ('\n')
for key,value in [Link]():
print (key, value)
OUTPUT:
ENTER ZERO NUMBER FOR EXIT!!!!!!!!!!!!
ENTERSTUDENTINFORMATION------------------------
enter roll no.. :: -- 1
entermarks1:: --77
entermarks2:: --88
entermarks3:: --77
Please enter number of students:2
Enter the name of student: Alex Alex
Enter the roll number of student:1 1
Enter the marks in subject1:99 99
Enter the marks in subject2:88 88
Enter the marks in subject3:88 88
Totalis:275
Average is: 91.66666666666667
Enter the name of student:Avin Avin
Enter the roll number of student:2 2
Enter the marks in subject1:77 77
Enter the marks in subject2:77 77
Enter the marks in subject3:77 77
Total is:231
Average is:77.0
Name Alex
Rollno 1
Mark199
Mark288
Mark388
Total275
Average91.66666666666667
8. Write a python program to find the sum of list of elements using function
Aim: To write a program to sum of a numbers in a list using function
H
Algorithm :
o Step 1: Start.
w Step2: Create a empty listl=[]
m Step 3: Read number of elements to store in a
a list. Step4: Get the input and store in a list
n using for loop Step 5: Make a call to sum_list()
y function.
n Step6: Initialize sum=0
u Step7: Iterate the elements using for loop and add the
m elements.
b Step 8: Return the sum
e Step9: Print
r the sum Step
s 10: Stop
:Program:
5 def sum_list(l):
sum=0
E for i in range(len(l)):
n sum=sum+l[i]
t return sum
e l=[]
r n=int(input("Enter the number of
elements")) for i in range(n):
n x=int(input("Enter the
u element")) [Link](x)
m result=sum_list
b (l) print(result)
eOutput:
r Enter the number of elements :5
Enter the element:10
1 Entertheelement: 20
0 Entertheelement: 30
. Entertheelement: 40
Entertheelement: 50
. 150
9. 9. Write a python program to implementing programs using Strings
Aim: To implement python program using strings.
ALGORITHM:
STEP1:Get the input from the user
STEP2:The function call is invoked, where the string is passed as input to reverse function
STEP3: For loop is executed until the last character in the string is reached
STEP4:The str variable is incremented for each iteration and returned as the reversed
string.
PROGRAM:
defreverse(s):
str = ""
for i in s:
str=i+str
return str
s =input("Enter the string to reverse:")
print("The original string is:",end="")
print (s)
print("The reversed string is:",end="")
print (reverse(s))
OUTPUT:
Enter the string to reverse: python
The original stringis : python
The reversed string is : nohtyp
10. Write a python program to get Student mark range validation
ALGORITHM:
STEP1:Getthenumberbetweenthe range(1 -100)
STEP2:Intheifblock,checkwhetherthenumberisbetween1to100. STEP3:
Otherwise, print “Above 100, wrong”
STEP4:If theageentered is anon-number, then theexception block is executed.
PROGRAM:
definput_number(min,max):
try:
while True:
n=int(input("Pleaseenteranumberbetween{}and{}:".format(min,max)))
n=int(n)
if(min<=n<=max):
print(n)
break
else:
print("Above100,wrong")
break
except:
print("markmustbeavalidnumber")
input_number(1,100)
OUTPUT1:
Please enter a number between 1and 100 :44
OUTPUT2:
Please enter a number between 1and 100 :156
Above100, wrong
OUTPUT3:
Please enter a number between 1 and 100: ab
mark must be a valid number