Professional Documents
Culture Documents
LABORATORY MANUAL
PYTHON PROGRAMMING
CS-506
V SEM (CSE)
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
DEPARTMENT OF
COMPUTER SCIENCE AND ENGINEERING
CERTIFICATE
Vishwavidhyalaya, Bhopal for …V…… Semester of the Computer Science and Engineering Department
Signature of
Faculty In-charge
LIST OF EXPERIMENTS
13 13 Write a program to find the most frequent words in a text using file handling.
Program:
def compute_hcf(x,y):
if x >y:
smaller =y
else:
smaller=x
for I in range(1, smaller+1):
if((x%i==0) and (y%i==0)):
hcf=i
return hcf
num1=54
num2=24
print(“The Hcf is “,compute_hcf(num1,num2))
Output
The Hcf is 6
Viva Questions:
1. What is Python?
EXPT. No.- 2. Write a Python program to find the square root of a number by Newton’s Method.
Square root expression is rephrased as a parabola that can be optimized via Newtonian optimization. The
same can be applied to higher order roots. Optimal point for this function is the square root for “a”. Let us
implement this in Python.
Program:
def squareRoot(n,1):
x=n
count=0
while(1):
count+=1
root = 0.5*(x+(n/x))
if(abs(root-x)<1):
break
x=root
return root
if_name_==”_main_”:
n=327
l=0.00001
print(squareRoot(n,1))
Output:
18.0831
Since in Python, there is always more than one way of achieving things calculating power with the
exponentiation operator is also possible. The exponentiation operator x**y evaluates to power.
Program:
import math
print(math.exp(65))
print(math.exp(-6.89))
Output:
1.6948892444103338e+28
0.0010179138409954387
Viva Questions:
1. What is type conversion in Python?
2. Is indentation required in Python?
3. What is the difference between Python Array and List?
4. How to define functions in Python?
5. How can we write comments in Python?
EXPT. No.- 4. Write a Python program to find the maximum from a List of numbers.
Output:
How many number?3
Enter number:8
Enter number:10
Enter number:94
largest element of the list is :94
Viva Questions:
1. Define List Data Structure in Python.
2. Give the difference between List and Tuple.
3. What types of values are contained in the List?
4. Is Python List mutable or not?
5. What are the applications of the List?
Theory: In computer science, a linear search or sequential search is a method for finding an element within a
list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
Here is the algorithm:
Step 1: Start
Step 2: Read n elements into the list.
Step 3: Read the element to be searched
Step 4: If a List[pos]==item, then print the position of the item
Step 5: Else increment the position and repeat step 3 until pos reaches the length of the list.
Step 6: Stop
Program:
def linearSearch(array,n,x):
for I in range(0,n):
if(array[i]==x):
return i
return -1
array=[2,4,0,1,9]
x=1
n=len(array)
result=linearSearch(array,n,x)
if(result==-1):
print(“element not found”)
else:
print(“Element found at index”,result)
Output:
Element found at index 3
Viva Questions:
1. Define range() function in Python.
2. How many types of loops available in Python?
3. What is Python Decorators?
4. What is “Pass” in Python?
5. Explain “is” and “is not” in Python.
Viva Questions:
1. Explain condition statement in Python.
2. How to use “elif” in Python?
3. What is “Doc.” string?
4. What is dictionary in Python?
5. What is a number data type in Python?
Program:
def selectionSort(array, size):
Output:
Sorted Array in Ascending Order:
[-9, -2, 0, 11, 45]
Viva Questions:
1. What is function in Python?
2. How many types of function in Python?
3. What is the use of append () function?
4. What is “def” keyword in Python?
5. What is lambda function?
Algorithm:
Step 1 – Start.
Step 5− Shift all the elements in the sorted sub-list that is greater than the value to be sorted.
Step 8 – Stop.
Program:
def insertionSort(arr):
key = arr[i]
j = i-1
arr[j+1] = arr[j]
j -= 1
insertionSort(arr)
for i in range(len(arr)):
Output:
Sorted array is 5,6,11,12,13
Viva Questions:
1. What is map() function in Python?
2. How to use filter() function in Python?
3. How to use reduce() functions in Python?
4. What is the use of “pass” in Python?
5. What are the iterators in Python?
mergeSort(arr,0,n-1)
print ("\n\nSorted array is")
for i in range(n):
print ("%d" %arr[i]),
Output:
Given array is
12
11
13
5
6
7
Sorted array is
5
6
7
11
12
13
Program:
import numpy as np
Viva Questions:
1. What is “numpy” in Python?
2. Explain “generator” in Python.
print(type(sys.argv))
print('The command line arguments are:')
for i in sys.argv:
print(i)
Output:
<class 'list'>
The command line arguments are:
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py
-f
C:\Users\darkw\AppData\Roaming\jupyter\runtime\kernel-2b1e58d5-d4d8-4bf9-a663-
b783548dbd3d.json
Viva Questions:
1. What is the difference between “Xrange” and “range”?
2. What is “package” in Python?
3. What type casting in Python?
4. What is “constructor” in Python?
5. How to delete a file in Python?
EXPT. No. - 13. Write a program to find the most frequent words in a text using file handling.
Program:
k=0
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter==l):
k=k+1
print(k)
Viva Questions:
1. What is file handling in Python?
2. What is data set in Python?
3. How to use “open()” function in Python?
4. What is “pandas” in Python?
5. How to use “close()” function in Python?
Viva Questions:
1. How to use”split” function in Python?
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:
Viva Questions:
1. How to use ”init()” function in Python?
2. What is class in python?
3. What is object in python?
4. What is set in python?
5. What is math module in python?