Pyhton Lab Manual Merged
Pyhton Lab Manual Merged
RECORD NOTEBOOK
ACADEMIC YEAR: 2020 - 2021
I YEAR / II SEM
PEO1 : To ensure graduates will be proficient in utilizing the fundamental knowledge of basic
sciences, mathematics and Information Technology for the applications relevant to various
streams of Engineering and Technology.
PEO2 : To enrich graduates with the core competencies necessary for applying knowledge of
computers and telecommunications equipment to store, retrieve, transmit, manipulate and
analyze data in the context of business enterprise.
PEO3 : To enable graduates to think logically, pursue lifelong learning and will have the capacity to
understand technical issues related to computing systems and to design optimal solutions.
PEO4 : To enable graduates to develop hardware and software systems by understanding the
importance of social, business and environmental needs in the human context.
Name…………………………………………………………………………………………..
Year…………………………………Semester………………Branch……………….
Regulation: …………………………
Register No.
Certified that this is a Bonafide Record work done by the above student in the ………………..
EXAMINERS
DATE:
7 Multiply matrices
8 Programs that take command line arguments (word count)
9 Find the most frequent words in a text read from a file
Page Signature
S.No Date Name of the Experiment Marks
No. with Date
Average Marks:
Signature:
JGE1211-Python Programming Laboratory
Ex. No: 1
Date:
COMPUTE THE GCD OF TWO NUMBERS
AIM:
ALGORITHM:
PROGRAM :
1
JGE1211-Python Programming Laboratory
smaller = num1
else:
smaller = num2
gcd = i
OUTPUT:
RESULT:
Thus, the program to compute the GCD of two numbers has been executed
successfully.
2
JGE1211-Python Programming Laboratory
Ex. No: 2
FIND THE SQUARE ROOT OF A NUMBER (NEWTON’S
Date: METHOD)
AIM:
ALGORITHM:
following,
PROGRAM :
def newtonSqrt(n):
approx = 0.5 * n
approx = better
return approx
3
JGE1211-Python Programming Laboratory
OUTPUT:
RESULT:
4
JGE1211-Python Programming Laboratory
Ex. No: 3
EXPONENTIATION (POWER OF A NUMBER)
Date:
AIM:
ALGORITHM:
Else
PROGRAM:
def power(base,exp):
if exp==1:
return(base)
5
JGE1211-Python Programming Laboratory
if exp!=1:
return(base*power(base,exp-1))
print("Result:",power(base,exp))
OUTPUT:
RESULT:
successfully
6
JGE1211-Python Programming Laboratory
Ex. No:4
FIND THE MAXIMUM OF A LIST OF NUMBERS
Date:
AIM:
ALGORITHM:
1. Initialize a List
PROGRAM :
def MaxList():
list1=[]
7
JGE1211-Python Programming Laboratory
x=int(input("Enter a Value"))
list1.append(x)
print(list1)
Max=list1[0]
j=1
while j<N:
if list1[j]>Max :
Max=list1[j]
j=j+1
MaxList()
OUTPUT:
RESULT:
successfully
8
JGE1211-Python Programming Laboratory
Ex. No:5
GUESS AN INTEGER NUMBER IN A GIVEN RANGE
Date:
AIM :
range
ALGORITHM:
PROGRAM:
import random
num = random.randint(lower_limit,higher_limit)
while True:
guess = input()
9
JGE1211-Python Programming Laboratory
i = int(guess)
if i == num:
print('You won!!!')
break
print('Try Higher')
print('Try Lower')
OUTPUT:
10
JGE1211-Python Programming Laboratory
RESULT:
successfully
11
JGE1211-Python Programming Laboratory
Ex. No: 6
INSERT A CARD IN A LIST OF SORTED CARDS
Date:
AIM:
ALGORITHM:
3. When found split the list using the slice list option
4. Now create a new list by merging the three list that is the two
PROGRAM :
for i in range(len(list)):
if list[i] > n:
index = i
12
JGE1211-Python Programming Laboratory
break
# list = list[:i] creates new list from from start to pre-defined point
# list = list[i:] creates new list from from pre-defined point to end
return list
# Driver function
list = [1, 2, 4]
n=3
print(insert(list, n))
OUTPUT:
RESULT:
13
JGE1211-Python Programming Laboratory
Ex. No: 7
MULTIPLICATION OF TWO MATRICE
Date:
AIM:
ALGORITHM:
PROGRAM:
# Initialize matrix
matrix1= []
matrix2= []
rmatrix= []
14
JGE1211-Python Programming Laboratory
a =[]
a.append(int(input()))
matrix1.append(a)
a =[]
a.append(int(input()))
matrix2.append(a)
a =[]
a.append(int(0))
15
JGE1211-Python Programming Laboratory
rmatrix.append(a)
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
for r in rmatrix:
print(r)
OUTPUT
16
JGE1211-Python Programming Laboratory
RESULT:
successfully
17
JGE1211-Python Programming Laboratory
Ex. No: 8
WORD COUNT USING COMMAND LINE ARGUMENTS
Date:
AIM:
arguments
ALGORITHM:
4. Split the characters in the file into words and count the number of
words.
5. Split the words if new lines is found and count the number of lines.
PROGRAM:
import sys
if len(sys.argv) < 2:
exit(1)
18
JGE1211-Python Programming Laboratory
def count_words(data):
num_words = len(words)
return num_words
def count_lines(data):
lines = data.split("\n")
for l in lines:
if not l:
lines.remove(l)
return len(lines)
filename = sys.argv[1]
f = open(filename, "r")
data = f.read()
f.close()
num_words = count_words(data)
num_lines = count_lines(data)
19
JGE1211-Python Programming Laboratory
OUTPUT
RESULT:
20
JGE1211-Python Programming Laboratory
Ex. No: 9
FIND THE MOST FREQUENT WORDS IN A TEXT
Date: READ FROM A FILE
AIM:
To write a python program to find most frequent words from a given file
ALGORITHM
3. In order to apply regular expression easier, turn all the letters in the
4. write regular expressions that would return all the words with the number
5. Use here is Python's Dictionaries, since we need key-value pairs, where key
is the word, and the value represents the frequency words appeared in the
6. Finally,we get the word and its frequency (number of times it appeared in
PROGRAM:
import re
import string
21
JGE1211-Python Programming Laboratory
frequency = {}
text_string = document_text.read().lower()
count = frequency.get(word,0)
frequency[word] = count + 1
frequency_list = frequency.keys()
print(words, frequency[words])
OUTPUT
RESULT:
using python.
22
JGE1211-Python Programming Laboratory
Ex. No: 10
CREATE A USER DEFINED EXCEPTION
Date:
AIM:
ALGORITHM
class in Python.
class
alphabet is entered
PROGRAM
class CustomException(Exception):
pass
class PrecedingLetterError(CustomException):
23
JGE1211-Python Programming Laboratory
"""Raised when the entered alphabet is smaller than the actual one"""
pass
class SucceedingLetterError(CustomException):
"""Raised when the entered alphabet is larger than the actual one"""
pass
alphabet = 'k'
while True:
try:
raise PrecedingLetterError
raise SucceedingLetterError
break
except PrecedingLetterError:
print('')
except SucceedingLetterError:
print('')
24
JGE1211-Python Programming Laboratory
OUTPUT
# PROGRAM 2
class Error(Exception):
pass
class Dividebyzero(Error):
pass
25
JGE1211-Python Programming Laboratory
try:
if i_num ==0:
raise Dividebyzero
except Dividebyzero:
print()
OUTPUT
RESULT:
26
JGE1211-Python Programming Laboratory
Ex. No:11
DRAW A 2D CIRCLE AND SQUARE
Date:
AIM:
ALGORITHM
3. get the radius value and use the function circle() to draw the circle
5. use the combinations of forward() func and left() func functions to draw a
square
PROGRAM:
import turtle
t = turtle.Turtle()
t.color("red")
t.circle(R)
turtle.color("purple")
27
JGE1211-Python Programming Laboratory
turtle.penup()
turtle.goto(200,0)
turtle.pendown()
turtle.forward(D)
turtle.left(90)
turtle.forward(D)
turtle.left(90)
turtle.forward(D)
turtle.left(90)
turtle.forward(D)
turtle.left(90)
OUTPUT
Result:
28