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">
KCS453 - Python Language Programming Lab: Name: Reg. No: 1900970130053 Year: 2 Sem: 4 Section: B
KCS453 - Python Language Programming Lab: Name: Reg. No: 1900970130053 Year: 2 Sem: 4 Section: B
GREATER NOIDA
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
Page | 1
GALGOTIAS COLLEGE OF ENGINEERING AND TECHNOLOGY
No 1, Knowledge Park-II, Greater Noida, Uttar Pradesh
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.
Page | 2
KCS453 – PYTHON LANGUAGE PROGRAMMING LAB
INDEX
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:
Result: Thus, the python code for word count was created and executed successfully.
Page | 4
EXPERIMENT NO – 2
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:
Page | 5
Result: Thus, the program to multiply two matrices was executed successfully.
Page | 6
EXPERIMENT NO – 3
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)
Output:
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:
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:
a = float(number)
for i in range(number_iters):
return number
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
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:
Output:
Result: Thus, the program to find the exponentiation (power of a number) was executed successfully.
Page | 10
EXPERIMENT NO – 7
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:
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:
Output:
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:
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:
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:
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]
Program:
def mergeSort(myList):
print("Splitting ",myList)
if len(myList) > 1:
mid = len(myList) // 2
left = myList[:mid]
right = myList[mid:]
Page | 18
k += 1
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
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:
Output:
Result: Thus, the program to find the first n prime numbers was executed successfully.
Page | 21
EXPERIMENT NO – 14
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:
Output:
Page | 22
Result: Thus, the program to simulate bouncing ball using pygame was executed successfully.
Page | 23