Python Programs Lab Manual

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

LABORATORY MANUAL

Programming and Problem Solving Laboratory


Subject Code: 110005
First Year of Engineering
(2019 Pattern) Semester-I

TEACHING SCHEME EXAMINATION SCHEME


LECTURES: 03 Hours/Week Term Work: 25 Marks
PRACTICAL: 02 Hours/Week

CONTENTS
S. List of programs Page
NO. No.
1. Calculate salary of an employee given his basic pay (taken as
input from user). Calculate the gross salary of the employee.
Let HRA be 10% of the basic pay and TA be 5% of the basic
pay. Let the employee pay professional tax as 2% of total
salary. Calculate the net salary payable after deductions.
2. To accept an object mass in kilograms and velocity in meters
per second and display its momentum. Momentum is
calculated as e = mc2 where m is the mass of the object and c
is its velocity
3. To accept students five courses marks and compute his/her
result. Student is passing if he/she scores marks equal to and
above 40 in each course. If student scores aggregate greater
than 75%, then grade is distinction. If aggregate is 60>= and
<75 then the grade is first division. If aggregate is 50>= and
<60, then the grade is second division. If aggregate is 40>=
and <50, then the grade is third division
4. To accept n numbers from user. Compute and display
maximum in the list, minimum in the list, sum and average of
the functions using built-in function
5. To check whether a input number is Armstrong number or
not. An Armstrong number is an integer with three digits such
that the sum of the cubes of its digits is equal to the number
itself
6. Write a python program that accepts a string from user and
perform following string operations
1. Calculate the length of the string
2. String reversal
3. Equality check of two strings
4. Check palindrome
5. Check substring
7. To copy the contents of one file to other. While copying
1. All the full stops should be replaced by commas
2. Lowercase to be replaced with uppercase
3. Uppercase to be replaced with lowercase
8. Write a python program to create a class car with two
attributes name and cost. Create two objects and display
information
9. Calculator with basic functions. Add more functionality such
as graphic user interface and complex calculations.
Course Objectives:
Prime objective is to give students a basic introduction to programming and problem
solving with computer language Python. And to introduce students not merely to the
coding of computer programs, but to computational thinking, the methodology of computer
programming, and the principles of good program design including modularity and
encapsulation.
 To understand problem solving, problem solving aspects, programming and to know about
various program design tools.
 To learn problem solving with computers
 To learn basics, features and future of Python programming.
 To learn data types, input output statements, decision making, looping and functions in
Python
 To learn features of Object Oriented Programming using Python
 To learn use and benefits of files handling in Python

Course Outcomes:
 On completion of the lab, learner will be able to–
 Apply skills in problem solving for finding solutions to real life problems.
 Identify methods and choose the most appropriate one for solving problems
 Create, run and manipulate Python Programs using core data structures like Lists,
Dictionaries and use Regular Expressions.
Experiment 1
Problem Statement: Calculate salary of an employee given his basic pay (taken as input
from user). Calculate the gross salary of the employee. Let HRA be 10% of the basic pay and
TA be 5% of the basic pay. Let the employee pay professional tax as 2% of total salary.
Calculate the net salary payable after deductions.
Algorithm:
Step 1: Accept the basic pay form the user
Step 2: If basicpay< 0
Step 3: Print Basic Pay cannot be negative
Step 4: Calculate hra
Step 5: Calculate ta
Step 6: Calculate totalsalary
Step 7: Calculate professionaltax
Step 8: Calculate salarypayable
Step 9: Print the total salarypayable
Code:
basicpay = float(input('Enter the Basic Pay: '))
if basicpay<0:
print("Basic Pay cannot be negative")
hra = basicpay*0.1
ta = basicpay*0.05
totalsalary = basicpay + hra + ta
professionaltax = totalsalary*0.02
salarypayable = totalsalary - professionaltax
print("Total Salary payable is: ",salarypayable)
Output:

Conclusion: Understood the basic concept of programming language and solved


mathematical calculations in programming approach with the help of formulas and equations
Experiment 2
Problem Statement: To accept an object mass in kilograms and velocity in meters per
second and display its momentum. Momentum is calculated as e = mc2 where m is the mass
of the object and c is its velocity
Theory:
Momentum is calculated as e = mc2 where m is the mass of the object and c is its velocity
Algorithm:
Step1: Start
Step2: Accept the value of mass and velocity from the user
Step3: Calculate the value of momentum using the formula e = mc2
Step4: Print the value of the momentum
Step 5: Stop
Code:
mass = float(input('Enter mass in kgs: '))
velocity = float(input('Enter velocity in m/s: '))
momentum = mass*(velocity**2)
print("Momentum of object is", momentum)
Output:

Conclusion: Understood the basic concept of programming language and solved


mathematical calculations in programming approach with the help of formulas and equations
Experiment 3
Problem Statement: To accept students five courses marks and compute his/her result.
Student is passing if he/she scores marks equal to and above 40 in each course. If student
scores aggregate greater than 75%, then grade is distinction. If aggregate is 60>= and <75
then the grade is first division. If aggregate is 50>= and <60, then the grade is second
division. If aggregate is 40>= and <50, then the grade is third division
Algorithm:
Step 1: Start
Step2: Accept the marks of students in 5 courses
Step 3: If marks are equal or above 40 in each course
Step 4: Print student pass
Step 5: elif student scores aggregate greater than 75%
Step 6: Print Distinction
Step 7: elif aggregate is >=60 and <75
Step 8: Print First Division
Step 9: elif aggregate is 50>= and <60
Step 10: Print Second Division
Step 11: else aggregate is 40>= and <50
Step 12: Print Third Division
Code:
S1 = int(input("Enter first subject marks:"))
S2 = int(input("Enter Second subject marks:"))
S3 = int(input("Enter third subject marks:"))
S4 = int(input("Enter fourth subject marks:"))
S5 = int(input("Enter fivth subject marks:"))
marks = (S1 + S2 + S3 + S4 + S5)/5
if S1 >= 40 and S2 >= 40 and S3 >= 40 and S4>= 40 and S5>=40:
if marks >= 75:
print("Student Passed with Distinction")
elif marks >= 60:
print("Student Passed with First Division")
elif marks >= 50:
print("Student Passed with Second Division")
elif marks >= 40:
print ("Student Passed with Third Division")
else:
print ("Student Failed")
Output:

Conclusion: Understood the basic concept of decision control statement and made use of ‘if
statement’ , ‘elif statement’ and ‘else statement’ to display the result of the student
Experiment 4
Problem Statement: To accept n numbers from user. Compute and display maximum in the
list, minimum in the list, sum and average of the functions using built-in function
Algorithm:
Step 1: Accept the number of elements in the list from the user
Step 2: Accept the number to be added to the list
Step 3: Append the number to the list
Step 4: Compute the maximum of the numbers in the list using inbuilt function max()
Step 5: Compute the minimum of the numbers in the list using inbuilt function min()
Step 6: Compute the sum of the numbers in the list using inbuilt function sum()
Step 7: Compute the average as sum/length of list
Step 8: Print the maximum, minimum, sum and average of the numbers in the list
Code:
n = int(input('Enter the number of elements: '))
nolist = []
for i in range(n):
num = int(input('Enter the number: '))
nolist.append(num)
maxno = max(nolist)
minno = min(nolist)
sumno = sum(nolist)
average = sumno/len(nolist)
print("Maximum number in the list is: ",maxno)
print("Minimum number in the list is: ",minno)
print("Sum of the numbers in the list is: ",sumno)
print("Average of the numbers in the list is: ",average)
Output:
Conclusion: Understood the basic concept of functions and used inbuilt functions like max(),
min(), sum() and len() to find the maximum, minimum, sum and average of the numbers in
the list
Experiment 5
Problem Statement: To check whether a input number is Armstrong number or not. An
Armstrong number is an integer with three digits such that the sum of the cubes of its digits is
equal to the number itself.
Algorithm:
Step 1: Start.
Step 2: READ a number as N
Step 3: sum=0
Step 4: temp=N
Step 5: IF temp> 0 go to step 5.1 ELSE go to step 6
Step 5.1: digit=temp%10
Step 5.2: sum = sum + digit*digit*digit
Step 5.3: temp=temp/10
Step 5.4: go to step 5
Step 6: IF N==sum THEN PRINT “Given Number is Armstrong Number” ELSE
PRINT
“Given Number is Not Armstrong Number”

Code:
def armstrong(num):
temp = num
sum = 0
while temp > 0:
digit = temp % 10
sum = sum + digit ** 3
temp = temp // 10
if num == sum:
print(num, "is Armstrong number")
else:
print(num, "is not Armstrong number")

num1 = int(input("Enter the number:"))


armstrong(num1)
Output:

Conclusion: Understood the basic concept of functions and used user defined function to
check if the number entered by user is Armstrong or not
Experiment 6
Problem Statement: Write a python program that accepts a string from user and perform
following string operations
6. Calculate the length of the string
7. String reversal
8. Equality check of two strings
9. Check palindrome
10. Check substring
Algorithm:
Step 1: Start
Step 2: Accept string from the user
Step 3: Ask the user to select the choice 1. Length 2. Reverse 3. Equality 4.
Palindrome 5. Check Substring
Step 4: If choice is 1 compute the length of string using len() and print the same
Step 5: If choice is 2 perform string reversal and print the reversed string
Step 6: If choice is 3 ask the user to input second string
Compare the two strings
Print the result of comparison
Step 7: If choice is 4 check if the string is palindrome and print the result
Step 8: If choice is 5 ask the user to input the substring
Check if the substring exists in the string
Print the result
Step 9: Else print invalid input
Code:
s = input('Enter a string: ')
while(1):
print("\n1. Length \n2. Reverse \n3. Equality of two strings \n4. Check palindrome \
n5. Check substring")
choice = int(input('Enter your choice: '))
if choice == 1:
print("Length of string is: ",len(s))
elif choice == 2:
print("Reverse of the string is: ",s[::-1])
elif choice == 3:
s2 = input('Enter the string to compare: ')
if s == s2:
print("Strings are equal")
else:
print("Strings are not equal")
elif choice == 4:
if s == s[::-1]:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
elif choice == 5:
s2 = input('Enter the substring to check:')
if s2 in s:
print("Substring is present")
else:
print("Substring is not present")
else:
print("Invalid input")
Output:
Conclusion: Understood the basic concept of strings and various operations that can be
performed on the strings. Used inbuilt string functions to perform the above-mentioned string
operations
Experiment 7
Problem Statement: To copy the contents of one file to other. While copying
1. All the full stops should be replaced by commas
2. Lowercase to be replaced with uppercase
3. Uppercase to be replaced with lowercase
Algorithm:
Step 1: Start
Step 2: Open the file input.txt in read mode
Step 3: Open the file output.txt in write mode
Step 4: For each line in the input.txt file
Swap cases
Replace fullstops by commas
Step 5: Write the new lines to output.txt
Code:
fin = open("input.txt", 'r')
fout = open("output.txt", 'w')

for line in fin:


line = line.swapcase()
line = line.replace(".",",")
fout.write(line)

Output:

Conclusion: Understood the basic concept of file handling in python. Learnt to open a text
file in different modes, perform operations on the file and copy the content from one file to
another.
Experiment 8
Problem statement: Write a python program to create a class car with two attributes name
and cost. Create two objects and display information
Algorithm:
Step 1: Start
Step 2: create a class cars
Step 3: define two attributes modelname and price
Step 4: create two objects of the class cars
Step 5: Display the information
Code:
class cars():
def __init__(self,modelname,price):
self.modelname = modelname
self.price = price
honda = cars('city', 900000)
tata = cars('tigor', 800000)

print("Model Name:", honda.modelname)


print("Price:",honda.price)
print("Model Name:",tata.modelname)
print("Price:",tata.price)
Output:

Conclusion: Understood the basic concept of object-oriented programming in python. Learnt


to create a class with attributes in python. Created objects for the defined class and displayed
the information
Mini Project
Problem Statement: To simulate simple calculator that performs basic tasks such as
addition, subtraction, multiplication and division
Algorithm:
Step 1: Write a function for addition of two numbers
Step 2: Write a function for subtraction of two numbers
Step 3: Write a function for multiplication of two numbers
Step 4: Write a function for division of two numbers
Step 5: Accept the choice from user 1. Addition 2. Subtraction 3. Multiplication 4.
Division
Step 6: Accept the two numbers from the user
Step 7: if choice is 1 call the function for addition
Step 8: if choice is 2 call the function for subtraction
Step 9: if choice is 3 call the function for multiplication
Step 10: if choice is 4 call the function for division
Step 11: if choice is not 1/2/3/4 print invalid input
Step 12: Print the output
Code:
def add(x,y):
return x + y
def subtract(x,y):
return x - y
def multiply(x,y):
return x * y
def divide(x,y):
return x / y

while(1):
print("Select Operation: ")
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")

choice = input('Enter choice 1/2/3/4: ')


num1 = int(input('Enter the first number: '))
num2 = int(input('Enter the second number: '))

if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
if choice == '2':
print(num1,"-",num2,"=", subtract(num1,num2))
if choice == '3':
print(num1,"*",num2,"=", multiply(num1,num2))
if choice == '4':
print(num1,"/",num2,"=", divide(num1,num2))
else:
print("Invalid input")

Output:

You might also like