INDEX
1. Write a list function to convert a string into a list, as in list (-abc) gives [a, b,
c].
2. Write a program to generate Fibonacci series.
3. Write a program to check whether the input number is even or odd.
4. Write a program to compare three numbers and print the largest one.
5. Write a program to print factors of a given number.
6. Write a method to calculate GCD of two numbers.
7. Write a program to create Stack Class and implement all its methods, (Use
Lists).
8. Write a program to create Queue Class and implement all its methods, (Use
Lists)
9. Write a program to implement linear and binary search on lists.
10. Write a program to sort a list using insertion sort and bubble sort and
selection sort.
Practical 01 : Write a list function to convert a string into a list, as in list (-abc)
gives [a, b, c].
Source Code :-
# Python code to convert string to list character-wise
def Convert(string):
list1=[]
list1[:0]=string
return list1
# Driver code
str1=input("Enter String : ")
print(Convert(str1))
Output Of Program:-
Practical NO. 2 :- Write a program to generate Fibonacci series.
Source Code :-
#Python program to generate Fibonacci series until 'n' value
n = int(input("Enter the value of 'n': "))
a=0
b=1
sum = 0
count = 1
print("Fibonacci Series: ", end = " ")
while(count <= n):
print(sum, end = " ")
count += 1
a=b
b = sum
sum = a + b
Output Of Program :-
Program NO. 3:- Write a program to check whether the input number is even
or odd.
Source Code :-
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("Number is Even")
else:
print("Number is Odd")
Output of Program :-
Practical NO. 4:- Write a program to compare three numbers and print the
largest one.
Source Code :-
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
if (num1 > num2) and (num1 > num3):
largest = num1
elif (num2 > num1) and (num2 > num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
Output Of Program :-
Practical NO. 5:- Write a program to print factors of a given number.
Source Code :-
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0:
print(i)
num = int(input("Enter Number : "))
print_factors(num)
Output of Program :-
Practical NO. 6:- Write a method to calculate GCD of two numbers.
Source Code:-
# Python code to demonstrate naive
# method to compute gcd ( recursion )
def hcf(a, b):
if(b == 0):
return a
else:
return hcf(b, a % b)
a = 60
b = 48
# prints 12
print("The gcd of 60 and 48 is : ", end="")
print(hcf(60, 48))
Output of Program :-
Practical NO. 07:- Write a program to create Stack Class and implement all its
methods, (Use Lists).
Source code:-
stack = []
# append() function to push
# element in the stack
stack.append('a')
stack.append('b')
stack.append('c')
print('Initial stack')
print(stack)
# pop() function to pop
# element from stack in
# LIFO order
print('\nElements popped from stack:')
print(stack.pop())
print(stack.pop())
print(stack.pop())
print('\nStack after elements are popped:')
print(stack)
Output of Program :-
Initial stack
['a', 'b', 'c']
Elements popped from stack:
c
b
a
Stack after elements are popped:
[]
Practical NO. 08:- Write a program to create Queue Class and implement all
its methods, (Use Lists)
Source Code:-
class Queue:
def __init__(self):
self.queue = []
# Add an element
def enqueue(self, item):
self.queue.append(item)
# Remove an element
def dequeue(self):
if len(self.queue) < 1:
return None
return self.queue.pop(0)
# Display the queue
def display(self):
print(self.queue)
def size(self):
return len(self.queue)
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.display()
q.dequeue()
print("After removing an element")
q.display()
Output of Program :-
[1, 2, 3, 4, 5]
After removing an element
[2, 3, 4, 5]
Practical NO. 09:- Write a program to implement linear and binary search on
lists.
Source Code:-
def linearsearch(arr, x):
for i in range(len(arr)):
if arr[i] == x:
return i
return -1
arr = ['t','u','t','o','r','i','a','l']
x = 'a'
print("element found at index "+str(linearsearch(arr,x)))
Output of Program :-
element found at index 6
Practical NO. 10 :- Write a program to sort a list using insertion sort and
bubble sort and selection sort.
Source Code:-
#A. Python program for implementation of Bubble Sort
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j + 1] :
arr[j], arr[j + 1] = arr[j + 1], arr[j]
# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print ("Sorted array is (Bubble sort) : ")
for i in range(len(arr)) :
print ("% d" % arr[i],end=" ")
A. Output of Program :-
Sorted array is (Binary sort) :
2
10
12
18
18
39
72
85
#B. Python program for implementation of Bubble Sort
def selectionSort( itemsList ):
n = len( itemsList )
for i in range( n - 1 ):
minValueIndex = i
for j in range( i + 1, n ):
if itemsList[j] < itemsList[minValueIndex] :
minValueIndex = j
if minValueIndex != i :
temp = itemsList[i]
itemsList[i] = itemsList[minValueIndex]
itemsList[minValueIndex] = temp
return itemsList
el = [21,6,9,33,3]
print(“Sorted array is (Selection sort) :”)
print(selectionSort(el))
B. Output of Program :-
Sorted array is (Selection sort) :
[3, 6, 9, 21, 33]