PYTHON PROGRAMMING
LAB MANUAL
REGD NO:
Certificate
Certified that this is a bonafide Record of practical work done by
Mr./Kumari ________________________________
Of Class in Laboratories
Of College during the year
NO.of expt.done and certified:
Lecturerin-Charge: Head of Department
Date:
Week-1
1)Write a program to compute distance between two points taking input from the
user (using Pythagorean Theorem).
Program:-
x1 = (int(input("Enter the x coordinate of the first point : ")))
y1 = (int(input("Enter the y coordinate of the first point : ")))
x2 = (int(input("Enter the x coordinate of the second point : ")))
y2 = (int(input("Enter the y coordinate of the second point : ")))
distance = ((x2-x1)**2+(y2-y1)**2)**0.5
print("Distance = ")
print(distance)
output:-
Week-2
a. Write a program to purposefully raise Indentation Error and correct it.
Program:- indentation Error:-
age = 21
print("you can vote")
output:-
Indentation correction:-
Program:-
age = 21
print("you can vote")
output:-
b. Write a Program for checking whether the given number is an even number or not
program:-
num = int(input("enter the number:"))
if (num%2==0):
print(num,"is even")
else:
print(num,"is not even")
output:-
c. Write a program using a while loop that asks the user for a number, and prints a countdown from
that number to zero
program:-
n = int(input("enter the value of n : "))
while n>=0:
print(n,end =' ')
n = n-1
output:-
d. Using a for loop, write a program that prints out the decimal equivalents of
1/2, 1/3, 1/4, . . . ,1/10.
Program:-
i=1;
for j in range(2,11):
print("i:",i,"j:",j)
print(i,"/",j)
print (i/j);
output:-
Week-3(Program Organization, Control Flow)
a. Write a function that takes in a string and a number and prints the string that number of times.
Program:-
def f(string, n, c=0):
if c < n:
print(string * n)
f(string, n, c=c + 1)
f('abc', 3)
Output:-
b. Write a script that prints prime numbers from 100 to 500.
Program:-
for Number in range (100, 500):
count = 0
for i in range(2, (Number//2 + 1)):
if(Number % i == 0):
count = count + 1
break
if (count == 0 and Number != 1):
print(" %d" %Number, end = ' ')
output:-
c. Write a program that takes 2 numbers as command line arguments and prints its sum.
Program
import sys;
n1=int(sys.argv[1]);
n2=int(sys.argv[2]);
print (n1+n2)
output:-
Week-4(Functions)
a. Write a function ball_collide that takes two balls as parameters and computes if they are colliding. Your function should
return a Boolean representing whether or not the balls are colliding.Hint: Represent a ball on a plane as a tuple of (x, y, r), r
being the radius If (distance between two balls centers) <= (sum of their radii) then (they are colliding)
Program:-
import math
def ball_collide(x1,y1,r1,x2,y2,r2):
dist=math.sqrt((x2-x1)**2+(y2-y1)**2);
print("Distance b/w two balls:",dist)
center=dist/2;
print("Collision point",center);
r=r1+r2;
print("Sum of radious",r)
if(center<=r):
print("They are Colliding")
return True;
else:
print("Not Colliding")
return False;
c=ball_collide(4,4,3,2,2,3)
print(c)
c=ball_collide(100,200,20,200,100,10)
print(c)
output:-
b. Find mean, median, mode for the given set of numbers in a list.
Program:-
numbers=[10,15,16,18,20,25,56,58]
def mean3(numbers):
mean = sum(numbers)/len(numbers)
print("mean=",mean)
def median3(numbers):
median = sorted(numbers) [len(numbers) // 2]
print("median=",median)
def mode3(numbers):
mode = max(numbers,key = numbers.count)
print("mode=",mode)
mean3(numbers)
median3(numbers)
mode3(numbers)
output:-
Week-5( Functions) - Continued
a) Write a function nearly_equal to test whether two strings are nearly equal. Two
strings a and b are nearly equal when a can be generated by a single mutation on b.
program:- (same strings)
from difflib import SequenceMatcher
def Nearly_Equal(a,b):
return SequenceMatcher(None,a,b).ratio();
a="khit"
b="khit"
c=Nearly_Equal(a,b)
if(c*100>80):
print("Both Strings are similar")
print("a is mutation on b")
else:
print("Both Strings are Different")
output:-
b) Write a function dups to find all duplicates in the list.
Program:-
def FindDuplicates(list):
for i in list:
count = list.count(i)
if count > 1:
print ('There are duplicates in list')
return True
print ('There are no duplicates in list' )
return False
a = [8, 64, 16, 32, 4, 24]
b = [2,2,3,6,78,65,4,4,5]
print(a)
FindDuplicates(a)
print(b)
FindDuplicates(b)
Output:-
c) Write a function unique to find all the unique elements of a list.
Program:-
def FindUnique(list):
unique = set(list)
for i in unique:
count = list.count(i)
if count > 1:
print ('There are no unique elements in list')
return True
print ('There are unique elements in list' )
return False
a = [8, 64, 16, 32, 4, 24]
b = [2,2,3,6,78,65,4,4,5]
print(a)
FindUnique(a)
print(b)
FindUnique(b)
Output ;-
Week--6(Advanced Functions)
1.Design lambda functions to perform the following tasks:
a. Take one parameter; return its square
program:-
x = lambda a : a * a
print(x(5))
output :-
b. Take two parameters; return the square root of the sums of their squares.
Program:-
x = lambda a,b : a * a + b * b
print(x(5,6))
output:-
c. Take any number of parameters; return their average
program :-
from functools import reduce
inp_lst = [1,2,3,4,5,6,7,8,9,10]
lst_len= len(inp_lst)
lst_avg = reduce(lambda x, y: x + y, inp_lst) /lst_len
print("Average value of the list:\n")
print(lst_avg)
output :-
d. Take a string parameter; return a string which contains the unique letters in the input string (in
any order).
Program: -
# String with most unique characters
# using max() + key + lambda
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'vaas']
# printing original list
print("The original list is : " + str(test_list))
# String with most unique characters
# using max() + key + lambda
res = max(test_list, key = lambda sub: len(set(sub)), default = None)
# printing result
print ("The string with most unique characters is : " + str(res))
output:-
Week-9(DS)
a) Write a Python script that performs all basic set operations on two given sets.
Program:-
E = {0, 2, 4, 6, 8};
N = {1, 2, 3, 4, 5};
# set union
print("Union of E and N is",E | N)
# set intersection
print("Intersection of E and N is",E & N)
# set difference
print("Difference of E and N is",E - N)
# set symmetric difference
print("Symmetric difference of E and N is",E ^ N)
output:-
c) Find the most frequent words in a text read from a file.
PROGRAM:
count = 0;
word = "";
maxCount = 0;
words = [];
file = open("sample.txt", "r")
for line in file:
string = line.lower().replace(',','').replace('.','').split(" ");
for s in string:
words.append(s);
for i in range(0, len(words)):
count = 1;
for j in range(i+1, len(words)):
if(words[i] == words[j]):
count = count + 1;
if(count > maxCount):
maxCount = count;
word = words[i];
print("Most frequent word: " + word);
file.close();
OUTPUT:
Week-12 (Files)
a) Write a program to print each line of a file in reverse order.
PROGRAM:
input_file=open('sample1.txt','r')
for line in input_file:
l=len(line)
s=' '
while(l>=1):
s=s+line[l-1]
l=l-1
print(s)
input_file.close()
OUTPUT:-
b) Write a program to compute the number of characters, words and lines in a file.
PROGRAM:-
k=open('sample1.txt','r')
char,wc,lc=0,0,0
for line in k:
for k in range(0,len(line)):
char +=1
if(line[k]==' '):
wc+=1
if(line[k]=='\n'):
wc,lc=wc+1,lc+1
print("The no.of chars is %d\n The no.of words is %d\n The no.of lines is %d"%(char,wc,lc))
OUTPUT:-