Panimalar Institute of Technology: Name of The Student:G Venkat
Panimalar Institute of Technology: Name of The Student:G Venkat
DEGREE : BE
BRANCH : CSE
YEAR / SEMESTER : I /I
Sign. of Staff-In-Charge
Date:
INDEX
PAGE STAFF
S.NO DATE NAME OF THE EXPERIMENT MARKS
NO SIGNATURE
BASIC PYTHON PROGRAMS
PYTHON PROGRAMS-MATHEMATICAL PROBLEM SOLVING
1. 5-12-2020 Arithmetical operations
2. 5-12-2020 Area and circumference of circle
3. 5-12-2020 Square root of a number
4. 5-12-2020 Swapping of two numbers
PYTHON PROGRAMS-SELECTION/DECISION STATEMENTS
5. 12-12-2020 To check leap year
6. 12-12-2020 To check a number for odd or even
12-12-2020
7. To check a number for positive or negative
8. 12-12-2020 To find largest among three numbers
PYTHON PROGRAMS-ITERATION/LOOPING STATEMENTS
9. 19-12-2020 To find factorial of a number
10. 19-12-2020 To find sum of digits of a number
19-12-2020 To find reverse of a number and check
11.
palindrome
19-12-2020 To print numbers divisible by 2 and not by 3
12.
and 5
PYTHON PROGRAMS-FUNCTIONS AND RECURSION
2-1-2021 Square and Cube of a number using
13.
functions
2-1-2021
14. To Find Armstrong number using Functions
2-1-2021
15. Fibonacci Series using Recursion
LABORATORY EXPERIMENTS
1. 9-1-2021 Program to compute the gcd/hcf of two
numbers
2. 9-1-2021 Program to find the square root of a number
using newton’s method
3. 9-1-2021 Program to find the exponentiation/power of
a number
4. 23-1-2021 Program to find the maximum of a list of
numbers
5. 23-1-2021 Program to search using linear search
6. 23-1-2021 Program to search using binary search
7. 30-1-2021 Program to sort using selection sort
8. 30-1-2021 Program to sort using insertion sort
9. 30-1-2021 Program to sort using merge sort
1 6-2-2021 Program to generate first n prime numbers
0.
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
PAGE STAFF
S.NO DATE NAME OF THE EXPERIMENT MARKS
NO SIGNATURE
1 6-2-2021 Program to perform matrix multiplication
1.
1 6-2-2021 Program for word count using command line
2. arguments
1 13-2-2021 Program to find the most frequent words in
3. a text read from a file
.
1 13-2-2021 Program to simulate elliptical orbits using
4. pygame
1 13-2-2021 Program to simulate bouncing ball using
5. pygame
OUTPUT:
Enter the radius of the circle:5
78.5
31.400000000000002
3.SQUAREROOT OF A NUMBER
num=float(input("Enter a number:"))
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
OUTPUT:
Enter a number:25
The square root of 25.000 is 5.000
OUTPUT:
Enter value of x: 10
Enter value of y: 20
The value of x after swapping: 20
The value of y after swapping: 10
OUTPUT:
Enter any Year: 2016
Year is Leap
rem = num % 2
if rem > 0:
print("Odd number.")
else:
print("Even number.")
OUTPUT:
Enter any number: 6
Even number.
7.POSITIVE OR NEGATIVE
n=int(input("Enter number: "))
if(n>0):
print("Number is positive")
else:
print("Number is negative")
OUTPUT:
Enter number: 5
Number is positive
OUTPUT:
Enter first number: 12
Enter second number: 15
Enter third number: 10
The largest number between 12.0 , 15.0 and 10.0 is 15.0
OUTPUT:
Enter a number: 5
The factorial of 5 is 120
num=int(input("Enter a number:"))
while num > 0:
r=num%10
sum=sum+r
num//=10
print("Sum of digits of number:",sum)
OUTPUT:
Enter a number:123
Sum of digits of number: 6
OUTPUT:
Enter a number to check :121
The reverse of the number is: 121
The given number is a palindrome number
Enter a number to check :123
The reverse of the number is: 321
The given number is not a palindrome number
if((i%2==0)and(i%3!=0)and(i%5!=0)):
print(i)
OUTPUT:
2
4
8
14
16
22
26
28
32
34
38
44
46
52
56
58
62
64
68
74
76
82
86
88
92
94
98
def square(x):
sq=x*x
return sq
def cube(x):
cu=x*x*x
return cu
x=5
print("Square value for the given number is :",square(x))
print("Cube value for the given number is :",cube(x))
OUTPUT:
def Armstrong_Number():
num = int(input('Enter Number to check for Armstrong'))
f = num
sum = 0
while(f!=0):
d = f % 10
f = f / 10
sum = sum+(d*d*d)
if( sum == num):
print('%d is a armstrong number' %num)
else:
print('%d is not a armstrong number' %num)
Armstrong_Number()
OUTPUT :
"""Recursive function to
print Fibonacci sequence"""
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
OUTPUT:
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
LABORATORY EXPERIMENTS
PROGRAM:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
OUTPUT:
Enter first number:8
Enter second number:12
GCD is:
4
PROGRAM:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
#Python program to find the square root of a number using newton's method using user
defined function
n= float(input("Enter a number to find the squareroot: "))
def nsquare_root(n):
estimate = n/2
newestimate = (estimate+(n/estimate))/2
while newestimate!=estimate:
estimate = newestimate
newestimate = (estimate+(n/estimate))/2
return(newestimate)
newestimate=nsquare_root(n)
print("The square root of the number using newton method is:",newestimate)
OUTPUT:
Enter a number to find the squareroot: 2
The square root of the number using newton method is: 1.414213562373095
PROGRAM:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
PROGRAM:
#Find the maximum of a list of number without inbuilt function and list
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
list=[100,20,60,80,10]
maxno=list[0]
for i in list:
if i>maxno:
maxno=list[i]
print("The maximum number in the list is",maxno)
OUTPUT:
The maximum number in the list is 100
PROGRAM
#Program for linear search
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
def linearsearch(list,search):
print("The elements in the list is",list)
n=len(list)
for i in range(0,n):
if(list[i]==search):
print("The element is present in the list at position",i)
break
else:
print("The element is not present in the list")
return
l=[20,100,60,120,140]
s=int(input("Enter the element to be searched in the list:"))
linearsearch(l,s)
OUTPUT:
Enter the element to be searched in the list:140
The elements in the list is [20, 100, 60, 120, 140]
The element is present in the list at position 4
On second execution:
Enter the element to be searched in the list:160
The elements in the list is [20, 100, 60, 120, 140]
The element is not present in the list
PROGRAM
OUTPUT:
enter upper limit5
enter the elements20
enter the elements40
enter the elements60
enter the elements80
enter the elements100
enter element to search100
element found in position 4
def selection_sort(a):
for i in range(0,len(a)):
min=i
for j in range(i+1,len(a)):
if (a[j]<a[min]):
min=j
temp=a[min]
a[min]=a[i]
a[i]=temp
a=[14,46,43,27,57,41,45,21,70]
print("The list of elements before sorting",a)
selection_sort(a)
print("The list of elements after sorting",a)
OUTPUT:
The list of elements before sorting
[14, 46, 43, 27, 57, 41, 45, 21, 70]
The list of elements after sorting
[14, 21, 27, 41, 43, 45, 46, 57, 70]
for i in range(1,len(a)):
j=i
while(j!=0 and a[j]<a[j-1]):
a[j],a[j-1]=a[j-1],a[j]
j=j-1
a=[14,46,43,27,57,41,45,21,70]
print("The list of elements before sorting",a)
insertion_sort(a)
print("The list of elements after sorting",a)
OUTPUT:
The list of elements before sorting [14, 46, 43, 27, 57, 41, 45, 21, 70]
The list of elements after sorting [14, 21, 27, 41, 43, 45, 46, 57, 70]
#Merge Sort
def merge(left,right):
result=[]
i=j=0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i=i+1
else:
result.append(right[j])
j=j+1
result=result+left[i:]
result=result+right[j:]
return result
def mergesort(a):
if(len(a)<=1):
return a
mid=int(len(a)/2)
left=mergesort(a[:mid])
right=mergesort(a[mid:])
return merge(left,right)
nlist = [14,46,43,27,57,41,45,21,70]
print("Before sorting",nlist)
print("After sorting",mergesort(nlist))
OUTPUT:
Before sorting [14, 46, 43, 27, 57, 41, 45, 21, 70]
After sorting [14, 21, 27, 41, 43, 45, 46, 57, 70]
10. PROGRAM TO GENERATE FIRST N PRIME NUMBERS
PROGRAM:
#First n prime numbers
limit =int(input("Enter the limit until which to print the prime numbers:"))
print("Prime numbers till the limit %d: "%(limit))
for num in range(2,limit + 1):
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
OUTPUT:
Enter the limit until which to print the prime numbers:10
Prime numbers till the limit 10:
2
3
5
7
# 3x3 matrix
X = [[12,7,3], [4 ,5,6],[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],[6,7,3,0], [4,5,9,1]]
# result is 3x4
result = [[0,0,0,0], [0,0,0,0],[0,0,0,0]]
# iterate through rows of X
for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
OUTPUT:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
PROGRAM:
#Program using command line arguments for word count
import sys
if len(sys.argv) != 2:
print('Usage: ./wc.py <filename>')
sys.exit(1)
num_words = num_lines = num_chars = 0
with open(sys.argv[1]) as infile:
for line in infile:
num_lines += 1
num_chars += len(line)
line = line.strip()
words = line.split()
num_words += len(words)
print('Number of Lines is %d' % num_lines)
print('Number of Words is %d' % num_words)
print('Number of Characters is %d' % num_chars)
OUTPUT:
Number of Lines is 2
Number of Words is 7
Number of Characters is 45
FROM A FILE
PROGRAM:
#Program for most frequent words in a text read from a file
s = open('sample1.txt','r').read()
num_chars = len(s)
num_lines = s.count('\n')
words = s.split()
d = {}
for w in words:
if w in d:
d[w] += 1
else:
d[w] = 1
num_words = sum(d[w] for w in d)
list1 = [(d[w],w) for w in d]
list1.sort()
list1.reverse()
print("Your input file has characters",num_chars)
print("Your input file has lines",num_lines)
print("Your input file has the following words = ",num_words)
print("\n The most frequent words are")
i=1
for count, word in list1[:50]:
print(i,count,word)
i+= 1
OUTPUT:
OUTPUT:
GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY
PANIMALAR INSTITUTE OF TECHNOLOGY DEPARTMENT OF CSE
PROGRAM:
#Simulation of bouncing ball using Pygame
import sys, pygame
pygame.init()
size = width, height = 600, 400
speed = [2, 2]
background = 255,255,255
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing Ball")
ball = pygame.image.load("ball.bmp")
ballrect = ball.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(background)
screen.blit(ball, ballrect)
pygame.display.flip()
OUTPUT: