0% found this document useful (0 votes)
3 views54 pages

Python Practical 2025

Uploaded by

nithyasree9344
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)
3 views54 pages

Python Practical 2025

Uploaded by

nithyasree9344
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/ 54

EX NO:1

a)Flow control

AIM:

To write a python program to find whether a number is odd or even using

flow control.

ALGORITHM:

Step 1:Start the program.

Step 2:Read input number from the user.

Step 3:Divide the number by 2 and check the remainder.

Step 4:If the remainder is 0,the number is even.

Step 5:Display the result.

Step 6: Stop the program.


EX NO:1 ( A) FLOW CONTROL

CHECKING IF A NUMBER IS ODD OR EVEN

num=int(input("enter a number:"))

if num%2==0:

print(num,"is even")

else:

print(num,"is odd")
OUTPUT
EX No:1

B) Functions

AIM:

To write a python program using function to calculate the area of a circle

ALGORITHM:

Step 1:Start the program.

Step 2 Read the radius of the circle from the user.

Step 3:Define a function named calculate the area that takes the radius as a

Parameter.

Step 4:Inside the function calculate the area of the circle using the formula area

=pi* reading where pi is constant value.

Step 5:Return the calculate area from function.

Step 6:Call the calculate area of function with the input radius as an argument.

Step 7:Store the returned area value in a variable.

Step 8:Display the calculate Area.

Step 9:Stop the program.


EX NO:1 (B)FUNCTION

CALCULATE THE AREA OF CIRCLE

import math

def calculate_area(radius):

area=math.pi*radius*radius

return area

radius=float (input("Enter the radius of the circle;"))

area=calculate_area(radius)

print ("The area of the ciecle is:",area)


OUTPUT:
EX NO:1

C) STRING MANIPULATION

AIM:

To write a python program using string manipulation to reverse a given string.

ALGORITHM:

Step 1:Start the program.

Step 2:Read the Input string from the user.

Step 3:Define the function named reversed string that takes the string as a

Parameter.

Step 4:Inside the function are string slicing with a step of 1 to reverse string.

Step 5:Return the reversed string from function.

Step 6:Call the reversed string from the input string as a argument.

Step 7:Store the returned reversed string in a variable.

Step 8:Display the reversed string.

Step 9:Stop the program.


EX NO:1 C) STRING MANIPULATION

REVERSING A STRING

def revers_string(string)

revers_string=string[::-1]

return revers_string

text=input("enter a string:")

revers_text=reverse_string(text)

print("reversed string:",revers_text)
OUTPUT:
RESULT:

Thus the program is executed successfully.


EX NO:2

A)OPERATIONS ON TUPLES

AIM:

To write a python program to find maximum and minimum values in tuple.

ALGORITHM:

Step 1:Start the program.

Step 2:Define a tuple containing a set of numbers

Step 3:Initialize variables maximum and minimum with the first element of the

tuple

Step 4:Iterate through the tuple starting from the second element.

Step 5:If the current element is greater than maximum update maximum.

Step 6:If the current element is smaller than minimum update minimum.

Step 7:After iterating through all elements display the results.

Step 8:Stop the program.


EX NO:2 OPERATIONS ON TUPLES AND LISTS

(A)FINDING MAXIMUM AND MINIMUM ELEMENTS IN A TUPLE

numbers=(10,30,20,5,15)

maximum=minimum=numbers[0]

for num in numbers[1:]:

if num>maximum:

maximum=num

if num<minimum:

minimum=num

print("maximum:", maximum)

print("minimum:", minimum)
OUTPUT:
EX NO:2

B)OPERATIONS ON LIST

AIM:

To write a python program to find the sum of elements in a list.

ALGORITHM:

Step 1:Start the program.

Step 2:Define a list Containing a set of numbers.

Step 3:Initilize a variable total to zero.

Step 4:Iterate through the list and add each element to total.

Step 5:Display the sum total.

Step 6:Stop the program.


EX NO:2 (B) LISTS

FINDING THE SUM OF THE LISTS

numbers = [10, 20, 30, 40]

total = sum(numbers)

print("Sum of the list:", total)


OUTPUT:
RESULT:

Thus the program is executed successfully.


EX NO:3

OPERATIONS ON SETS

AIM:

To write a python program to perform various operations on set.

ALGORITHM:

Step1:Start the program.

Step 2:Define the sets with different elements.

Step 3:Perform various operations on the sets.

a) Union: find the union of the two sets using the union () method.

b) Intersection : find the intersection of the Two sets using the intersection () method.

c) Difference: Find the difference between the two sets using the difference()method.

Step 4:Display the result of operations.

Step 5:Stop the program.


EX NO:3 OPERATIONS ON SETS

set1 = {1, 2, 3, 4, 5}

set2 = {4, 5, 6, 7, 8}

union_set = set1.union(set2)

intersection_set = set1.intersection(set2)

difference_set = set1.difference(set2)

symmetric_difference_set = set1.symmetric_difference(set2)

print("union:", union_set)

print("intersection:", intersection_set)

print("difference:", difference_set)

print("symmetric difference:", symmetric_difference_set)


OUTPUT:
RESULT:

Thus the program is executed successfully.


EX NO:4

OPERATION ON DICTIONARY

AIM:

To write a python programming to perform various operations on dictionary.

ALGORITHM:

Step 1: Start the program.

Step 2: Define a dictionary with key-value pair.

Step 3: Perform various operations on the dictionary.

a) Accessing values : retrieve the value of specific key from the dictionary using the
inside square brackets
b) Adding key-values pair. Add a new key-specific key in the dictionary.

c) Removing key-value pairs : Remove a specific key value pair from

the dictionary.

Step 4: Display the result of the operations.

Step 5: Stop the program..


EX NO:4 OPERATIONS ON DICTIONARY

student_scores = {

"john": 85,

"slice": 92,

"bob": 78,

"emily": 95,

print("Accessing value:")

print("john's score:", student_scores["john"])

print("\nModifying value:")

student_scores["alice"] = 90

print("Updated score for alice:", student_scores["alice"])

print("\nAdding key-value pair:")

student_scores["david"] = 88

print("Added david's score:", student_scores["david"])

print("\nRemoving key-value pair:")

del student_scores["bob"]

print("bob's score removed.")

print("\nIterating through keys and values:")

for name, score in student_scores.items():


print(name, "scored", score)
OUTPUT:
RESULT:

Thus the program is executed successfully.


EX NO:5

SIMPLE OPP – CONSTRUCTORS CREATE A CLASS FROM

REPRESTING CAR

AIM:

To write a python program with simple oop by creating a class car with

Constructors.

ALGORITHM:

Step 1:Start the program.

Step 2:Defined a class named car.

Step 3:Inside the class,define a constructor method _init_() that takes a parameters

for the car’s make, model, year.

Step 4:Initialize instance variables using the constructor parameters.

Step5:Create an instance of the car, class , providing values of the car make,

models, year.

Step6:Access and display the car attributes.

Step7:Stop the program.


EX NO:5 SIMPLE OPP-CONSTRUCTORS CREATE A CLASS FOR REPRESENING A CAR

class Car:

def __init__(self, make, model, year, color):

self.make = make

self.model = model

self.year = year

self.color = color

my_car = Car("Toyota", "Corolla", 2020, "Blue")

print("make:",my_car.make)

print("model:",my_car.model)

print("year:",my_car.year)
OUTPUT:
RESULT:

Thus the program is executed successfully.


EX.NO:06

METHOD OVERLOADING –CREATE CLASSES FOR VECHICLE


AND BUS AND DEMONSTRATE

AIM:
Write a program using method over-loading

ALGORITHM:

Step 1: Start the program.

Step2: Define a class named ‘vehicle’

Step 3:Inside the class vehicle ,define method name ‘accelerate’ that takes single parameter
‘speed’

Step4:Define a class name ‘bus’ which inhert from the class ‘vehicle’

Step5:Inside bus class define an overloaded accelerate on that takes a 2 parameter ‘speed’,
’gear’.

Step6:Create the instances of the class vehicle & bus.

Step7:Call the accelerated method on birth instance with different set of parameter.

Step8:Display the result.

Step9:End the program.


EX NO:6

METHOD OVERLOADING – CREATE CLASSES FOR VEHICLE AND BUS AND


DEMONSTRATE

class vehicle:

def accelerate(self, speed):

print("vehicle is accelerating to",speed, "km/h")

class bus(vehicle):

def accelerate(self, speed, gear):

print("bus is accelerating to",speed, "km/h in", gear, "gear")

car=vehicle()

bus=bus()

car.accelerate(100)

bus.accelerate(80,4)
OUTPUT:
RESULT:

Thus the program is executed successfully.


EX.NO.:07

FILES-READING AND WRITING-PERFORM THE BASIC


OPERATION OF READING AND WRITING WITH STUDENT FILE

AIM:

To write a program to perform the basic reading and writing operation from the student file.

Algorithm:

step1:Start the program.

Step2:Define a function that reads student information “read student()”.

Step3:Definr a function that write information “write student()”.

Step4:Call read function to read and display the existing students information from file.

Step5:Call write fuction to add the information of new students to file and display.

Step6:Again call the read student to read and display the updated student information.

Step7:End of program.
EX NO:7 FILES-READING AND WRITING PERFORM THE BASIC OPERATION OF
READING AND WRITING WITH STUDENT FILE

def read_students():

print("Student Information:")

try:

with open("students.txt", "r") as file:

for line in file:

name, score = line.strip().split(",")

print("Name:", name, "Score:", score)

except FileNotFoundError:

print("No student data found yet.")

def write_students():

name = input("Enter student name: ")

score = input("Enter student score: ")

with open("students.txt", "a") as file:

file.write(name + "," + score + "\n")

print("Student information added successfully.")

write_students()

read_students()
OUTPUT:
RESULT:

Thus the program is executed successfully.


EX.NO:08

REGULAR EXPRESSIONS

AIM:

To write a program to extract email address from a text .

Algorithm:

Step1:Start the program.

Step2:Import re module the supports regular expression.

Step3:Define a regular expression pattern that matches an email address.

Step4:Read a text input from user .

Step5:Use, the re.find al l () function to find all the occurrences of the email address and
return them as a list.

Step6:Display the extracted email address.

Step7:End the program.


EX NO 8: REGULAR EXPRESSIONS

import re

pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'

email = input("Enter an email address: ")

if re.match(pattern, email):

print("Valid email address.")

else:

print("Invalid email address.")


OUTPUT:
RESULT:

Thus the program is executed successfully.


EX.NO:09

MODULE

AIM:

To write a program using module to perform mathematical operations.

Algorithm:

Step1:Start the program.

Step2:Import math module which support mathematical fuctions.

Step3:Read the input from the user.

Step4:Use various operations from the math module such as:

(i)Square root

(ii)logarithmic function &

(iii)Trignometric function.

Step5:Display the result.

Step6:End the program


EX NO:9 MODULE

import math

number = float(input("Enter a number: "))

# Square root (note: function is 'sqrt', not 'sqr')

square_root = math.sqrt(number)

print("Square root:", square_root)

# Trigonometric functions (use 'sin', not 'sine')

sine = math.sin(number)

print("Sine:", sine)

cosine = math.cos(number)

print("Cosine:", cosine)

tangent = math.tan(number)

print("Tangent:", tangent)

# Logarithms

log_base10 = math.log10(number)

print("Logarithm (base 10):", log_base10)

natural_log = math.log(number)

print("Natural logarithm (base e):", natural_log)


OUTPUT:
RESULT:

Thus the program is executed successfully.


EX.NO:10

PACKAGE

AIM:

To perform mathematical function using a package.

Algorithm

Step1:Start the program.

Step2:Create a folder named calculator

Step3:Inside the calculator folder create a file named “calculator. py” Which institute
function such as.

(i)def add

(ii)def sub

(iii)def mul

(iv)def divi

Step4:Create a another file named ‘init.py’ referred to package in the folder.

Strp5:Create another file “package.py” inside the folder calculator and declare the directory
path for the folder calculator.

Step6:Run the package file.

Step7:Print the display.

Step8:End the program.


EX NO 10 : PACKAGE

basic:

def add(a, b):

return a + b

def subtract(a, b):

return a - b

advanced:

def multiply(a, b):

return a * b

def division(a, b):

return a / b

main:

from math_operations.basic import add,sub

from math_operations.advanced import multiply,divide

num1 = float(input("Enter the first number: "))

num2 = float(input("Enter the second number: "))

result_add = add(num1, num2)

result_subtract = subtract(num1, num2)

result_multiply = multiply(num1, num2)

result_divide = division(num1, num2)

print("Addition:", result_add)

print("Subtraction:", result_subtract)

print("Multiplication:", result_multiply)

print("Division:", result_divide)
OUTPUT:
RESULT:

Thus the program is executed successfully.


EX.NO:11

EXCEPTION HANDLING

AIM:

To write a program to perform the division operation on exception handling.

Algorithm:

Step1:Start the program.

Step2:Read two numbers, numerator and denominator from the user.

Step3:Wrap the division operation inside try-except block to handle potential exceptions.

Step4:Perform the division operation inside try block.

Step5:If the division is successfully print the try result.

Step6:If the zero division error occurs catch the exception in the except block.

Step7:Display an error message indicating that division by zero is not allowed.

Step8:End the program.


EX NO:11 EXCEPTION HANDLING

numerator=float(input("enter the numerator:"))

denominator=float(input("enter the denominator:"))

try:

result=numerator/denominator

print("result:",result)

except zerodivisionerror:

print("error division by zero is not allowed")


OUTPUT:
RESULT:

Thus the program is executed successfully

You might also like