0% found this document useful (0 votes)
2 views17 pages

Python Lab Programs

The document outlines various Python programming exercises, including temperature conversion, pattern construction, grade calculation, area calculation for shapes, prime number identification, factorial calculation using recursion, counting odd and even numbers, string reversal, and file copying. Each exercise includes an aim, algorithm, program code, output, and result indicating successful execution. The exercises are designed to demonstrate fundamental programming concepts and techniques.

Uploaded by

Kala Surya
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)
2 views17 pages

Python Lab Programs

The document outlines various Python programming exercises, including temperature conversion, pattern construction, grade calculation, area calculation for shapes, prime number identification, factorial calculation using recursion, counting odd and even numbers, string reversal, and file copying. Each exercise includes an aim, algorithm, program code, output, and result indicating successful execution. The exercises are designed to demonstrate fundamental programming concepts and techniques.

Uploaded by

Kala Surya
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

EX.

NO :1
1. FAHRENHEIT TO CELSIUS & CELSIUS TO FAHRENHEIT
DATE :
AIM:

Program to convert the given temperature from Fahrenheit to Celsius and vice versa depending
upon user’s choice.

ALGORITHM:

1. Print the menu options for temperature conversion.


2. Prompt the user to enter their choice.
3. If the choice is 1:
a. Prompt the user to enter the temperature in Fahrenheit.
b. Perform the Fahrenheit to Celsius conversion using the formula: (5/9) * (f - 32)
c. Print the converted temperature in Celsius.
4. If the choice is 2:
a. Prompt the user to enter the temperature in Celsius.
b. Perform the Celsius to Fahrenheit conversion using the formula: (9/5) * c + 32
c. Print the converted temperature in Fahrenheit.
5. If the choice is neither 1 nor 2, print a message indicating that the choice is invalid.
6. The algorithm guides the program's flow, ensuring that the appropriate temperature
conversion is performed based on the user's choice.

PROGRAM:

#input celcius

celsius = float(input("Enter Celcius: "))

#calculate fahrenheit using the formula


fahrenheit = (9/5)*celsius + 32

print("Fahrenheit ",fahrenheit)

#input fahrenheit
fahrenheit = float(input("Enter Fahrenheit: "))

celsius = (5/9)*(fahrenheit - 32)


print("Celsius ",celsius)

OUTPUT:

KSRCASW/BCA/2026-27/ODD/ 1
Enter Celcius: 37.5
Fahrenheit 99.5

Enter Fahrenheit: 99.5


Celsius 37.5

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 2
[Link] :2
[Link] A SPECIFIC PATTERN
DATE :
AIM:

Write a Python program to construct the pattern, using a nested loop

ALGORITHM:

1. Start by defining the number of rows for the pattern.


2. Use the first loop to iterate from 1 to the number of rows (inclusive).
3. Inside the first loop, use the second loop to print asterisks based on the current row number.
4. Print a newline character (\n) after printing the asterisks for each row in the first loop.
5. After the first loop completes, use a third loop to iterate from rows - 1 down to 1.
6. Inside the third loop, use a fourth loop to print asterisks based on the current row number.
7. Print a newline character after printing the asterisks for each row in the third loop.
8. The pattern construction is complete.

PROGRAM:

n=5;
for i in range(n):
for j in range(i):
print ('* ', end="")
print('')

for i in range(n,0,-1):
for j in range(i):
print('* ', end="")
print('')

OUTPUT:

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 3
[Link] :3
3. CALCULATE TOTAL MARKS, PERCENTAGE AND
GRADE OF A STUDENT
DATE :

AIM:

Program is to calculate the total marks, percentage, and grade of a student based on the marks
obtained in five subjects, which will be input by the user. The program will then assign grades to the
student according to the given criteria.

ALGORITHM:

1. Start the program.


2. Initialize variables to store the marks of five subjects and other necessary variables like
total Marks, percentage, and grade.
3. Prompt the user to input marks for each subject one by one.
4. Calculate the total Marks by adding up the marks obtained in all five subjects.
5. Calculate the percentage by dividing the total Marks by 500 (assuming each subject is out
of 100 marks) and multiplying by 100.
6. Determine the grade based on the percentage using the following criteria:
7. If percentage 80, assign grade as "A".
8. If 70 percentage 80, assign grade as "B".
9. If 60 percentage 70, assign grade as "C".
10. If 40 percentage 60, assign grade as "D".
11. If percentage 40, assign grade as "E".
12. Display the total Marks, percentage, and grade to the user.
13. End the program.

PROGRAM:

print("ENTER THE MARKS OF FIVE SUBJECTS")

sub1=int(input("Enter marks of the first subject: "))


sub2=int(input("Enter marks of the second subject: "))
sub3=int(input("Enter marks of the third subject: "))
sub4=int(input("Enter marks of the fourth subject: "))
sub5=int(input("Enter marks of the fifth subject: "))
avg=(sub1+sub2+sub3+sub4+sub4)/5
if(avg>=80):
print("Grade: A")
elif(avg>=70&avg<80):
print("Grade: B")
elif(avg>=60&avg<70):
print("Grade: C")
elif(avg>=40&avg<60):
print("Grade: D")
else:
print("Grade: E")

KSRCASW/BCA/2026-27/ODD/ 4
OUTPUT:

ENTER THE MARKS OF FIVE SUBJECTS


Enter marks of the first subject: 89
Enter marks of the second subject: 96
Enter marks of the third subject: 98
Enter marks of the fourth subject: 83
Enter marks of the fifth subject: 89
Grade: A

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 5
[Link] :4
[Link] SHAPES
DATE :

AIM:
To write a python program is to calculate the area of different geometric shapes (rectangle,
square, circle, and triangle) based on the input parameters provided by the user.

ALGORITHM:

1. Display a menu to the user with options to choose the shape for which they want to
calculate the area: Rectangle, Square, Circle, or Triangle.
2. Based on the user's input, prompt for the necessary parameters required to calculate the area of
the chosen shape.
3. Perform the area calculation based on the formula for each shape.
4. Display the calculated area to the user.

PROGRAM:

def areacalculator():
_input_ = input("Enter the shape you want to calculate area of: ")
area = 0
pie = 3.14
if _input_ == "square":
side = int(input("Enter the value of side: "))
area = area + (side ** 2)
elif _input_ == "circle":
radius = int(input("Enter the value of radius: "))
area = area + (2 * pie * radius)
elif _input_ == "rectangle":
length = int(input("Enter the value of length:
")) width = int(input("Enter the value of length:
")) area = area + (length * width)
elif _input_ == "triangle":
base = int(input("Enter the value of base: "))
height = int(input("Enter the value of height: "))
area = area +(0.5 * base * height)
else:
print ("Select a valid shape")
print ("%.2f" % area)
areacalculator()

KSRCASW/BCA/2026-27/ODD/ 6
OUTPUT:

Enter the shape you want to calculate area of: square


Enter the value of side: 2
4.00

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 7
[Link] :5
[Link] NUMBERS
DATE :

AIM:
To write a Python script that prints prime numbers less than 20.
ALGORITHM:

1. Define a function is prime(number) that takes a positive integer number as input and checks if it
is prime.
2. In the is prime function, first, handle the special cases where the number is less than 2 (not prime) or
equal to 2 (prime).
3. For numbers greater than 2, check divisibility by integers from 2 up to the number itself (exclusive).
4. If the number is divisible by any integer in the range, it is not prime, and the function returns False.
5. If the number is not divisible by any integer in the range, it is prime, and the function returns True.
6. In the main part of the script, set the limit to 20 and use a list comprehension to find all
prime numbers less than the limit using the is prime function.
7. Print the list of prime numbers.

PROGRAM:

print("Prime numbers between 1 and 20 are:")


ulmt=20;
for num in range(ulmt):
# prime numbers are greater than 1
if num > 1:
for i in
range(2,num): if
(num % i) == 0:
break
else:
print(num)

OUTPUT:

Prime numbers between 1 and 20


are: 2
3
5

KSRCASW/BCA/2026-27/ODD/ 8
7
11
13
17
19

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 9
[Link] :6
[Link] USING RECURSION
DATE :

AIM:
To write a python Program to find factorial of the given number using recursive function
ALGORITHM:

1. Define the function factorial(n) that takes a positive integer 'v' as input and returns its factorial.
2. Inside the factorial() function:
i. Check if 'n' is less than or equal to 1. If true, return 1, as the factorial of 0 and 1 is 1.
ii. If 'num' is greater than 0, calculate the factorial by multiplying 'n' with the factorial of 'num-
1'. This is done by recursively calling the factorial() function with the argument 'v-1'.
iii. The recursive calls will continue until 'num' reaches 1 or 0, and then the function will
start returning the calculated factorial values back up the recursion stack.
3. Ask the user to input a value 'num' and store it in the variable 'num'.
4. Call the factorial(n) function to calculate the factorial of 'num'.
5. Print the result as "The factorial of [n] is [factorial_value]".

PROGRAM:

def recur_factorial(n):
if n == 1:
return n
else:
return n*recur_factorial(n-1)
# take input from the user
num = int(input("Enter a number: "))
# check is the number is negative
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",recur_factorial(num))

OUTPUT
Enter a number: 9

The factorial of 9 is 362880

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 10
[Link] :7
COUNT THE ODD AND EVEN NUMBERS
DATE :

AIM:
To Write a Python program to count the number of even and odd numbers from array of N
numbers.
ALGORITHM:

PROGRAM:

series = tuple(map(int, input("Enter Series of Numbers [Separated by Comma] : ").split(",")))

# series = (1, 2, 3, 4, 5)

even_count, odd_count = 0, 0

for item in series:


if item % 2 == 0:
even_count += 1
else:
odd_count += 1

print("Even count in series is : ", even_count)


print("Odd count in series is : ", odd_count)
OUTPUT:

Enter Series of Numbers [Separated by Comma]: 1,2,3,4

Even count in series is: 2

Odd count in series is: 2

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 11
[Link] :8
[Link] THE STRING
DATE :

AIM:
To Write a Python class to reverse a string word by word.

ALGORITHM:

1. Start the program.


2. Split the input string into individual words.
3. Reverse the list of words.
4. Join the reversed words back together into a new string.

PROGRAM:

my_string = input("Enter a string : ")

reverse_string = ""
for letter in my_string:
reverse_string = letter + reverse_string

print("Reversed String is ...")


print(reverse_string)

OUTPUT:

Enter a string : THIS IS A CLASSROOM

Reversed String is ...

MOORSSALC A SI SIHT

>

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 12
[Link] :9
[Link] COPY
DATE :
AIM:

To Read a file content and copy only the contents at odd lines into a new file
ALGORITHM:

1. Specify Input and Output Files: Define the paths to the input file (from which you want to
read) and the output file (where you want to save the odd lines).
2. Open Input File: Use the open() function to open the input file in read mode ('r'). This creates a
file object that allows reading the content.
3. Read Input File Lines: Use the readlines() method on the file object to read all lines from the
input file into a list. Each element of the list corresponds to a line from the file.
4. Filter Odd Lines: Iterate through the list of lines using a loop. For each line, use the index (line
number) to determine if it's an odd line. Lines are indexed starting from 0, so lines with even
indexes (0, 2, 4, ...) are considered odd lines.
5. Write Odd Lines to Output File: Create or open the output file in write mode ('w'). Use the
writelines() method to write the selected odd lines into the output file.
6. Close Files: Close both the input and output files using the close() method on their respective
file objects. This is important for proper file handling.

PROGRAM:

# opening the file


file1 = open('[Link]', 'r')

# creating another file to store odd lines


file2 = open('[Link]', 'w')

# reading content of the files


# and writing odd lines to another file
lines = [Link]()
type(lines)
for i in range(0, len(lines)):
if(i % 2 != 0):

KSRCASW/BCA/2026-27/ODD/ 13
[Link](lines[i])

# closing the files


[Link]()
[Link]()

# opening the files and printing their content


file1 = open('[Link]', 'r')
file2 = open('[Link]', 'r')

# reading and printing the files content


str1 = [Link]()
str2 = [Link]()

print("file1 content...")
print(str1)

print() # to print new line

print("file2 content...")
print(str2)

# closing the files


[Link]()

file named "[Link]", that contains following content


This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.

copying odd lines to another file named "[Link]".

Input: "[Link]"
This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.

Output: "[Link]"
This is line 2.
This is line 4.

OUTPUT:

file1 content...
This is line 1.
This is line 2.
This is line 3.
This is line 4.

KSRCASW/BCA/2026-27/ODD/ 14
This is line 5.

file2 content...
This is line 2.
This is line 4.

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 15
[Link] :10
[Link] GRAPHICS
DATE :

AIM:
To Create a Turtle graphics window with specific size.
ALGORITHM:

1. Import the turtle module.


2. Create a function called fill_circle that takes a color as input.
3. In the fill_circle function, set the fill color to the specified color.
4. Call the begin_fill() function.
5. Call the circle() function with the radius of the circle as the argument.
6. Call the end_fill() function.
7. In the main function, create a turtle graphics window.
8. Set the turtle speed to 10.
9. Lift the turtle pen.
10. Move the turtle to the origin.
11. Lower the turtle pen.
12. Call the fill_circle function with the color "red" as the argument.
13. Wait for the user to click on the screen to close the window.

PROGRAM:

import turtle
def create_turtle_window(width, height):
screen=[Link]()
[Link]("yellow")
[Link](width, height)
return screen

def fill_circle(color):
[Link](color)
turtle.begin_fill()
[Link](100)
turtle.end_fill()
if name == " main ":
screen=create_turtle_window(800, 500)
[Link](10)
[Link]()
[Link](0, 0)
[Link]()
fill_circle(“red”)
[Link]()

KSRCASW/BCA/2026-27/ODD/ 16
OUTPUT:

RESULT:

Thus the above program has been successfully executed and verified successfully.

KSRCASW/BCA/2026-27/ODD/ 17

You might also like