Lab Record Python
Lab Record Python
WEEK 1
Design algorithms and create flowcharts to solve fundamental problems like swapping
variables and summing numbers.
1.1 Swapping Two Variables
Aim
Write an algorithm, draw a flowchart and pseudocode to swap the values of two variables.
Example: Given A = 5 and B = 10, after the swap, A = 10 and B = 5.
Flowchart
Output:
Algorithm
Input: Two values to be swapped
Procedure:
Step 1: Start
1
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
2
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: Number of elements for which sum is to be calculated.
Procedure:
Step 1: Start
Step 2: Read number of elements into variable n
Step 3: Initialize variable i to 1
Step 4: repeat until i > n
3
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
4
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
5
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Algorithm
Input: Decimal number
Procedure:
Step 1: Start
Step 2: Read decimal number into n from user
Step 3: initialize variable i to 1
Step 4: repeat until n value becomes 0
Step 4.1: Store value of n%2 in variable r
Step 4.2: Store r value in list a[i]
Step 4.3: increment i by 1
Step 4.4: assign value of floor(n/2) to n
Step 5: decrement i by 1
Step 6: repeat until i becomes less than 1
Step 6.1: print a[i]
Step 6.2: decrement i by 1
Step 7: End
Output: Binary sequence of the entered number
6
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: An integer
Procedure:
Step 1: Start
Step 2: Read the number entered by user into variable n
Step 3: initialize variable s to 0
Step 4: repeat until n=0
Step 4.1: Store value of n%2 in variable r
Step 4.2: s = s*10 + r
Step 4.3: n = floor(n/10)
Step 5: Display value of s
Step 6: End
Output: Reverse of entered number
7
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
8
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: Two numbers
Procedure:
Step 1: Start
Step 2: Read first number from user into variable a
Step 3: Read second number from user into variable b
Step 4: initiate variable n to 1
Step 5: if a < b:
Step 5.1: assign value of ‘a’ to variable lowest
Step 6: else:
Step 6.1: assign value of b to variable lowest
Step 7: repeat until n > lowest
Step 7.1: if a%n=0 and b%n=0:
Step 7.1.1: gcd=n
Step 7.2: increment n by 1
Step 8: Display value of gcd
Step 9: End
Output: GCD of the two numbers
9
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
10
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
11
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: Range of numbers
Procedure:
Step 1: Start
Step 2: Read first number in range into variable a
Step 3: Read last number in range into variable b
Step 4: initialize variable count to ‘a’
Step 5: Repeat until count > b
Step 5.1: initialize variable i to 1
Step 5.2: initialize variable factors to 0
Step 5.3: repeat until i > count
Step 5.3.1: if count%i=0:
Step 5.3.1.1: increment factors by 1
Step 5.3.2: increment i by 1
Step 5.4: if factors = 2:
Step 5.4.1: Display ‘count’
Step 5.5: increment count by 1
Step 6: End
Output: Prime numbers in a given range
12
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
13
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
14
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: Number of values which has to be sorted
Procedure:
Step 1: Start
Step 2: initialize variable i to 1
Step 3: repeat until i > n:
Step 3.1: store value entered by user into list a[i]
Step 3.2: increment i by 1
Step 4: set i value to 1
Step 5: return until i > n
Step 5.1: initialize variable j to 1
Step 5.2: repeat until j > n – i
Step 5.2.1: if a[j] > a[j+1]:
Step 5.2.1.1: store value of a[j] in variable ‘temp’
Step 5.2.1.2: store value of a[j+1] in a[j]
Step 5.2.1.3: store value of temp in a[j+1]
Step 5.2.2: increment j by 1
Step 5.3: increment i by 1
Step 6: Set i value to 1
15
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: Enter a number
Procedure:
Step 1: Start
Step 2: Read value entered by user into variable n
Step 3: p = sqrt(n)
Step 4: Display p
Step 5: End
Output: Square root of the number
16
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
17
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Algorithm
Input: A number
Procedure:
Step 1: Start
Step 2: initialize variable count to 1
Step 3: initialize variable result to 1
Step 4: repeat until count > n
Step 4.1: result = result * count
Step 4.2: count = count + 1
Step 5: Display value of ‘result’
Step 6: End
Output: Factorial of the number
18
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 2
Generate Fibonacci sequences, solve quadratic equations, and implement array and
matrix operations.
2.1 Fibonacci sequence Generation
Aim
Write an algorithm, flowchart and pseudo code that generates the first ‘n’ terms of the
Fibonacci sequence. Example: Generate the first 10 terms: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.
Flowchart
19
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: Number of terms in Fibonacci series
Procedure:
Step 1: Start
Step 2: initialize variable ‘a’ to 0
Step 3: Display ‘a’ value
Step 4: initialize variable ‘b’ to 1
Step 5: Display ‘b’ value
Step 6: Read number of terms in Fibonacci series into variable ‘n’
Step 7: Initialize variable count to 1
Step 8: repeat until count > n-2
Step 8.1: c = a + b
Step 8.2: Display ‘c’ value
Step 8.3: assign ‘b’ value to ‘a’
Step 8.4: assign ‘c’ value to ‘b’
Step 8.5: increment count by 1
20
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Step 9: End
Output: Fibonacci series with n terms
21
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Algorithm
Input: Base and exponent
Procedure:
Step 1: Start
Step 2: Read base value entered by user into variable ‘x’
Step 3: Read exponent value entered by user into variable ‘n’
Step 4: initiate variable ‘result’ to 1
Step 5: initiate variable ‘i’ to 1
Step 6: repeat until i > n
Step 6.1: result = result * x
Step 6.2: increment i by 1
Step 7: Display ‘result’
Step 8: End
Output: Value after exponentiation
22
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
23
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Algorithm
Input: Number of elements in array
Procedure:
Step 1: Start
Step 2: Read number of elements in array into variable n
Step 3: initiate variable i to 1
Step 4: repeat until i > n
Step 4.1: read value entered by user into list a[i]
Step 4.2: increment i by 1
Step 5: set variable i to 1
Step 6: repeat until i > n
Step 6.1: r[i] = a[n-i+1]
Step 6.2: Display r[i]
Step 6.3: increment i by 1
Step 7: End
Output: The elements in array is reversed
24
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
25
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Algorithm
Input: Number of elements
Procedure:
Step 1: initiate variable i to 1
Step 2: repeat until i > n
Step 2.1: Read value entered by user into list a[i]
Step 2.2: increment i by 1
Step 3: set variable i to 2
Step 4: largest = a[1]
Step 5: repeat until i > n
Step 5.1: If a[i] > largest:
Step 5.1.1: largest = a[i]
Step 5.2: increment i by 1
Step 6: Display ‘largest’
Output: Largest number in array is displayed
26
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
27
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
28
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
29
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
x^2 −4x+4=0.
Flowchart
30
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Algorithm
Input: Coefficients of the equation and constant
Procedure:
Step 1: Start
Step 2: Read the coefficients of the equation, a, b and c from the user.
Step 3: Calculate discriminant = (b * b) – (4 * a * c)
Step 4: If discriminant > 0:
Step 4.1: Calculate root1 = ( -b + sqrt(discriminant)) / (2 * a)
Step 4.2: Calculate root2 = ( -b - sqrt(discriminant)) / (2 * a)
31
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
32
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 3
Demonstrate Python concepts by manipulating data types, handling input/output and
applying operators effectively.
Output:
33
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
34
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
35
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
print("Fail")
Output:
36
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 4
Construct programs to check leap years, manipulate strings, and analyze while loop
execution with error handling.
4.1: Leap Year Program:AIM:
Design algorithm, flowchart and a Python program to check if a given year is a leap year. A
year is a leap year if:
It is divisible by 4.
If divisible by 100, it must also be divisible by 400.
Program:
def is_leap_year(year):
if year % 4 != 0:
return False
elif year % 100 == 0:
return year % 400 == 0
else:
return True
year = 2024
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")
Output:
Program:
#Display numbers from 1 to 10 using while loop
i=1
37
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
while(i<=10):
print(i)
i+=1
else:
print("Invalid input entered")
Output:
Program:
name=input("Enter your name:")
38
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
print(name.title())
Output:
Program:
name=input("Enter your name:")
age=input("Enter your age:")
city=input("Enter your city:")
hobby=input("Enter your hobby:")
occupation=input("Enter your occupation:")
print("Meet the person %s, he/she is %s old , lives in %s,having
hobbies %s,occupation is %s"%(name,age,city,hobby,occupation))
Output:
39
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Program:
while(True):
n=int(input("Enter any number:\nEnter 555 to exit:"))
if(n==555):
break
else:
if(n>0):
print("The given number is positive")
elif(n<0):
print("The given number is negative")
else:
print("The given number is ",n)
continue
Output:
40
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Output:
41
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 5
Develop interactive games and programs, debug code, and classify numbers based on
specified criteria.
5.1 Number Guessing Game:
AIM:
Task: Develop a game where the user guesses a number between 1 and 100. Provide feedback
after each guess.
Extension: Add difficulty levels (easy, medium, hard) based on the range of numbers and the
number of allowed guesses. Include a scoring system and a leader board for an engaging
experience.
Program:
import random
level=int(input("Enter the level: \n1.Easy \n2.Medium \n3.Difficult"))
if(level==1):
n=7
elif(level==2):
n=6
elif(level==3):
n=5
key=random.randint(1,100)
for i in range(1,n+1):
num=int(input("Enter any number"))
if(num==key):
print("Entered number is correct")
break
elif(num>key):
print("key is lesser")
elif(num<key):
print("key is greater")
else:
print("Failed, no more attempts avalable.\nKey is ",key)
Output:
42
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
43
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
else:
sum+=int(x)
else:
print(sum//n)
Output:
Output:
Program:
44
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Program:
#Bubble sort program
values=[30,15,40,50,20,10]
for i in range(0,len(values)):
for j in range(0,len(values)-i-1):
if values[j]>values[j+1]:
temp=values[j]
values[j]=values[j+1]
values[j+1]=temp
print(values)
Output:
45
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 6
Compute LCM, analyze maze paths with recursion, and evaluate the results of summing
digits until a single digit is achieved.
Program:
n1=int(input("Enter first number"))
n2=int(input("Enter second number"))
if(n1>n2):
high=n1
else:
high=n2
while(True):
if(high%n1==0 and high%n2==0 ):
break
else:
high+=1
print("L.C.M is ",high)
Output:
46
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Program:
n = int(input("enter any number"))
s=0
while(True):
r = n%10
s = s+r
n = n//10
if(n==0):
if(s>9):
n=s
s=0
else:
break
print(s)
Output:
47
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Program:
#HCF
n1 = int(input("Enter first number: "))
n2 = int(input("Enter second number: "))
if(n1<n2):
least=n1
else:
least=n2
for i in range(1,least+1):
if(n1%i==0 and n2%i==0):
hcf=i
print("HCF is ",hcf)
Output:
Program:
score=0
while(True):
s=input("Enter any word, enter 'end' to exit")
if(s=="end"):
break
else:
if(s==s[::-1]):
score+=1
else:
score-=1
print("Your score is ",score)
48
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
Program:
#Selection sort algorithm
values=[30,15,25,69,90,19]
for i in range(len(values)):
for j in range(i+1,len(values)):
if(values[i]>values[j]):
temp=values[i]
values[i]=values[j]
values[j]=temp
print(values)
Output:
49
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 7
Organize and retrieve data using Python data structures like lists, sets, and dictionaries
to demonstrate data processing techniques.
Program:
list1=[10,15,30,'hello',5.6]
list1.append(66)
print("List after adding element is :",list1)
list1.remove(30)
print("List after removing element is :",list1)
t1=(10,15,30,'hello',5.6)
print("Accessing second element in tuple:",t1[1])
set1={10,15,30,'hello',5.6}
set2={17,20,5.6,15}
print("union:",set1.union(set2))
print("intersection:",set1.intersection(set2))
print("difference:",set1.difference(set2))
di={"college":"VRSEC","dept":"cse","year":1}
print(di)
Output:
50
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Program:
books={
"book1":{"title":"titleabc","genere":"comedy","author":"authorabc"},
"book2":{"title":"titledef","genere":"drama","author":"authordef"},
"book3":{"title":"titlexyz","genere":"comedy","author":"authorxyz"},
"book4":{"title":"title123","genere":"drama","author":"author123"}
}
Output:
51
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
● Difference (elements unique to one set). Include functionality for dynamically adding
and removing items from the sets.
Program:
set1={10,15,30,'hello',5.6}
set2={17,20,5.6,15}
print("union:",set1.union(set2))
print("intersection:",set1.intersection(set2))
print("difference:",set1.difference(set2))
Output:
Program:
#List comprehension
even_squares = [i**2 for i in range(2,21,2)]
print(even_squares)
#filtering strings
strings=["cse","program","computer","python"]
for s in strings:
if(s[0]=="p"):
print(s)
for s in strings:
if(s[0]=="c"):
print(s)
Output:
52
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Program:
#Tuple v.s Lists
coordinates=(20.4567,52.9754)
shopping=["Pen","book","plank","file"]
'''coordinates[1]=69.6969
Tuple is immutable so it cant be modified'''
shopping[2]="Color papers"
print(shopping)
Output:
Program:
#Dictionaries for Key-Value Storage
contacts={"DJ
Tillu":9609333222,"Subrahmanyam":9848032919,"Sivamani":9848022338}
while(True):
print("Enter your choice")
choice=input("1.Store\n2.Update\n3.Remove\n4.Exit")
if(choice=='1'):
name=input("Enter the name:")
53
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output:
54
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 8
Formulate functions, apply lambda expressions, and construct custom modules to
perform tasks efficiently.
8.1 Functions
Aim
Create a program that defines several functions to perform tasks like:
Calculating the area of a rectangle.
Finding the maximum of a list of numbers.
Printing personalized greetings.
Demonstrate variable scope by modifying variables inside and outside functions.
Program
def area_rectangle(l,b):
print("area of rectangle is",l*b)
def max_list(values):
max = 0
for n in values:
if(n>max):
max=n
print("maximum value is",max)
def greetings(name):
print("hello %s ! greetings"%name)
area_rectangle(12,5)
values = [15,18,30,11,99,55]
max_list(values)
greetings("siddhartha")
Output
Program
#sorting list of tuples
data = [(1, 'b'), (2, 'a'), (3, 'c')]
data.sort(key=lambda item: item[1])
print(data)
55
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output
Output
Program
x = 10 # Global variable
def my_function():
y = 5 # Local variable
print(y) #prints 5
56
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
print(x) #prints 10
# print(y) # Error: y is not accessible outside the function
Output
Program
def fact(n):
f=1
for i in range(2,n+1):
f = f*i
return f
def fib(n):
a = 0
b = 1
fibs = [a,b]
for i in range(1,n-1):
c = a+b
a = b
b =c
fibs.append(c)
return fibs
print("factorial is",fact(5))
print("fibonacci series is",fib(10))
Output
Program
class employees:
57
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
count=0
dept = "marketing"
def __init__(self):
employees.count+=1
ob1=employees()
ob2=employees()
ob3=employees()
print("number of instances are",employees.count)
ob2.dept= "finance" #updating attributes of individual objects
print(ob1.dept)
print(ob2.dept)
print(ob3.dept)
Output
58
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 9
Create custom data structures, model real-world objects using classes, and demonstrate
inheritance and method overriding.
9.1 Constructors and Attributes
Aim
Develop a class with a constructor that initializes attributes based on user input. Include
methods to update and display these attributes. Use this class to model real-world objects, such
as vehicles or products.
Program
class vehicle:
def __init__(self,color,year):
print("displaying from constructor:",color,year)
Output
Program
class vehicle:
color = "white"
class car(vehicle):
wheels = 4
def drive(self):
print("from car class")
print("car color:",self.color)
print("wheels:",self.wheels)
class airplane(vehicle):
59
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
wings = 2
def fly(self):
print("from airplane class")
print("air plane color:",self.color)
print("air plane wings:",self.wings)
c1 = car()
c1.drive()
c2 = airplane()
c2.fly()
Output
Program
class books:
titles = ["Programming in C", "Java complete Reference",
"Wednesday Soul"]
authors = ["Balagurusamy", "Herbert Schildt", "Sorabh Pant"]
genres = ["education", "education", "comedy"]
b1 = books()
while(True):
print("enter your choice")
choice = input("1.Add new Book, 2.Search by Genre, 3.Display,
4.End")
if(choice=='1'):
title = input("enter title")
author = input("enter author")
60
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output
Program
class computer:
61
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
RAM = "16GB"
def __init__(self,model):
print("processor",model)
cpu = computer("i5")
Output
Program
class character:
skill = "Taekwondo"
weapon = "Axe"
armor = "Ceramic Plate"
def __init__(self):
print('character class object created')
def battle1(self):
print('character class method executed')
print("character details are",self.skill, self.weapon,
self.armor)
class Composite:
def __init__(self):
self.obj1 = character()
def battle(self):
print('Composite class battle2 method executed')
self.obj1.battle1()
obj2 = Composite()
obj2.battle()
62
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output
63
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
WEEK 10
Implement recursive functions, apply search algorithms, and optimize sorting techniques
to handle large datasets efficiently
Program
#factorial calculation
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
print(factorial(5))
Output
64
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Implement a binary search algorithm to find a specific product in a sorted list of prices for a
shopping app. Discuss the efficiency of this approach for large datasets.
Program
prices = [220, 320, 1100, 340, 650, 30, 500, 630]
prices.sort()
print(prices)
key = 630
left = 0
right = len(prices)-1
while(left<=right):
mid = (left+right)//2
if(key==prices[mid]):
print("key found at index",mid)
break
elif(key>prices[mid]):
left=mid+1
else:
right = mid-1
else:
print("key not found")
Output
Program
def partition(array, low, high):
pivot = array[high]
i = low -1
for j in range(low, high):
if array[j] <= pivot:
i = i + 1
(array[i], array[j]) = (array[j], array[i])
(array[i + 1], array[high]) = (array[high], array[i + 1])
return i + 1
65
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
Output
Program
def mergeSort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
leftHalf = arr[:mid]
rightHalf = arr[mid:]
sortedLeft = mergeSort(leftHalf)
sortedRight = mergeSort(rightHalf)
return merge(sortedLeft, sortedRight)
66
I Year I Semester 2024-25
Problem Solving with Python Lab Computer Science and Engineering Roll No: 24EU04
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result
Program
Program
text = "This is a sample Text considered in the python program to
Perform case insensitive search"
text = text.lower()
keyword = "PERFORM"
keyword = keyword.lower()
textlist = text.split(" ")
for i in range(len(textlist)):
if(textlist[i]==keyword):
print("keyword is at index",i)
break
else:
print("keyword not found")
Output
67
I Year I Semester 2024-25