0% found this document useful (0 votes)
1 views5 pages

Python 21ME381 Lab Manual

The document contains a series of Python programming tasks including calculating average test scores, finding the smallest and largest numbers in a list, sorting numbers, implementing binary and bubble sort algorithms, checking for palindromes, analyzing sentences, pattern recognition, reading and writing to spreadsheets, file handling, demonstrating classes and inheritance, and working with PDF files. Each task is presented with code snippets and explanations. The document serves as a practical guide for learning various programming concepts in Python.

Uploaded by

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

Python 21ME381 Lab Manual

The document contains a series of Python programming tasks including calculating average test scores, finding the smallest and largest numbers in a list, sorting numbers, implementing binary and bubble sort algorithms, checking for palindromes, analyzing sentences, pattern recognition, reading and writing to spreadsheets, file handling, demonstrating classes and inheritance, and working with PDF files. Each task is presented with code snippets and explanations. The document serves as a practical guide for learning various programming concepts in Python.

Uploaded by

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

1.

Develop a python program to find the better of two test average marks out of three
test’s marks accepted from the user.
m1 = int(input("Enter the marks in the first test: "))
m2 = int(input("Enter the marks in second test: "))
m3 = int(input("Enter the marks in third test: "))
if (m1 > m2):
if (m2 > m3):
total = m1 + m2
else:
total = m1 + m3
elif (m1 > m3):
total = m1 + m2
else:
total = m2 + m3
Avg = total / 2
print("The average of the best two test marks is: ", Avg)

2. Develop a python program to find the smallest and largest number in a list
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
print("Maximum element in the list is :", max(lst), )
print("\nMinimum element in the list is :", min(lst))
print(lst)

3. Develop a python program to arrange the numbers in ascending and descending or-
der
lst = []
num = int(input('How many numbers: '))
for n in range(num):
numbers = int(input('Enter number '))
lst.append(numbers)
lst.sort()
print("/asencending oreder/n")
print(lst)
print("/desending order")
lst.sort(reverse=True)
#newlist=sorted(lst,reverse=True)
print(lst)
4. Develop a binary search program in python
def binary_search(arr, a, low, high):
# Repeat until low and high meet each other
while low <= high:
mid = low + (high - low)//2
if arr[mid] == a:
return mid
elif array[mid] < a:
low = mid + 1
else:
high = mid - 1
return -1
arr = [1, 2, 3, 4, 5, 6, 7]
a=4
#printing the array
print("The given array is", arr)
#printing element to be found
print("Element to be found is ", a)
index = binary_search(arr, a, 0, len(arr)-1)
if index != -1:
print("The Index of the element is " + str(index))
else:
print("Element Not found")

5. Develop a bubble sort program in python


def bubbleSort(array):
# loop to access each array element
for i in range(len(array)):
# loop to compare array elements
for j in range(0, len(array) - i - 1):
# compare two adjacent elements
# change > to < to sort in descending order
if array[j] > array[j + 1]:
# swapping elements if elements
# are not in the intended order
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
data = [-2, 45, 0, 11, -9]
bubbleSort(data)
print('Sorted Array in Ascending Order:')
print(data)
6. Develop a Python program to check whether a given number is palindrome or not
and also count the number of occurrences of each digit in the input number.
n = str(input("Enter Number: "))
k = str(input("Enter the digit: "))
print("Digit count is: ",n.count(k))
#num = input("Enter a number")
if n == n[::-1]:
print("Yes its a palindrome")
else:
print("No, its not a palindrome")
#k = str(input("Enter the digit: "))
#print("Digit count is: ",n.count(k))

7. Write a Python program that accepts a sentence and find the number of words, dig-
its, Uppercase letters and lowercase letters.
s = input("Enter a sentence: ")
w, d, u, l = 0, 0, 0, 0
l_w = s.split()
w = len(l_w)
for c in s:
if c.isdigit():
d=d+1
elif c.isupper():
u=u+1
elif c.islower():
l=l+1
print ("No of Words: ", w)
print ("No of Digits: ", d)
print ("No of Uppercase letters: ", u)
print ("No of Lowercase letters: ", l)

8. Write a Python program for pattern recognition with and without using regular ex-
pressions
import re
phoneNumRegex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
mo = phoneNumRegex.search('My number is 415-555-4243.')
print('Phone number found: ' + mo.group())
Demonstration Experiments ( For CIE )

9. Demonstrate python program to read the data from the spreadsheet and write the
data in to the spreadsheet
# Python program to read an excel file
# import openpyxl module
import openpyxl
# Give the location of the file
path = "gfg.xlsx"
# To open the workbook
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
# Get workbook active sheet object
# from the active attribute
sheet_obj = wb_obj.active
# Cell objects also have a row, column,
# and coordinate attributes that provide
# location information for the cell.
# Note: The first row or
# column integer is 1, not 0.
# Cell object is created by using
# sheet object's cell() method.
cell_obj = sheet_obj.cell(row=3, column=4)
# Print value of cell object
# using the value attribute
print(cell_obj.value)
cell_obj.value=45
print(cell_obj.value)
wb_obj.save("gfg.xlsx")

10. Demonstration of reading, writing and organizing files.


#This program shows how to write data in a text file and read from it.
file = open("myfile.txt","w")
L = ["This is Lagos \n","This is Python \n","This is Fcc\n"]
# i assigned ["This is Lagos \n","This is Python \n","This is Fcc \n"] to
#variable L, you can use any letter or word of your choice.
# Variable are containers in which values can be stored.
# The \n is placed to indicate the end of the line.
file.write("Hello There \n")
file.writelines(L)
file.close()
f = open("myfile.txt", "r")
print(f.readline())
print(f.readline())
print(f.read())
# Use the close() to change file access modes

11. Demonstration of the concepts of classes, methods, objects and inheritance


class Animal:
# attribute and method of the parent class
name = ""
def eat(self):
print("I can eat")
# inherit from Animal
class Dog(Animal):
# new method in subclass
def display(self):
# access name attribute of superclass using self
print("My name is ", self.name)
# create an object of the subclass
labrador = Dog()
# access superclass attribute and method
labrador.name = "Rohu"
labrador.eat()
# call subclass method
labrador.display()

12. Demonstration of working with PDF and word files


# importing required modules
import PyPDF2
# creating a pdf file object
pdfFileObj = open('AEC_Python_RA_syllabus.pdf', 'rb')
# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# printing number of pages in pdf file
print(pdfReader.numPages)
# creating a page object
pageObj = pdfReader.getPage(0)
# extracting text from page
print(pageObj.extractText())
# closing the pdf file object
pdfFileObj.close()

You might also like