0% found this document useful (0 votes)
8 views34 pages

Python Record 11

The document is a practical record for the Python Programming Lab for the MSc Information Technology program at CMS College of Science & Commerce. It includes a series of experiments covering various programming concepts such as data structures, conditional branches, loops, functions, exception handling, inheritance, polymorphism, file operations, and modules. Each experiment outlines the aim, algorithm, source code, and results of the programs written in Python.

Uploaded by

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

Python Record 11

The document is a practical record for the Python Programming Lab for the MSc Information Technology program at CMS College of Science & Commerce. It includes a series of experiments covering various programming concepts such as data structures, conditional branches, loops, functions, exception handling, inheritance, polymorphism, file operations, and modules. Each experiment outlines the aim, algorithm, source code, and results of the programs written in Python.

Uploaded by

jeyavasan112233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CMS COLLEGE OF SCIENCE & COMMERCE

(AUTONOMOUS)
(Affiliated to Bharathiar University)
An ISO 9001: 2008 Certified Institution and Reaccredited by NAAC at

the ‘A++’ Grade by NAAC 3.58 out 4(4 th cycle)

Chinnavedampatti - Coimbatore - 641 049.

DEPARTMENT OF INFORMATIO TECHNOLOGY

PRACTICAL RECORD
PYTHON PROGRAMMING LAB
I MSC INFORMATION TECHNOLOGY
2024 – 2026 Batch
OCTOBER 2024
CMS COLLEGE OF SCIENCE & COMMERCE
(AUTONOMOUS)

(Affiliated to Bharathiar University)


An ISO 9001: 2008 Certified Institution and Reaccredited by NAAC at the ‘A’ level with a CGPA

of 3.53 out of 4

Chinnavedampatti - Coimbatore - 641 049.

Register no:

This is to certify that this the Bonafied record of work done by


Mr/Ms. ………………………………

of MSc IT during the year 2024 - 25 Submitted for the Practical


Examination held on at CMS College of Science&
Commerce, Coimbatore-641 049.

.................................. ……………………..
Staff In-charge HOD

………………………… …………………………….

Internal Examiner External Examiner


INDEX

S.NO DATE TITLE OF EXPERIMENT PAGE SIGNATURE


NO

PROGRAMS USING ELEMENTARY DATA


1 ITEMS,LISTS,DICTIONARIES AND TUPLES

PROGRAMS USING CONDITIONAL


2 BRANCHES

PROGRAMS USING LOOPS


3

PROGRAMS USING FUNCTIONS


4

PROGRAMS USING EXCEPTION HANDLING


5

PROGRAMS USING INHERITANCE


6

PROGRAMS USING POLYMORPHISM


7

PROGRAMS TO IMPLEMENT FILE


8 OPERATIONS

PROGRAMS USING MODULES


9

PROGRAMS FOR CREATING DYNAMIC AND


10 INTERACTIVE WEB PAGES USING FORMS
EX NO:1
DATE:

PGM 1-LISTS ,DICTIONARIES AND TUPLES

AIM:
To write a program using elementary data items, lists, dictionaries and tuples in
python.

ALGORITHM:

Step 1: Start the process


Step 2: create a list and print it.
Step 3: create a empty list and print it.
Step 4: create a list from existing tuple and print it.
Step 5: Create a tuple with defined items and print it.
Step 6: Create an empty tuple and print it.
Step 7: Create a tuple without parenthesis, single item , tuple from existing tuple and
print it.
Step 8: Create a set of unique items, empty set and print it.
Step 9: Create a dictionary and print it.
Step 10: Stop the process.

SOURCE CODE:

# Create a list of items


print(' ----------------- ')
print('Details of lists: ')
print(' ----------------- ')
list_1 = [1, 'josh', 'hope', 'demon', 24]
print('1. create a list of items: ')
print('============================')
print(list_1)

# Create Empty List


print('2. create empty list: ')
print('=========================')
list_2 = list()
print(list_2)
# create a list from existing tuple
tuple1 = (11, 'raja', 'rahul', 'ram')
list_3 = list({tuple1})
print('3.create a list from existing tuple : ')
print('=====================================')
print(list_3)
print ('\n')
print(' ------------------ ')
print("details of tuple : ")
print(' ------------------ ')

# Create a tuple with defined items


tuple_1 = (20, 'stefan', 'care', 'bonnie', 17)
print('\n4.Create a tuple with defined items:')
print('=====================================')
print(tuple_1)

# Create an empty tuple


tuple_2 = ()
print('\n 5.Create an empty tuple:')
print('==========================')
print(list_3)
print(tuple_2)

# Create a tuple without parathesis (they are optional)


tuple_3 = 1, 'finn', 'davina', 'Matt', 'freya'
print('\n6.Create a tuple without parathesis: ')
print('=====================================')
print(list_3)
print(tuple_3)

# Single item tuple


tuple_4 = 'klaus'
print('\n7.Single item tuple:')
print('====================')
print(list_3)
print(tuple_4)
# Tuple from existing list
tuple_5 = tuple([list])
print('\n 8.Tuple from existing list: ')
print('==============================')
print(list_3)
print(tuple_5)
print ('\n')

# Create a set of unique items


print(' ---------------- ')
print("Details of SET : ")
print(' ---------------- ')
set_1 = {1, 'mon', 'thus', 'wed', 24}
print('\n 9.Create a set of unique items: ')
print('=====================================')
print(list_3)
print(set_1)

# Creates and empty set


set_2 = {}
print('\n 10.Creates and empty set : ')
print('=====================================')
print(list_3)
print(set_2)

# Create a set from existing list


set_3 = {list}
print('\n 11.Create a set from existing list: ')
print('=====================================')
print(list_3)
print(set_3)
print ('\n')
# Create a dictionary with defined values
print(' ----------------------- ')
print("Details of DICTIONARY : ")
print(' ----------------------- ')
dict_1 = {'Ester': 24, 'Akash': 23, 'Tyler': 19, 'Daniel': 24}
print('\n 12.Create a dictionary with defined values:- ')
print('================================================')
print(list_3)
print(dict_1)

# Creates an empty dictionary


dict_2 = {}
print('\n 13.Creates an empty dictionary:-')
print('=====================================')
print(list_3)
print(dict_2)
# Creates a dictionary from provided variables
dict_3 = dict({'Ester': 24, 'Akash': 23, 'Tyler': 25, 'Daniel': 27})
print('\n14.Creates a dictionary from provided variables:- ')
print('===============================================')
print(list_3)
print(dict_3)
OUTPUT:
RESULT:

The above program is successfully verified and the required output is obtained.
EX NO:2
DATE:

PGM2-CONDITIONAL BRANCHES

AIM:
To write a program using conditional branches in python.

ALGORITHM:

Step 1: Start the process


Step 2: Read the value from the user.
Step 3: Using else if statement check the given marks is above 75 if true ,print
distinction.
Step 4: Else, check marks is between 74 to 60, if true print grade A .
Step 5: Else, check marks is between 59 to 50 if true print grade B.
Step 6: Else, Check marks is between 49 to 40 if true print grade C.
Step 7: Else, print the output as Fail.
Step 8: Stop the process.

SOURCE CODE:

marks = int(input("Enter the marks: "))

if marks > 75 and marks <= 100:


print("Congrats! You scored distinction with A+.")
elif marks > 59 and marks <= 75:
print("You scored grade A.")
elif marks > 49 and marks <= 59:
print("You scored grade B.")
elif marks > 40 and marks <= 49:
print("You scored grade C.")
else:
print("Sorry, you have failed.")
OUTPUT:

RESULT:

The above program is successfully verified and the required output is obtained.
EX NO:3
DATE:

PGM 3: LOOPS

AIM:
To write a program using loops in python.

ALGORITHM:

Step 1: Start the process


Step 2: Read the value from the user to print a pyramid using for loop using range().
Step 3: Read the starting range and ending range value from the user to print the even
number for the given range using for loop using range().
Step 4: Read the value using for loop using range to print the multiplication table for
the given number.
Step 5: Stop the process.

SOURCE CODE:

Program to print pyramid:

print("1)")

rows = int(input("Enter number of rows: "))


for i in range(rows):
for j in range(i + 1):
print("*", end=" ")
print()

Program to print even numbers between range :

print("2)")

start = int(input("Enter the start of range: "))


end = int(input("Enter the end of range: "))

print(f"Even numbers between {start} and {end} are:")


for num in range(start, end + 1):
if num % 2 == 0:
print(num)
Program to print table of a given number:

print("3)")

num = int(input("Enter the number: "))


print("Multiplication table of", num)
print("======================")

for i in range(1, 11):


print(num, "x", i, "=", num * i)

OUTPUT:

1)

2)
3)

RESULT:
The above program is successfully verified and the required output is obtained.
EX NO:4
DATE:

PGM 4-FUNCTIONS

AIM:
To write a program using functions in python.

ALGORITHM:

Step 1: Start the process


Step 2: Create five function for performing arithmetic operators.
Step 3: Read the two values from the user.
Step 4: Print the result for addition, subtraction, multiplication, division and mod by calling
the defined function.
Step 5: Stop the process.

SOURCE CODE:

# Define arithmetic operations using functions


def add(a, b):
return a + b

def subtract(a, b):


return a - b

def multiply(a, b):


return a * b

def divide(a, b):


if b == 0:
return "Division by zero error!"
return a / b

def mod(a, b):


return a % b

# Taking values from the user


a = int(input("Enter a: "))
b = int(input("Enter b: "))

# Printing the results of different operations


print("Sum =", add(a, b))
print("Subtraction =", subtract(a, b))
print("Product =", multiply(a, b))
print("Division =", divide(a, b))
print("Modulus =", mod(a, b))
OUTPUT:

RESULT:
The above program is successfully verified and the required output is obtained.
EX NO:5
DATE:

PGM 5:EXCEPTIONAL HANDLING

AIM:
To write a program using exceptional handling in python.

ALGORITHM:

Step 1: Start the process


Step 2: Read the two numbers from the user
Step 3: Perform division, Using try catch block exception will be blocked for division by
zero error.
Step 4: Print the output of the division if proper division is done.
Step 5: Stop the process.

SOURCE CODE:

try:
print('try block')
x = int(input('Enter a number: '))
y = int(input('Enter another number: '))
z=x/y
except ZeroDivisionError: # Correcting the exception name
print("except ZeroDivisionError block")
print("Division by 0 is not accepted")
else: # This block executes only if no exception occurs
print("else block")
print("Division value =", z)
finally: # This block executes no matter what
print("finally block")
x=0
y=0
print("Out of try, except, else, and finally blocks.”)
OUTPUT:

RESULT:
The above program is successfully verified and the required output is obtained.
EX NO:6
DATE:

PGM 6-INHERITANCE

AIM:

To write a program using inheritance in python.

ALGORITHM:

Step 1: Start the process


Step 2: Create a Base class Marks using function init () to get the value from the user
and a
function to display the result.
Step 3: Create a derived class Result from Marks class and in init () to calculate the
total
marks and percentage for the marks read from the base class and it print the result
Step 4: Print the marks and result.
Step 5: Stop the process.

SOURCE CODE:

class Marks: # Base class


def init (self):
self.m1 = int(input("Enter the marks in Physics: "))
self.m2 = int(input("Enter the marks in Chemistry: "))
self.m3 = int(input("Enter the marks in Maths: "))

def displayMarks(self):
print("Marks in Physics:", self.m1)
print("Marks in Chemistry:", self.m2)
print("Marks in Maths:", self.m3)

class Result(Marks): # Derived class


def init (self):
super(). init () # Call the constructor of the base class
self.total = self.m1 + self.m2 + self.m3
self.percentage = self.total / 3

def displayResult(self):
print("Total Marks:", self.total)
print("Percentage:", self.percentage)
# Creating an object of the derived class
r = Result()
# Accessing the function of base class using the object of derived class
r.displayMarks()
r.displayResult()

OUTPUT:

RESULT:

The above program is successfully verified and the required output is obtained.
EX NO:7
DATE:

PGM 7-POLYMORPHISM

AIM:
To write a program using polymorphism in python.

ALGORITHM:

Step 1: Start the process


Step 2: Create one Base class and two derived class from it.
Step 3: define a common name function in all the class with different definition.
Step 4: Print the output by creating objects of each class and call the common name function
of each class to show the concept of polymorphism.
Step 5: Stop the process.

SOURCE CODE:

class Bird: # Class names should start with a capital letter


def intro(self):
print("There are many types of birds:")

def flight(self): # Proper indentation


print("Most of the birds can fly but some cannot.")

class Sparrow(Bird): # Class names start with a capital letter


def flight(self):
print("Sparrows can fly.")

class Ostrich(Bird): # Class names must match in capitalization


def flight(self):
print("Ostriches cannot fly.")

# Object Creation
obj_bird = Bird()
obj_spr = Sparrow()
obj_ost = Ostrich()

# Method Calls
obj_bird.intro()
obj_bird.flight()
obj_spr.intro()
obj_spr.flight()

obj_ost.intro()
obj_ost.flight()

OUTPUT:

RESULT:

The above program is successfully verified and the required output is obtained.
EX NO:8
DATE:

PGM 8 -FILE OPERATIONS

AIM:

To write a program using file operation functions in python.

ALGORITHM:

Step 1: Start the process


Step 2: Open the file in write mode.
Step 3: Write text data into file.
Step 4: Read the entire data as one string.
Step 5: Print the text read from the file.
Step 6: Stop the process.

SOURCE CODE:

# file opening example in Python


fo = open("sample.txt", "wb")
print ("File Name: ", fo.name)
print ("Mode of Opening: ", fo.mode)
print ("Is Closed: ", fo.closed)

# Write text data to a file


with open('sample.txt' , 'wt') as f:
print(" ")
f.write ('hi there, this is a first line of file.\n')

# read the entire file as one string


with open('sample.txt') as f:
data = f.read()

# Iterate over the lines of the File


with open('sample.txt') as f:
for line in f :
print(line,)
OUTPUT:

RESULT:

The above program is successfully verified and the required output is obtained.
EX NO:9
DATE:

PGM 9-MODULES

AIM:
To write a program using modules in python.

ALGORITHM:
Step 1: Start the process
Step 2: import math module
Step 3: Print the output for the specified values using function
sqrt(),pi,degrees(),radians,sin(),cos(),tan() and factorial()
Step 4:Stop the process.

SOURCE CODE:

import math
# using square root(sqrt) function contained in math module
print("square root function")
print("====================")
print(math.sqrt(25))
print("\n")

# using pi function contained in math module


print("pi function")
print("====================")
print(math.pi)
print("\n")

# 2 radians = 114.59 degrees


print("degrees function")
print("====================")
print(math.degrees(2))
print("\n")

# 60 degrees = 1.04 radians


print ("radians function")
print("====================")
print(math.radians(60))
print("\n")

# Sine of 2 radians
print("sin function")
print("====================")
print(math.sin(2))
print("\n")

# Cosine of 0.5 radians


print("cos function")
print("====================")
print("\n")
# Tangent of 0.23 radians
print("tan function")
print("====================")
print(math.tan(0.23))
print("\n")

# 1 * 2 * 3 * 4 = 24
print ("factorial function")
print("====================")
print(math.factorial(4))

print(math.cos(0.5))
OUTPUT:
RESULT:

The above program is successfully verified and the required output is obtained.
EX NO:10
DATE:

PGM 10-DYNAMIC AND INTERACTIVE WEB PAGE

AIM:
To write a program to create dynamic and interactive web pages using forms.

ALGORITHM:
Step 1: Start the process
Step 2: Read a name from the user.
Step 3: Pass the read string to the htmlcontent format and write to the html file opened
in write mode.
Step 4: Output is printed in the web browser with value read from the user.
Step 4: Stop the process.

SOURCE CODE:

content='''
<html> <head> <title> Webpage Template</title></head>
<body>
Hello, {strname} !
</body>
</html> '''

strname=input('Enter your name :')


htmlcontent=content.format(**locals())

of=open('t1.html','w')
of.write(htmlcontent)
of.close()

import webbrowser,os.path
webbrowser.open('file:///'+os.path.abspath('t1.html'))
OUTPUT:

RESULT:
The above program is successfully verified and the required output is obtained.

You might also like