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

Python Manual PDF

The document is a lab manual for Python programming for second-year computer engineering students at KBS's College of Engineering & Technology, detailing various practical exercises. It includes a list of practicals such as finding prime numbers, calculating areas of geometric shapes, splitting even and odd numbers, and reversing strings. Each practical contains a title, aim, problem description, solution steps, program explanation, and sample outputs.

Uploaded by

yugwanshkoli22
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)
3 views33 pages

Python Manual PDF

The document is a lab manual for Python programming for second-year computer engineering students at KBS's College of Engineering & Technology, detailing various practical exercises. It includes a list of practicals such as finding prime numbers, calculating areas of geometric shapes, splitting even and odd numbers, and reversing strings. Each practical contains a title, aim, problem description, solution steps, program explanation, and sample outputs.

Uploaded by

yugwanshkoli22
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

KBS’s

College of Engineering & Technology,

North Maharashtra Knowledge City, Jalgaon

Computer Department

LAB MANUAL

Python Programming

For

S.E COMP (SEM IV)


KBS’s

College of Engineering & Technology,

North Maharashtra Knowledge City, Jalgaon

Department of Computer Engineering

List Of Practicals

[Link]. Practical Name


1. Program to Find Prime Numbers in a Given Range.

2. Program to calculate area of triangle, rectangle, circle, square.


3. Program to slit Even and Odd Elements into two lists.

4. Program to Find the Length of a List using Recursion


5. Program to Check How to Reverse a String in Python.
6. Program to map two lists into a dictionary
7. Program to Sort a List of Tuples in Increasing Order by the Last Element in Each
Tuple.
8. Program to create a Class which Performs Basic Calculator Operations.
9. Program to flatten a nested list using recursion.
10. Program to Add a Key-Value pair to the Dictionary.
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll No: Date of Exp:

Class: S.E Sign of Staff Member:

Experiment No:1

Title: Python Program to Find Prime Numbers in a Given Range.

Aim: Write a Program to print all prime numbers within a given range.

Problem Description:

The program takes in the upper limit and prints all prime numbers within the given
range.

Problem Solution:

1. Take in the upper limit for the range and store it in a variable.
2. Let the first for loop range from 2 to the upper limit.
3. Initialize the count variable to 0.
4. Let the second for loop range from 2 to half of the number (excluding 1 and the
number itself).
5. Then find the number of divisors using the if statement and increment the count
variable each time.
6. If the number of divisors is lesser than or equal to 0, the number is prime.
7. Print the final result.
8. Exit.

Program Explanation:

1. User must enter the upper limit of the range and store it in a different variable.
2. The first for loop ranges till the upper limit entered by the user.
3. The count variable is first initialized to 0.
4. The for loop ranges from 2 to the half of the number so 1 and the number itself aren’t counted
as divisors.
5. The if statement then checks for the divisors of the number if the remainder is equal to 0.
6. The count variable counts the number of divisors and if the count is lesser or equal to 0, the
number is a prime number.
7. If the count is greater than 0, the number isn’t prime.
8. The final result is printed.
Conclusions:

C P A Total Sign

4 4 2 10
Program Name: Write a Program to print all prime numbers within a given range.

r=int(input("Enter upper limit: "))


for a in range(2,r+1):
k=0
for i in range(2,a//2+1):
if(a%i==0):
k=k+1
if(k<=0):
print(a)

Output:

Case 1:
Enter upper limit: 15
2
3
5
7
11
13

Case 2:
Enter upper limit: 40
2
3
5
7
11
13
17
19
23
29
31
37
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll No: Date of Exp:

Class:S.E Sign of Staff Member:

Experiment No:2

Title: Program to calculate area of triangle, rectangle, circle, square.

Aim: Write a Program to calculate area of circle, rectangle, triangle, square.

Problem Description:
The program calculate the area of a circle, rectangle, square, triangle depending upon the
user's input

FORMULAUSED:

Area of triangle=1/2*breadth*height

Area of the Square=length *length

Area of the rectangle=width*height

Area of the circle=π*radius*radius

Problem Solution:
1:Start the program.

2:Get the height and breadth of the triangle.

3:Calculate area of triangle using the formula.

Area of triangle=1/2*breadth*height

4:Get the length of the square.

5:Calculate area of the square using the formula.

Area of the Square=length*length


6:Get the width and height of the rectangle.

7:[Link]

of the rectangle=width*height.

8:Get the radius of the circle.

9:[Link]

a of the circle=π*radius*radius

10:Write the area of the triangle, square, rectangle and circle.

11:End of the Program.

Program Explanation:
1. User must enter the value of radius.
2. Then the area of the circle is printed.
3. User must enter the value of length and breadth.
4. Then the area of the is printed.

5. User must enter all three numbers and store it in separate variables.
6. First the value of s is found out which is equal to (a+b+c)/2
7. Then the Heron’s formula is applied to determine the area of the triangle formed by all
three sides.
8. Then the area of the triangle is printed.

Conclusions:

C P A Total Sign

4 4 2 10
Program Name: Write a Program to calculate area of circle, rectangle, triangle, square.

th=int(input("Enter the height of the triangle"))


tb=int(input("Enter the breadth of the triangle"))
tarea=float((0.5)*(tb)*(th))
sl=int(input("Enter the length of the square"))
sarea=float(sl*sl)
rw=int(input("Enter the width of the rectangle"))
rh=int(input("Enter the height of the rectangle"))
rarea=float(rw*rh)
cr=int(input("Enter the radius of the circle"))
carea=float((3.14)*cr*cr)
print(" AREA OF TRIANGLE")
print("The area of Triangle is {0}",tarea)
print(" AREA OF SQUARE")
print("The area of square is {0}",sarea)
print(" AREA OF RECTANGLE")
print("The area of rectangle is {0}",rarea)
print(" AREA OF CIRCLE")
print("The area of Circle is {0}",carea)

Output:
Enter the height of the triangle 4
Enter the breadth of the triangle
7Enter the length of the square
3Enter the width of the rectangle
2Entertheheightoftherectangle5E
nter the radius of the circle 8

AREAOFTRIANGLE
The area of Triangle is {0}
14.0

AREAOFSQUARE
The area of square is {0}
9.0

AREAOFRECTANGLE
The area of rectangle is{0}10.0

AREAOFCIRCLE+

The area of Circle is {0} 200.96


Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll No: Date of Exp:

Class: SE : Sign of Staff Member:

Experiment No:3

Title: Program to slit Even and Odd Elements into two lists.

Aim: Write Program to Split Even and Odd Elements into Two Lists.

Problem Description:

The program takes a list and puts the even and odd elements in it into two separate lists.

Problem Solution:

1. Take in the number of elements and store it in a variable.


2. Take in the elements of the list one by one.
3. Use a for loop to traverse through the elements of the list and an if statement to check if
the element is even or odd.
4. If the element is even, append it to a separate list and if it is odd, append it to a different
one.
5. Display the elements in both the lists.
6. Exit.

Program Explanation
1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a list.
3. Another for loop is used to traverse through the elements of the list.
4. The if statement checks if the element is even or odd and appends them to separate lists.
5. Both the lists are printed.
Conclusions:

C P A Total Sign

4 4 2 10
Program Name: Write Program to Split Even and Odd Elements into Two Lists.

a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
[Link](b)
even=[]
odd=[]
for j in a:
if(j%2==0):
[Link](j)
else:
[Link](j)
print("The even list",even)
print("The odd list",odd)

Output:
Enter number of elements:5
Enter element:67
Enter element:43
Enter element:44
Enter element:22
Enter element:455
The even list [44, 22]
The odd list [67, 43, 455]
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll No: Date of Exp:

Class: SE Sign of Staff Member:

Experiment No:4

Title: Python Program to Find the Length of a List using Recursion

Aim: Write Program to Find the Length of a List using Recursion This is a Python Program to find the

length of a list recursively.

Problem Description:
The program takes a list and finds the length of the list recursively.

Problem Solution:

1. Define a recursive function which takes a list as the argument.


2. Initialize a variable to a list.
3. In the function, put the condition that if it is not the original list, return 0.
4. Otherwise, recursively call the function to find the length of the list.
5. Print the final result of the string.
6. Exit.

Program Explanation:

1. Define a recursive function which takes a list as the argument.


2. Initialize a variable to a list.
3. In the function, put the condition that if it is not the original list, return 0.
4. Otherwise, recursively call the function to find the length of the list.
5. Print the final result of the string.
Conclusions:

C P A Total Sign

4 4 2 10
Program Name: Write Program to Find the Length of a List using Recursion This is a Python

Program to find the length of a list recursively.

def length(lst):
if not lst:
return 0
return 1 + length(lst[1::2]) + length(lst[2::2])
a=[1,2,3]
print("Length of the string is: ")
print(a)

Output:
Case 1:
Length of the string is:
[1, 2, 3]
Khandesh College Education Society’s

College of Engineering& Management, Jalgaon

Department of Computer Engineering

Roll No: Date of Exp:

Class: SE Sign of Staff Member:

Experiment No:5

Title: Program to Check How to Reverse a String in Python.

Aim: Write Program to Check How to Reverse a String in Python.

Problem Description:
Write a Python program to reverse a given string.

Problem Solution:
Method : Reverse a String using Slicing.
In this approach, the program takes a string and use string slicing to reverse the string

Method : Reverse a String using Recursion.


In this approach, we use recursion to reverse a string by repeatedly swapping the first and last
characters.

Method : Reverse a String using Loops.


In this approach, we use for loop to reverse a string by repeatedly swapping the first and last
characters.

Program Explanation:

1. The function reverse_string_loop takes a string as input and initializes an empty


string reversed_string to store the reversed string.
2. It uses a loop to iterate over each character in the input string.
3. Inside the loop, it concatenates each character with the reversed_string, but in reverse order.
4. After the loop, it returns the reversed_string.
5. The program prompts the user to enter a string to be reversed using the input() function and
assigns it to the variable string.
6. Finally, it calls the reverse_string_loop function with the string as an argument and prints
the reversed string using print().
Time Complexity: O(n)
The time complexity of the program is O(n), where n is the length of the input string. This is
because the program iterates through each character in the string once.
Space Complexity: O(n)
The space complexity of the program is O(n), as it creates a new string of the same length as the
input string to store the reversed string.

Conclusions:

C P A Total Sign

4 4 2 10
Program Name: Write Program to Check How to Reverse a String in Python.

1. Method : Reverse a String using Slicing.

a=str(input("Enter a string: "))


print("Reverse of the string is: ")
print(a[::-1])

2. Method : Reverse a String using Recursion


def reverse(string):

if len(string) == 0:

return string

else:

return reverse(string[1:]) + string[0]

a = str(input("Enter the string to be reversed: "))

print(reverse(a))

3. Method : Reverse a String using Loops.

def reverse_string_loop(string):

reversed_string = ''

for char in string:

reversed_string = char + reversed_string

return reversed_string

string = input("Enter the string to be reversed: ")

print(reverse_string_loop(string))
Output:

1. Method : Reverse a String using Slicing.

Enter a string: hello


Reverse of the string is:
olleh

2. Method : Reverse a String using Recursion

Enter the string to be reversed: hello world


dlrow olleh

3. Method : Reverse a String using Loops.

Enter the string to be reversed: hello world


dlrow olleh
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll No Date of Exp:

Class: SE Sign of Staff Member:

Experiment No:6

Title: Program to map two lists into a dictionary.

Aim: Write a Program to map two lists into a dictionary.

Problem Description:
The program takes two lists and maps two lists into a dictionary.

Problem Solution:
1. Declare two empty lists and initialize them to an empty list.
3. Consider a for loop to accept values for the two lists.
4. Take the number of elements in the list and store it in a variable.
5. Accept the values into the list using another for loop and insert into the list.
6. Repeat4 and5 for the values list also.
7. Zip the two lists and use dict() to convert it into a dictionary.
8. Print the dictionary.
9. Exit.

Program Explanation:
1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values to the same number of elements in to the list.
3. The append function obtains each element from the user and adds the same to the end of
the list as many times as the number of elements taken.
4. Thesameof2 and 3isdoneforthesecondvalueslistalso.
5. The two lists a remerged together using the zip()function.
6. The zipped lists are then merged to form a dictionary using dict().
7. The dictionary formed from the two lists is then printed.
Conclusions:

C P A Total Sign

4 4 2 10
Program Name: Write a Program to map two lists in to a dictionary.

keys=[]
values=[]
n=int(input("Enter number of elements for dictionary:"))
print("For keys:")
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
[Link](element)
print("For values:")
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
[Link](element)
d=dict(zip(keys,values))
print("The dictionary is:")
print(d)

Output:
Enter number of elements for dictionary: 3
Forkeys:
Enter element1:4
Enter element2:5
Enter element3:6
For values:
Enter element1:12
Enter element2:34
Enter element3:16
The dictionary is:
{4:12, 5:34, 6:16}
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll No: Date of Exp:

Class: SE Sign of Staff Member:

Experiment No: 7

Title: Python Program to Sort a List of Tuples in Increasing Order by the Last Element in Each
Tuple.

Aim: Write sort a list of tuples in increasing order by the last element in each tuple.
Problem Description:
The program takes a list of tuples and sorts the list of tuples in increasing order by the last
element in each tuple.

Problem Solution:
1. Take a list of tuples from the user.
2. Define a function which returns the last element of each tuple in the list of tuples.
3. Define another function with the previous function as the key and sort the list.
4. Print the sorted list.
5. Exit.

Program Explanation :
1. User must enter a list of tuples.
2. The function last returns the last element of each tuple in the list of tuples.
3. The function sort returns the sorted list with the last function as its key.
4. The sorted list is then printed.
Conclusions:

C P A Total Sign

4 4 2 10
Program Name: Write sort a list of tuples in increasing order by the last element in each
tuple.

def last(n):
return n[-1]

def sort(tuples):
return sorted(tuples, key=last)

a=input("Enter a list of tuples:")


print("Sorted:")
print(sort(a))

Output:

Case 1:
Enter a list of tuples:[(2,5),(1,2),(4,4),(2,3)]
Sorted:
[(1, 2), (2, 3), (4, 4), (2, 5)]

Case 2:
Enter a list of tuples:[(23,45),(25,44),(89,40)]
Sorted:
[(89, 40), (25, 44), (23, 45)]
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll No: Dateof Exp

Class:SE Sign of Staff Member:

Experiment No:8

Title: Python Program to Create a Class which Performs Basic Calculator Operations.

Aim: Write a Program to find the area of a rectangle using classes.

Problem Description:
The program takes the length and breadth from the user and finds the area of the rectangle using
classes.

Problem Solution:

1. Create a class and using a constructor to initialize values of that class.


2. Create methods for adding, substracting, multiplying and dividing two numbers and returning
the respective results.
3. Take the two numbers as inputs and create an object for the class passing the two numbers as
parameters to the class.
4. Using the object, call the respective function depending on the choice taken from the user.
5. Print the final result.
6. Exit

Program Explanation:

1. A class called cal is created and the _init_() method is used to initialize values of that class.
2. Methods for adding, substracting, multiplying, dividing two numbers and returning their
respective results is defined.
3. The menu is printed and the choice is taken from the user.
4. An object for the class is created with the two numbers taken from the user passed as
parameters.
5. Using the object, the respective method is called according to the choice taken from the user.
6. When the choice is 0, the loop is exited.
7. The final result is printed.
Conclusion:

C P A Total Sign

4 4 2 10
Program Name: Program to find the area of a rectangle using classes.
class rectangle():
def _init_(self, breadth, length):
[Link]=breadth
[Link]=length
def area(self):
return [Link]*[Link]
a=int(input("Enter length of rectangle: "))
b=int(input("Enter breadth of rectangle: "))
obj=rectangle(a,b)
print("Area of rectangle:",[Link]())

print()

Output:

Case 1:
Enter length of rectangle: 4
Enter breadth of rectangle: 5
Area of rectangle: 20

Case 2:
Enter length of rectangle: 15
Enter breadth of rectangle: 13
Area of rectangle: 195
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Rol No: Date of Exp:

Class: SE Sign of Staff Member:

Experiment No: 9

Title: Python Program to flatten a nested list using recursion.

Aim: Write Python Program to flatten a nested list using recursion.


Problem Description:
The program takes a nested list and flattens it using recursion.

Problem Solution:
1. Initialize a variable to a nested list.
2. Pass the list as an argument to a recursive function to flatten the list.
3. In the function, if the list is empty, return the list.
4. Otherwise call the function is recursively with the sub lists as the parameters until the entire list
is flattened.
5. Print the flattened list.
6. Exit.

Program Explanation :
1. A variable is initialized to contain a nested list.
2. The list is passed as an argument to a recursive function to flatten the list.
3. In the function, if the list is empty, the list is returned.
4. Otherwise the function is recursively called with the sub lists as the parameters until the entire
list is flattened.
5. The flattened list is printed.
Conclusion:

C P A Total Sign

4 4 2 10
Program Name: Write Python Program to flatten a nested list using recursion.

def flatten(S):
if S == []:
return S
if isinstance(S[0], list):
return flatten(S[0]) + flatten(S[1:])
return S[:1] + flatten(S[1:])
s=[[1,2],[3,4]]
print("Flattened list is: ",flatten(s))

Output:
Case 1:
Flattened list is: [1, 2, 3, 4]
Khandesh College Education Society’s

College of Engineering & Management, Jalgaon

Department of Computer Engineering

Roll o: Date of Exp:

Class: SE Sign of Staff Member:

Experiment No:10

Title: Python Program to Add a Key-Value Pair to the Dictionary.


Aim: Program to add a key-value pair to a dictionary.
Problem Description:
The program takes a key-value pair and adds it to the dictionary.
Problem Solution:
1. Take a key-value pair from the user and store it in separate variables.
2. Declare a dictionary and initialize it to an empty dictionary.
3. Use the update() function to add the key-value pair to the dictionary.
4. Print the final dictionary.
5. Exit.

Program Explanation :
1. User must enter a key-value pair and store it in separate variables.
2. A dictionary is declared and initialized to an empty dictionary.
3. The update() function is used to add the key-value pair to the
dictionary.
4. The final dictionary is printed.

Conclusion:

C P A Total Sign

4 4 2 10
Program Name: Program to add a key-value pair to a dictionary.

key=int(input("Enter the key (int) to be added:"))


value=int(input("Enter the value for the key to be added:"))
d={}
[Link]({key:value})
print("Updated dictionary is:")
print(d)

Output:

Case 1:
Enter the key (int) to be added:12
Enter the value for the key to be added:34
Updated dictionary is:
{12: 34}

Case 2:
Enter the key (int) to be added:34
Enter the value for the key to be added:29
Updated dictionary is:
{34: 29}

You might also like