Problem Solving and PythonProgramming RECORD
Problem Solving and PythonProgramming RECORD
DATE:
Aim:
To write a python program to find the weight of the steel bar.
Algorithm:
Step 1: Start
Step 2: Read the Diameter d
Step 3: Calculate w = (d*d)/162
Step 4: Print d
Step 5: Stop
Program:
Output:
Result:
The Python Program to find the weight of steel bar has been successfully executed
and output is verified.
EX NO: 1B ELECTRICITY BILLING
DATE:
Aim:
To develop an algorithm, flowchart and program to compute electricity bill.
Algorithm:
Program:
Output:
Result:
Thus the algorithm and flow chart to compute electricity bill has been solved.
EX NO : 2A EXCHANGE THE VALUES OF TWO VARIABLES
DATE:
Aim:
To write a python program to exchange the value of two variables.
Algorithm:
Step 1: Start the program
Step 2:Declare two variables
Step 3: Get the value of X,Y
Step 4: Declare a temporary variable temp
Step 5 : Exchange the value of X,Y using temp variable
Step 6: Print the value of X,Y
Step 7: Stop
Program:
temp = x
x=y
y = temp
print("Value of x:", x)
print("Value of y:", y)
Output:
Enter value of x 10
Enter value of y 50
Value of x: 50
Value of y: 10
Result:
Thus the python program to exchange the values of two variables was successfully
executed.
EX NO :2B CIRCULATE THE VALUES OF N VARIABLES
DATE :
Aim:
To write a python program to circulate the values of n variables.
Algorithm:
Step 1: Start the program
Program:
n = int(input("Enter number of values : "))
list1 = []
for i in range(0,n,1):
ele = int(input("Enter integer : "))
list1.append(ele)
print("Circulating the elements of list ", list1)
for i in range(0,n,1):
ele =
list1.pop(0)
list1.append(ele)
print(list1)
Output:
Result:
Thus the python program circulate the values of n variables was successfully executed.
EX NO :2C DISTANCE BETWEEN TWO POINTS
DATE :
Aim:
To write a python program to find the distance between two points.
Algorithm:
Step 1: Start the program
Step 2:Declare the input variables
Step 3: Get the input variables from the user
Step 4 : Calculate the distance using formula ((x2 - x1 )**2) + ((y2-y1)**2) )**0.5
Step 5: Print the result
Step 6: Stop
Program:
x1=int(input("enter x1 : "))
x2=int(input("enter x2 : "))
y1=int(input("enter y1 : "))
y2=int(input("enter y2 : "))
result=((((x2-x1)**2)+((y2-y1)**2))**0.5)
print("Distance : ",result)
OUTPUT:
enter x1 : 4
enter x2 : 6
enter y1 : 0
enter y2 : 6
Distance : 6.324555320336759
Result:
Thus the python program to calculate the distance between two points was successfully executed
EX NO:3A FIBONACCI SERIES
DATE:
Aim:
To write a python program to print the Fibonacci series.
Algorithm:
Step 1: Start the program
Step 2: Get the total numbers of positive terms from the user
Step 3: If the nterms is then 1 then return n2 Else print the Fibonacci
series using while loop count<nterms
Step 4: Print the result
Step 5: Stop
Program:
Result:
Thus the python program to compute Fibonacci series was successfully executed.
EX.NO:3B SIMPLE NUMBER TRIANGLE PATTERN
DATE:
Aim:
To write a python program to print a simple triangle pattern using numbers
Algorithm:
Step 1:Start the program
Step2:Get the number of rows to be printed from the user
Step3: First outer loop is used to handle number of rows and Inner nested loop is used to
handle the number of columns.
Step 4:Manipulating the print statements display the number pattern Step5:
Stop the program
Program:
n = int(input("Enter the number of rows: "))
for i in range(1, n+1):
for j in range(1, i + 1):
print(j, end=' ')
print("")
Output:
1
12
123
1234
Result:
Thus the python program to print a simplenumbertriangle pattern was successfully executed.
EX.NO.3C PROGRAM FOR PYRAMID PATTERN
DATE:
Aim:
To write a python program to print Pyramid pattern
Algorithm:
Step 1:Start the program
Step 2:Get the number of rows to be printed
Step 3: First outer loop is used to handle number of rows and Inner nested loop is used to
handle the number of columns.
Step 4:Manipulating the print statements display the star pattern
Program:
rows = 5
for i in range(0, rows): for j in
range(0, i + 1):
print("*", end=' ') print("\r")
**
***
****
*****
****
***
**
*
Result:
Thus the python program to print pyramid pattern was successfully
Executed.
EX NO :4A FACTORIAL OF A NUMBER
DATE:
Aim:
To write the python program to find out the factorial of a number using functions.
Algorithm:
Step 1: Start the program
Step 2:Get an integer to find the factorial from the user Step
3:Read the integer and assign it to a variable
Step 4:If the integer entered is negative display the message the factorial Step
5:If the value entered is 0 or 1 then the factorial is 1
Step 6:Else calculate the factorial using formula n*factorial(n-1) Step
7:Stop
Program:
def factorial(num):
fact=1
for i in range(1,num+1):
fact=fact*i
return fact
Output:
Enter the number: 4
The factorial of 4 is 24
Result:
Thus the python program to calculate factorial of a number was successfully executed.
EX NO :4B AREA OF SHAPES
DATE:
Aim:
To write the python program to find out the area of various shapes using
functions.
Algorithm:
Step 1: Start the program
Step 2:Get the shape for which you want to calculate the area from the user
Step 3:Using conditionals compute the area of square, circle , rectangle and triangle
using formula.
Step 4:Display the area of the shape entered by the user.
Step 5:Stop
Program:
def areacalculator():
val=input("enter the shape you want to calculate area of:") area=0
pie=3.14
if val=="square":
side= int(input("enter the value of side:"))
area=area+(side**2)
print(area)
elif val== "circle":
radius= int(input("enter the value of radius:"))
area=area+(2*pie*radius)
print(area)
elif val== "rectangle":
length= int(input("enter the value of length:"))
width= int(input("enter the value of width:"))
area=area+(length*width)
print(area)
elif val== "triangle": print(area)
base= int(input("enter the value of base:")) height=
int(input("enter the value of height:"))
area=area+(0.5*base*height)
print(area) else:
print("Enter valid shape")
areacalculator()
Output:
Result:
Thus the python program to calculate area of different shapes was successfully executed.
EX NO:5A CHARACTER COUNT
DATE:
Aim:
To write the python program for character count
Algorithm:
Step 1: Start the program
Step 2:Read the input string
Step 3: Calculate the count of the character
Step 4: Print the count and display the output
Step 5:Stop the program
Program:
counter = test_str.count(c)
print("Count of '{}' is :".format(c)+str(counter))
Output:
Count of 'p' is :3
Result:
Thus the python program to character count was executed successfully and the
output was verified.
EX NO : 5B PALINDROME
DATE:
Aim:
To write the python program to find out whether a given string is a palindrome
or not.
Algorithm:
Step 1: Start the program
Step 2:Read the string
Step 3:Hold the string in a temporary variable.
Step 4: Reverse the string
Step 5:Compare the temporary variable with reversed string.
Step 6:If both letters or numbers are the same, print "this string is a palindrome."
Step 7:Else print, "this string is not a palindrome.
Step 8:Stop the program
Program:
Thus the python program to find out whether a string a given string is
palindrome or not was executed successfully and the output was verified.
EX NO:6 CONSTRUCTION OF A BUILDING USING
LISTS
DATE:
Aim:
To write a python program to perform various operations of list.
Algorithm:
Step 1: Start the program
Step 2: Declare the components of construction using lists
Program:
thislist[1] = "Tiles"
print(thislist)
thislist.insert(2, "bricks")
print(thislist)
thislist.append("butterfly tiles")
print(thislist)
tropical = ["floor", "marbel", "granite"]
thislist.extend(tropical)
print(thislist) thislist.remove("floor")
print(thislist)
thislist.pop(1)
print(thislist) i = 0
whilei<len(thislist):
print(thislist[i])
i=i+1
thislist.sort()
print(thislist)
thislist.sort(reverse=True)
print(thislist)
Output:
['bricks', 'cement', 'brush', 'sand', 'Paint'] 5
cement Paint
['cement', 'brush']
<class 'list'>
Yes, 'cement' is in the list
['bricks', 'Tiles', 'brush', 'sand', 'Paint']
['bricks', 'Tiles', 'bricks', 'brush', 'sand', 'Paint']
['bricks', 'Tiles', 'bricks', 'brush', 'sand', 'Paint', 'butterfly tiles']
['bricks', 'Tiles', 'bricks', 'brush', 'sand', 'Paint', 'butterfly tiles', 'floor', 'marbel', 'granite']
['bricks', 'Tiles', 'bricks', 'brush', 'sand', 'Paint', 'butterfly tiles', 'marbel', 'granite']
['bricks', 'bricks', 'brush', 'sand', 'Paint', 'butterfly tiles', 'marbel', 'granite'] bricks
bricks
brushsand
Paint
butterfly tiles
marbel granite
['Paint', 'bricks', 'bricks', 'brush', 'butterfly tiles', 'granite', 'marbel', 'sand']
['sand', 'marbel', 'granite', 'butterfly tiles', 'brush', 'bricks', 'bricks', 'Paint']
Result:
Thus the python program to execute operations of lists was successfully
executed.
EX NO: 7 COMPONENTS OF A CAR USING TUPLES
DATE:
Aim:
To write the python program to perform components of car using tuples
Algorithm:
Step 1: Start the program
Step 2: Declare the components of car using tuples
Program:
y = list(thistuple)
y[1] = "kiwi"
thistuple = tuple(y)
print(thistuple)
y = list(thistuple)
y.append("Brakes")
thistuple=tuple(y)
print(thistuple)
y = list(thistuple)
y.remove("Battery")
thistuple = tuple(y)
print(thistuple)
i=0
while i < len(thistuple):
print(thistuple[i])
i=i+1
print(thistuple)
Output:
('Steering wheel', 'Seat belt', 'Speedometer', 'Battery', 'Windscreen')
5
<class 'tuple'>
Seat belt
Windscreen
('Speedometer', 'Battery', 'Windscreen')
('Steering wheel', 'Seat belt', 'Speedometer', 'Battery')
('Speedometer', 'Battery', 'Windscreen')
('Seat belt', 'Speedometer', 'Battery')
Yes, 'Seat belt' is in the tuple
('Steering wheel', 'kiwi', 'Speedometer', 'Battery', 'Windscreen')
Windscreen Brakes
('Steering wheel', 'kiwi', 'Speedometer', 'Windscreen', 'Brakes')
Result:
Thus the python program to execute operations of tuples was successfully executed.
EX NO:8 OPERATIONS OF SET
DATE:
Aim:
To write the python program to operations of set.
Algorithm:
Program:
add_set={0,1,2,3} print(add_set)
add_set.update('4','5')
print("\n after adding element",add_set)
add_set.remove(0)
print("\n after removing element",add_set)
A = {0, 2, 4, 6, 8}
B = {1, 2, 3, 4, 5}
print("\nUnion :", A | B)
print ("\nIntersection :", A & B)
print ("\nDifference :", A - B)
print("\nSymmetric difference :", A ^ B)
Output:
{0, 1, 2, 3}
after adding element {0, 1, 2, 3, '5', '4'}
after removing element {1, 2, 3, '5', '4'}
Union : {0, 1, 2, 3, 4, 5, 6, 8}
Intersection : {2, 4}
Difference : {0, 8, 6}
Symmetric difference : {0, 1, 3, 5, 6, 8}
Result:
Thus the python program to operations of set was successfully executed.
EX NO :9 OPERATIONS OF DICTIONARY
DATE:
Aim:
To write the python program to operations of dictionary
Algorithm:
Program:
Dict = {}
print(Dict)
Dict[0] = 'python'
Dict[2] = 'java'
Dict[3]= „c‟
print("\
nDictionaryafteradding 3
elements: ")
print(Dict)
Dict['Value_set'] = 2, 3, 4
print(Dict)
Dict[2] = 'Welcome'
print(Dict)
print(Dict)
print("\nAccessing a element using key:")
print(Dict[0])
print(Dict[5]['Nested'])
del Dict[3]
print(Dict)
pop_ele = Dict.pop(5)
Empty Dictionary:
{}
{0: 'python', 2: 'Welcome', 3: 1, 'Value_set': (2, 3, 4), 5: {'Nested': {'1': 'Life', '2': 'python'}}}
python
{0: 'python', 2: 'Welcome', 'Value_set': (2, 3, 4), 5: {'Nested': {'1': 'Life', '2': 'python'}}}
DATE:
Aim:
To write a python program to implement copy from one file to another using FileHandling
Algorithm:
Program:
Result:
Thus the program to copy from one File to another using File Handling is executed
and the output is verified.
EX NO:10B FIND THE LONGEST WORD IN A FILE
DATE:
Aim:
To write a python program to implement Find the longest word in a File
Algorithm:
Step 3: Declare the fin and Read the file and find max length in a file
Program:
fin = open("test.txt","r")
str = fin.read()
words =str.split()
max_len =len(max(words,key=len)) for
word in words:
if len(word)==max_len:
longest_word =word
Result:
Thus the program to Find the longest word in a File is executed and the output is verified.
EX NO:11A DIVIDE BY ZERO ERROR USING EXCEPTION
HANDLING
DATE:
Aim:
To write a python program to implement Divide by zero using Exception handling
Algorithm:
Program:
a = int(input(“Enter a:”)
b = int(input(“Enter b:”)
c = a/b
print(“c:”,c)
except
print("Can't divide with zero")
Output:
Result:
Thus the program to execute divide by zero is executed and the output is verified.
VOTER’S AGE VALIDITY USING EXCEPTION
EX NO: 11B
HANDLING
DATE:
Aim:
To write a python program to implement voter’s age validity using Exception handling
Algorithm:
Step 1: Start the program.
Step 2: Declare the variables to get the input age from user
Step 3: Inside the try block print the eligibility to vote based on the condition
Step 4: Catch the exception and display the appropriate message.
Program:
def main():
age=int(input("Enter your age:"))
if age>18:
print("Eligible to vote") else:
print("Not eligible to vote")
except:
main()
Output:
Enter your age: 21
Eligible to vote
Result :
Thus the program to implement student mark validation using exception handling isexecuted
and the output is obtained
EX NO:12 PANDAS
DATE:
Aim:
To write a python program to print the series using an inbuilt package pandas.
Algorithm:
Step 1: Start the program
Program:
import pandas as pd
A=[“python”,‟maths”,‟physics”,‟chemistry”,‟english”]
df=pd.Series(A)
print(df)
Output:
0 python
1 maths
2 physics
3 chemistry
4 English
dtype : object
Result:
Thus the python program to print a series using pandas was executed
successfully and the output was verified.
EX NO: 13 NUMPY
DATE:
Aim:
To write a python program to perform matrix addition using an inbuilt package
numpy.
Algorithm:
Step 1: Start the program
Step 2: Import the numpy package
Step 3: Get the input array to perform matrix addition
Step 4: Using np.add operation perform matrix addition for the input numbers
Step 5: Print the output matrix
Step 6:Stop the program
Program:
import numpy as np
from matplotlib import pyplot as plt
x = np.arange(3,21) y = 2
*x+8
Output:
Result:
python program to perform matrix addition using numpy was executed successfully and the output
was verified.
EX NO: 14 MATPLOTLIB
DATE:
Aim:
To write a python program to plot a graph using an inbuilt package matplotlib.
Algorithm:
Step 1: Start the program
Step 2: Import the matplotlib package
Step 3: Get the input array to plot the graph
Step 4: Using plt.plot operation plot the numbers graph
Step 5: Display the graph using plt.show()
Step 6:Stop the program
Program:
xdata=['A','B','C']
ydata=[1,3,5]
plt.bar(range(len(xdata)),ydata)
plt.title('BarPlot')
plt.show()
Output:
Result:
Thus the python program to plot a graph using matplotlib was executed
successfully and the output was verified.
EX NO: 15 SCIPY
DATE:
Aim:
To write a python program to perform integration using an inbuilt package scipy.
Algorithm:
Step 1: Start the program
Step 2: Import the scipy package
Step 3: Get the input function to perform integration
Step 4:
Perform the integration operation
Step 5: Display the output
Program:
Output:
2.0
2.220446049250313e-14
Result:
Thus the python program to perform integration using scipy was executed
successfully and the output was verified
EX NO: 16 BOUNCING BALL USING PYGAME
DATE:
Aim:
To write a python program to simulate bouncing ball using pygame.
Algorithm:
Step 1: Import the necessary files for the implementation of this bounce
ball game
Step 2: Set the display mode for the screen using
Step 4: Open the image using pygame.image.load() method and set the ball rectangle
boundary using get_rect() method.
Step 5: Set the speed of the ball
import turtle
wn=turtle.Screen()
wn.bgcolor("white")
wn.title("ball")
ball = turtle.Turtle()
ball.shape("circle")
ball.color("red")
ball.penup()
ball.speed(0)
ball.goto(0,200)
ball.dy = 0
gravity = 0.1
while True:
ball.dy -=gravity
ball.sety(ball.ycor() + ball.dy)
Output:
Result:
Thus the program to simulate bouncing ball using pygame is executed and the output is
sobtained.