Python Practicals
Python Practicals
1. Write a list function to convert a string into a list, as in list (-abc) gives [a, b,
c].
4. Write a program to compare three numbers and print the largest one.
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)
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 :-
Output Of Program:-
Practical NO. 2 :- Write a program to generate Fibonacci series.
Source Code :-
Output Of Program :-
Program NO. 3:- Write a program to check whether the input number is even
or odd.
Source Code :-
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: "))
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)
print_factors(num)
Output of Program :-
Practical NO. 6:- Write a method to calculate GCD of two numbers.
Source Code:-
# 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 = []
print('Initial stack')
print(stack)
Initial stack
['a', 'b', 'c']
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)
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()
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:-
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 :-
Source Code:-
def bubbleSort(arr):
n = len(arr)
bubbleSort(arr)
A. Output of Program :-
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 :-