max: max = a return max list1 = [5,3,8,6,4] print("Maximum element is:", max_of_list(list1)) Output: Maximum element is: 8 This program takes a list as input, initializes the max variable to the first element of the list. It then iterates through the list and compares each element to the current max. If an element is greater than max, it updates max to"> max: max = a return max list1 = [5,3,8,6,4] print("Maximum element is:", max_of_list(list1)) Output: Maximum element is: 8 This program takes a list as input, initializes the max variable to the first element of the list. It then iterates through the list and compares each element to the current max. If an element is greater than max, it updates max to">
[go: up one dir, main page]

0% found this document useful (0 votes)
83 views23 pages

KCS453 - Python Language Programming Lab: Name: Reg. No: 1900970130053 Year: 2 Sem: 4 Section: B

Here is a Python program to find the maximum of a list of numbers: import math def max_of_list(list1): max = list1[0] for a in list1: if a > max: max = a return max list1 = [5,3,8,6,4] print("Maximum element is:", max_of_list(list1)) Output: Maximum element is: 8 This program takes a list as input, initializes the max variable to the first element of the list. It then iterates through the list and compares each element to the current max. If an element is greater than max, it updates max to

Uploaded by

Ishika Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views23 pages

KCS453 - Python Language Programming Lab: Name: Reg. No: 1900970130053 Year: 2 Sem: 4 Section: B

Here is a Python program to find the maximum of a list of numbers: import math def max_of_list(list1): max = list1[0] for a in list1: if a > max: max = a return max list1 = [5,3,8,6,4] print("Maximum element is:", max_of_list(list1)) Output: Maximum element is: 8 This program takes a list as input, initializes the max variable to the first element of the list. It then iterates through the list and compares each element to the current max. If an element is greater than max, it updates max to

Uploaded by

Ishika Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

GALGOTIAS COLLEGE OF ENGINEERING AND TECHNOLOGY

GREATER NOIDA
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

B.Tech (Computer Science and Engineering)

KCS453 – Python Language Programming Lab


(2020-2021 – ODD)
Faculty Name: - Mr. Abhishek Tiwari

Name: ISHIKA GUPTA


Reg. No: 1900970130053
Year: 2nd
Sem: 4th
Section: B

Page | 1
GALGOTIAS COLLEGE OF ENGINEERING AND TECHNOLOGY
No 1, Knowledge Park-II, Greater Noida, Uttar Pradesh

LABORATORY RECORD NOTEBOOK

Department of Computer Science and Engineering

AKTU Register number - 1900970130053

Certified that this is a bonafide record of work done by the

candidate Ms. Ishika Gupta of B.Tech Computer Science and Engineering in KCS453 –

Python Language Programming laboratory during the academic year 2020-2021 ODD

Semester.

Staff-in-charge Head of the Department

University Examination held on …………..………….

Internal Examiner External Examiner

Page | 2
KCS453 – PYTHON LANGUAGE PROGRAMMING LAB

INDEX

Name: Ishika Gupta Year: 2nd Sec: B Batch: B2

S.NO. Name of Experiment Date Page No


1 Program to take command line arguments 4/04/21 4
2 Multiply Matrices 11/04/21 5-6
3 GCD of two numbers 18/04/21 7
4 Read Most frequent words in a text file 25/04/21 8
5 Square root of a number (Newton’s method) 2/05/21 9
6 Exponentiation (Power of a number) 9/05/21 10
7 Maximum of a list of numbers 16/05/21 11
8 Linear search 23/05/21 12-13
9 Binary search 30/05/21 14-15
10 Selection sort 7/06/21 16
11 Insertion sort 14/06/21 17
12 Merge sort 21/06/21 18-20
13 Prime numbers 28/06/21 21
14 Simulate bouncing ball using pygame 28/06/21 22-23

Page | 3
EXPERIMENT NO – 1

AIM: To write a python program to take command line arguments (word count).

Algorithm:

Step 1: Start
Step 2: Take the file name from user.
Step 3: Read each line from the file and split the lines to form a list of words.
Step 4: Find the length of items in a list and print it.

Program:
import sys
print("\n\n\t Script Name : ",sys.argv)
print("\n")
le=len(sys.argv)
for i in range(1,le):
print("\t Word : ",i," : ", sys.argv[i])
print("\n\n\t Word Count : ", len(sys.argv)-1)

Output:

Script Name : ['c:\\Users\\dell\\OneDrive\\Documents\\python lab\\cmdlineargs.py']

Result: Thus, the python code for word count was created and executed successfully.

Page | 4
EXPERIMENT NO – 2

AIM: To write a python program to multiply two matrices.

Algorithm:

Step 1. Start
Step 2. Declare variables and initialize necessary variables.
Step 3. Enter the elements of matrices by row wise using loops.
Step 4. Check the numbers of rows and columns of first and second matrices.
Step 5. If number of rows of first matrix is equal to the number of columns of second matrix
go to step 6, otherwise, print matrix multiplication are not possible and go to step 3.
Step 6. Multiply the matrices using nested loops.
Step 7. Print the product in matrix form as console output.
Step 8. Stop.

Program:

X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]
for i in range(len(X)):
for j in range(len(Y[0])):
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]

Page | 5
Result: Thus, the program to multiply two matrices was executed successfully.

Page | 6
EXPERIMENT NO – 3

AIM: To write a python program to compute GCD of two numbers.

Algorithm:

Step 1. Start
Step 2. Take two numbers from the user as input.
Step 3. Calculate the remainder d1%d2
Step 4. While the remainder is not equal to 0
Step 5. d1=d2
Step 6. d2= remainder
Step 7. remainder=d1%d2
Step 8. Print the GCD
Step 9. Stop

Program:

def gcd(a,b):
if(b==0):
return a
else:
return gcd(b,a%b)

a=int(input("Enter first number: "))


b=int(input("Enter second number: "))
print("GCD of ",a," and ",b," is: ",gcd(a,b))

Output:

Enter first number: 60


Enter second number: 48
GCD of 60 and 48 is: 12

Result: Thus, the program to find GCD of two numbers is executed successfully.

Page | 7
EXPERIMENT NO – 4

AIM: To write a python program to find the most frequent words in a text read from a file.

Algorithm:
Step 1. Start
Step 2. Create a file in a notepad and save it with .txt extension.
Step 3. Take the file name and letter to be counted from the user.
Step 4. Read each line from the file and split the line to form a list of words.
Step 5. Use a for loop to traverse through the words in the list and another for loop to traverse the
letters in the word.
Step 6. Check if the letter provided by the user and the letter encountered over iteration is equaland if
they are, increment the letter count.
Step 7. Stop

Program:

from collections import Counter


def frequent(name):
with open(name) as file:
return Counter(file.read().split())

print ("Most frequent word")


print ("File name: ")
name=input()
print (frequent(name))

test.txt:
Galgotias college of engineering and technology
Galgotia
College

Output:

File name:
test.txt
Counter({'Galgotias': 1, 'college': 1, 'of': 1, 'engineering': 1, 'and': 1, 'technology': 1, 'Galgotia': 1, 'College':
1})

Result: Thus, the program to find the most frequent words in a text read file was executed successfully.

Page | 8
EXPERIMENT NO – 5

AIM: To write a python program to find square root of a number using Newton’s Method.

Algorithm:

Step 1. Start
Step 2. The first parameter is the value whose square root will be approximated.
Step 3. The second is the number of times to iterate the calculation yielding a better result from the
user.
Step 4. Calculate better = ½ * (approx. + n/approx.)
Step 5. Print the final result.
Step 6. Stop

Program:

def newton_squareroot(number, number_iters = 100):

a = float(number)

for i in range(number_iters):

number = 0.5 * (number + a / number)

return number

n = int(input("Enter a number: "))


print ("Square root of ",n," is: ",newton_squareroot(n))

Output:

Enter a number: 25
Square root of 25 is: 5.0

Result: Thus, the program to find the square root of a number using Newton’s Method was executed successfully.

Page | 9
EXPERIMENT NO – 6

AIM: To write a python program to find the exponentiation (power of a number).

Algorithm:

Step 1. Start
Step 2. r->n
Step 3. Read base number n
Step 4. Read exponent number e
Step 5. For i->1 to e
Step 5.1. Compute r->n*r
Step 6. Print exponent r
Step 7. End

Program:

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


e=int(input("Enter the exponent:"))
r=n
for i in range(1,e):
r=n*r
print ("Exponent",r)

Output:

Enter the number:3


Enter the exponent:2
Exponent 9

Result: Thus, the program to find the exponentiation (power of a number) was executed successfully.

Page | 10
EXPERIMENT NO – 7

AIM: To write a python program to find the maximum of a list of numbers.

Algorithm:

Step 1. Start
Step 2. Take n – the number of elements and store it in a variable.
Step 3. Take the elements of the list one by one.
Step 4. Print the last element of the list.
Step 5. End

Program:

n=int(input("Enter the list size: "))


a=[]
for i in range(n):
num=int(input("Enter the number: "))
a.append(num)
print (a)
max=a[0]
for i in range(n):
if(max<a[i]):
max=a[i]
print ("Maximum number is: ",max)

Output:
Enter the list size: 5
Enter the number: 1
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
[1, 2, 3, 4, 5]
Maximum number is: 5

Result: Thus, the program to find the maximum number from the list was executed successfully.

Page | 11
EXPERIMENT NO – 8

AIM: To write a python program to search an element in an array using Linear Search.

Algorithm:

Step 1. Start
Step 2. Get list of elements from the user.
Step 3. Get the number to be searched.
Step 4. Set i to 1.
Step 5. If i>n then go to step 7.
Step 6. If A[i]=x then go to step 6.
Step 7. Set i to i+1.
Step 8. Go to step 2.
Step 9. Print element x found at index i and step 8.
Step 10. Print element not found.
Step 11. End

Program:

n=int(input("Enter the no. of element: "))


i=0
a=[i for i in range(n)]
for i in range(0,n):
a[i]=int(input("Enter the array element: "))
for i in range(0,n):
print (a[i])
key=int(input("Enter the key element to be searched: "))
for i in range(0,n):
if a[i]==key:
print ("key found")

Output:

Enter the no. of element: 5


Enter the array element: 10
Enter the array element: 20
Enter the array element: 30
Enter the array element: 40
Enter the array element: 50
10
20
30
40
50
Enter the key element to be searched: 20
key found

Page | 12
Result: Thus, the program to search an element in an array using Linear Search was executed successfully.

Page | 13
EXPERIMENT NO – 9

AIM: To write a Python program to search an element in a list of elements using Binary Search technique.

Algorithm:
Step 1. Start
Step 2. Get list of elements from the user.
Step 3. Get the number to be searched.
Step 4. Found<-false
Step 5. While not found and first <=top
if list[mid] = item sought then
item found = true
elif first>=last then
SearchFailed = true
elif list[mid] > item sought then
last = mid-1
else
first = mid+1
End if
End While
Step 6. Stop

Program:

def binary_search(a,n,key):
low=0
high=n
while(low<=high):
mid=int((low+high)/2)
if(key==a[mid]):
return mid
elif(key<a[mid]):
high=mid-1
else:
low=mid+1
return -1
n=int(input("Enter the no of element: "))
a=[i for i in range(n)]
for i in range(0,n):
a[i]=int(input("Enter the array element: "))
k=int(input("Enter the key element to be searched: "))
position=binary_search(a,n,k)
if(position!=-1):
print ("Present in: ",(position))
else:
print ("Not present")
Page | 14
Output:

Enter the no of element: 5


Enter the array element: 10
Enter the array element: 45
Enter the array element: 20
Enter the array element: 61
Enter the array element: 23
Enter the key element to be searched: 23
Present in: 4

Result: Thus, the program to search an element in an array using Binary Search was executed successfully.

Page | 15
EXPERIMENT NO – 10

AIM: To write a python program to sort the elements using selection sort.

Algorithm:
Step 1. Start
Step 2. Get elements from the user.
Step 3. Set min to location 0.
Step 4. Search the minimum element from the list.
Step 5. Swap with value at location min.
Step 6. Increment min to point to the next element.
Step 7. Repeat until list is sorted.
Step 8. End

Program:

def selectionSort(a):
for i in range(len(a)):
least=i
for k in range(i+1,len(a)):
if a[k]<a[least]:
least=k
temp=a[least]
a[least]=a[i]
a[i]=temp
a=[50,30,10,20,40,70,60]
print ("Original list",a)
selectionSort(a)
print("SelectionSort:",a)

Output:

Original list [50, 30, 10, 20, 40, 70, 60]


SelectionSort: [10, 20, 30, 40, 50, 60, 70]

Result: Thus, the program to sort the elements in a list using selection sort was executed successfully.

Page | 16
EXPERIMENT NO – 11

AIM: To write a python program to sort the elements using insertion sort.

Algorithm:

Step 1. Start
Step 2. Get the elements from the user.
Step 3. The second element in the list is compared with the elements that appear before it.
Step 4. If second element is smaller than the first element, second element is inserted in the position
of first element. After first step, first two elements of the array will be sorted.
Step 5. Pick next element.
Step 6. Repeat step 3 until all the elements are sorted.
Step 7. End

Program:

def insertion(a):
for i in range(1,len(a)):
currentvalue=a[i]
position=i
while position>0 and a[position-1]>currentvalue:
a[position]=a[position-1]
position=position-1
a[position]=currentvalue
a=[51,23,87,63,20,11]
print("Original list: ",a)
insertion(a)
print ("Insertion list: ",a)

Output:

Original list: [51, 23, 87, 63, 20, 11]


Insertion list: [11, 20, 23, 51, 63, 87]

Result: Thus, the program to sort the elements in a list using insertion sort was executed successfully.

Page | 17
EXPERIMENT NO – 12

AIM: To write a python program to sort the elements in a list using merge sort.

Algorithm:

Step 1. Start
mergeSort(A,p,r)
Step 2. If r > p
Step 3. Find the middle point to divide the array into two halves.
Then q=FLOOR[(p+r)/2]

Step 4. Call merge sort for first half.


Step 5. Call merge sort for second half.
Step 6. Merge the two halves sorted in step 2 and step 3.
Step 7. Stop

Program:

def mergeSort(myList):
print("Splitting ",myList)
if len(myList) > 1:
mid = len(myList) // 2
left = myList[:mid]
right = myList[mid:]

# Recursive call on each half


mergeSort(left)
mergeSort(right)
i = 0
j = 0
k = 0

while i < len(left) and j < len(right):


if left[i] <= right[j]:
myList[k] = left[i]
i += 1
else:
myList[k] = right[j]
j += 1
k += 1

while i < len(left):


myList[k] = left[i]
i += 1

Page | 18
k += 1

while j < len(right):


myList[k]=right[j]
j += 1
k += 1
print("Merging ",myList)

myList = [54,26,93,17,77,31,44,55,20]
mergeSort(myList)
print(myList)

Output:

Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20]
Splitting [54, 26, 93, 17]
Splitting [54, 26]
Splitting [54]
Merging [54]
Splitting [26]
Merging [26]
Merging [26, 54]
Splitting [93, 17]
Splitting [93]
Merging [93]
Splitting [17]
Merging [17]
Merging [17, 93]
Merging [17, 26, 54, 93]
Splitting [77, 31, 44, 55, 20]
Splitting [77, 31]
Splitting [77]
Merging [77]
Splitting [31]
Merging [31]
Merging [31, 77]
Splitting [44, 55, 20]
Splitting [44]
Merging [44]
Splitting [55, 20]
Splitting [55]
Merging [55]
Splitting [20]
Merging [20]
Merging [20, 55]
Merging [20, 44, 55]
Merging [20, 31, 44, 55, 77]
Merging [17, 20, 26, 31, 44, 54, 55, 77, 93]
[17, 20, 26, 31, 44, 54, 55, 77, 93]

Page | 19
Result: Thus, the program to sort the elements in a list using merge sort was executed successfully.

Page | 20
EXPERIMENT NO – 13

AIM: To write a python program to find the first n prime numbers.

Algorithm:
Step 1. Start
Step 2. Take the number to be checked and store it in a variable.
Step 3. Initialize the count variable to zero.
Step 4. Let the for loop range from 2 to half number (excluding 1 and the number itself).
Step 5. Then find the number of divisors using the if statement and increment the count variable.
Step 6. If the number of divisors is lesser than or equal to 0, the number is prime.
Step 7. Print the final result.
Step 8. End

Program:

n=int(input("Enter the limit:"))


for num in range(1,n):#iterate between 1 to n
for i in range(2,num): #iterate factors of thenum
if num%i==0:
r=num/i
break
else:
print ("Prime number:",num)

Output:

Enter the limit:7


Prime number: 1
Prime number: 2
Prime number: 3
Prime number: 5

Result: Thus, the program to find the first n prime numbers was executed successfully.

Page | 21
EXPERIMENT NO – 14

AIM: To write a python program to simulate bouncing ball using pygame.

Algorithm:

Step 1. Start
Step 2. Import necessary GUI for simulation.
Step 3. Set the window coordinates and ball coordinates.
Step 4. Assign various colours for the ball and set the base color.
Step 5. Fix the colour, shape and bouncing speed randomly.
Step 6. Write a function to move the base according to the ball position.
Step 7. Stop

Program:

import sys, pygame


pygame.init()
size = width, height = 800, 400
speed = [1, 1]
background = 255, 255, 255
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Bouncing ball")
ball = pygame.image.load("ball.jpg")
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:

Page | 22
Result: Thus, the program to simulate bouncing ball using pygame was executed successfully.

Page | 23

You might also like