Python Lab Manual 24-25 - Final
Python Lab Manual 24-25 - Final
LABORATORY MANUAL
Prepared By
Mr. Kumar. S,
Lecturer
GPC/IQAC/FORMAT/22 1 | Page
GPC/CIVIL/Python Programming Lab/P2418103
INDEX
GPC/IQAC/FORMAT/22 2 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Prepared By Verified By
Faculty In-charge HOD/Mech
GPC/IQAC/FORMAT/22 3 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Empowering the young minds with holistic education and futuristic skills to be a
• To train students and provide them with leading resources to address problems faced
• To encourage doers to embrace learning and achieve their personal best in building
Empowering the students in technical education and Excel those in the field of Civil
GPC/IQAC/FORMAT/22 4 | Page
GPC/CIVIL/Python Programming Lab/P2418103
GPC/IQAC/FORMAT/22 5 | Page
GPC/CIVIL/Python Programming Lab/P2418103
PO 7: Life-long learning:
Ability to analyze individual needs and engage in updating in the context of
technological changes.
PSO 1: The graduates will proficiency in mathematics, basic science and engineering
PSO 2: The graduates will plan, analyse, design and prepare cost estimates for Civil
Engineering structures.
PSO 3: The graduates will able to apply technical and management skills for the
execution of work
GPC/IQAC/FORMAT/22 6 | Page
GPC/CIVIL/Python Programming Lab/P2418103
TEACHING
Periods per Week 04
SCHEME
Hours of Exam. 03
Internal(A) 20
Practical (ESE)
External(B) 30
EXAMINATION-
SCHEME Total Marks (A+B) 50
Credits 06
GPC/IQAC/FORMAT/22 7 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Course Outcomes
Upon completion of this course, the students can able to acquire the following
Practical Skills:
Course Outcomes:
CO – PO / PSO MAPPING
POs /
COs PO1 PO2 PO3 PO4 PO5 PO6 PO7 PSO1 PSO2 PSO3
PSOs
CO103.1 1 - 1 - - - - - - -
CO103.2 1 - 2 1 - - - - - -
CO103.3 1 - 2 1 - - - - - -
CO103.4 1 - 2 1 - - - - - -
CO103.5 1 - 2 1 - - - - - -
CO103.6 1 - 2 1 - - - - - -
GPC/IQAC/FORMAT/22 8 | Page
GPC/CIVIL/Python Programming Lab/P2418103
4. Ex:4 Write and execute Python program to Check whether the CO2 Ap
given year is a Leap Year
6. EX:6 Write and execute Python program to Python program for CO2 Ap
removing i-th character from a string
7. EX:7 Write and execute Python program to find largest number CO3 Ap
in a given list without using max().
8. EX:8 Write and execute Python program to find the index of an CO3 Ap
item of a tuple
9. EX:9 Write and execute Python program to to reverse a tuple. CO3 Ap
GPC/IQAC/FORMAT/22 9 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 1
Date:
Aim:
To write and execute a Python program that calculates the area of a triangle
when the lengths of its three sides, aaa, bbb, and ccc, are given.
Heron's Formula:
1. Semi-Perimeter (s):
2. Area (A):
Where:
• a,b,a, b,a,b, and ccc are the lengths of the sides of the triangle.
• sss is the semi-perimeter.
• \sqrt{} denotes the square root function.
Program:
# Python Program to find the area of triangle
a=5
b=6
c=7
GPC/IQAC/FORMAT/22 10 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Output
Result:
GPC/IQAC/FORMAT/22 11 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 2
Date:
Aim:
To write and execute a Python program that swaps the values of two
variables.
Program:
# Define two variables with initial values
x = input("Enter the value of x: ")
y = input("Enter the value of y: ")
Output:
Result:
The program successfully swaps the values of two variables and displays
the values before and after the swap.
GPC/IQAC/FORMAT/22 12 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 3
Date:
Aim:
To write and execute a Python program that checks if a given number is
positive,
negative, or zero.
Program:
num = float(input("Enter a number: "))
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
GPC/IQAC/FORMAT/22 13 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Output:
Result:
The program successfully checks and displays whether the entered number
is positive, negative, or zero based on the user input.
GPC/IQAC/FORMAT/22 14 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 4
Date:
Write and execute Python program to Check whether the given year
is a Leap Year.
Aim:
To write a program using a for loop that loops over a sequence
Program:
year = 2000
# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
print("{0} is not a leap year".format(year))
GPC/IQAC/FORMAT/22 15 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Output:
Result:
The program using a for loop that loops over a sequence is executed
successfully.
GPC/IQAC/FORMAT/22 16 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 5
Date:
Aim
To reverse the order of words in a given string.
Program:
def rev_words(string):
words = string.split(' ')
rev = ' '.join(reversed(words))
return rev
s= "Python is good"
print ("reverse: ",rev_words(s))
Output:
Result:
The program reverses the order of words in the input string. The output is
"Python from World Hello".
GPC/IQAC/FORMAT/22 17 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 6
Date:
Aim:
To write and execute a Python program to remove the i-th character from
a given string.
Program:
s = "PythonProgramming"
Output:
Result:
To remove the i-th character from a string in one line: result = string[:i]
+ string[i+1:] if 0 <= i < len(string) else "Invalid position" You can replace
`string` and `i` with the appropriate values or variables.
GPC/IQAC/FORMAT/22 18 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 7
Date:
Aim:
To find the largest number in a given list without using the built-in max()
function.
Program:
def find_largest_number(numbers):
if not numbers:
raise ValueError("The list is empty")
largest = numbers[0]
for num in numbers[1:]:
if num > largest:
largest = num
return largest
# Example usage
if __name__ == "__main__":
try:
input_list = list(map(int, input("Enter numbers separated by spaces:
").split()))
result = find_largest_number(input_list)
print(f"The largest number in the list is: {result}")
except ValueError as e:
print(f"Error: {e}")
GPC/IQAC/FORMAT/22 19 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Output:
Result:
The program successfully finds and prints the largest number in the list,
which is 98 in this case.
GPC/IQAC/FORMAT/22 20 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 8
Date:
Write and execute Python program to Find the index of an item in a tuple
Aim:
To find the index of a specific item in a tuple.
Program:
def find_index_in_tuple(tup, item):
try:
# Find and return the index of the item
return tup.index(item)
except ValueError:
# If the item is not in the tuple
return -1
# Example usage
if __name__ == "__main__":
input_tuple = tuple(input("Enter the elements of the tuple separated by
spaces: ").split())
search_item = input("Enter the item to find: ")
index = find_index_in_tuple(input_tuple, search_item)
if index != -1:
print(f"The index of '{search_item}' in the tuple is: {index}")
else:
print(f"'{search_item}' is not found in the tuple.")
Output:
Result:
The program successfully finds and prints the index of the specified item (`30`)
in the tuple, which is `2`.
GPC/IQAC/FORMAT/22 21 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 9
Date:
Aim:
To reverse the order of items in a tuple.
Program:
def reverse_tuple(tup):
return tup[::-1]
# Example usage
if __name__ == "__main__":
input_tuple = tuple(input("Enter the elements of the tuple
separated by spaces: ").split())
reversed_tuple = reverse_tuple(input_tuple)
print(f"The reversed tuple is: {reversed_tuple}")
Output:
Result:
The program successfully reverses and prints the tuple.
GPC/IQAC/FORMAT/22 22 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 10
Date:
Aim:
To create a function that calculates the compound interest based on the
principal amount, rate of interest, and time period.
Program:
# defining the main function
def main_function():
# asking the user for input
p = float(input("Enter the Principal Amount: "))
r = float(input("Enter the Rate of Interest: "))
n = float(input("Enter the Number of times that interest is compounded
per unit t: "))
t = float(input("Enter the Time Period: "))
# calling the compound_interest() function and storing the value in
new variable
ci = compound_interest(p, r, n, t)
print()
# printing the result
print("Compound Interest (C.I.): %2f" %ci)
GPC/IQAC/FORMAT/22 23 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Output:
Result:
The program correctly calculates the compound interest based on the
given principal, rate, and time.
GPC/IQAC/FORMAT/22 24 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 11
Date:
Program:
# Base class (Single Inheritance)
class Animal:
def speak(self):
return "Animal makes a sound"
# Derived class
class Dog(Animal):
def speak(self):
return "Woof!"
self.department = department
def show_details(self):
return f"{self.greet()} {self.get_id()} I manage the {self.department}
department."
Output:
Result:
The program successfully demonstrates single, multiple, and multilevel
inheritance in Python, with the derived classes correctly inheriting and
overriding methods from their base classes.
GPC/IQAC/FORMAT/22 27 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Ex. No: 12
Date:
Aim:
To understand and demonstrate basic file operations in Python, including
creating a file, writing data to it, reading data from it, and appending data to it.
Program:
# 1. Create a new file and write data to it
with open('example_file.txt', 'w') as file:
file.write('This is the initial line of the file.\n')
file.write('Here is another line.\n')
GPC/IQAC/FORMAT/22 28 | Page
GPC/CIVIL/Python Programming Lab/P2418103
Output:
Contents of the file after writing:
This is the initial line of the file.
Here is another line.
Result:
The file operations program creates a file, writes and appends text, and
reads the updated content to confirm the changes.
GPC/IQAC/FORMAT/22 29 | Page