A
PRACTICAL Report
Submitted
In
Partial Fulfillment Of The Requirements For the Award Of The
Degree of
Bachelor of Computer
Applications For
Session: 2021-22
“ Programming In Python”
Submitted By: Guided By:
Kritika Asst. Prof. Mrs. Poonam
Shrivastav BCA Yadav HOD:
III Mr. Thakur Devraj Singh
Submitted To:
Shri Shankaracharya Mahavidyalaya, Junwani, Bhilai
Hemchand Yadav University, Durg(C.G)
ACKNOWLEDGEMENT
The practical work has been made possible through the direct and
indirect co- operation of various persons, for whom I wish to
express my appreciation and gratitude, but a complete
acknowledgement would be encyclopedic.
First and foremost I express my profound gratitude to my practical
guide Asst. Prof. Mrs. Poonam Yadav, SSMV, Junwani Bhilai, for
assigning me an interesting and challenging practical work. It is only
because of her invaluable guidance and encouragement; I have dared
to venture this task. If at all I have succeeded, I owe it to her and I am
left with a deep sense of gratitude for her. Under who’s able guidance
I had the privilege to work and who guided me at every stage.
I express my deep sense of gratitude towards Asst. Prof. Mrs.
Poonam Yadav, SSMV, Junwani Bhilai, whose consistent guidance
and moral encouragement helped me to complete the practical
successfully.
Atlast I offer my thanks to all those people and other’s, whose efforts
and contribution had made this possible.
Kritika
Shrivastav
B.C.A-III
CERTIFICATE
This is to certify that Kritika Shrivastav of class B.C.A-III, Shri
Shankaracharya Mahavidyalaya, Junwani, Bhilai has carried out
the practical work entitled in “Programming In Python”. She has
submitted the assignments during academic year 2021-22 towards the
fulfillment of the requirement of University. She has carried out
assignments under my guidance and this is her original work.
(SIGNATURE) (SIGNATURE)
HOD EXTERNAL
Mr. Thakur Devraj Singh
CERTIFICATE OF EVALUATION
This is to certify that the work incorporated in the practical entitled
“Programming In Python” is a record of practical work carried out
by Kritika Shrivastav under my/our guidance and supervision for the
part fulfillment for the award of B.C.A Degree of Hemchand Yadav
University, Durg (C.G.), India.
To the best of my knowledge and belief the practical:-
i) Embodies the work of the candidate him/herself,
ii) Has duly been completed,
iii) Is up to the desired standard both in respect of contents and
language for external.
(Signature Of
HOD) Mr. Thakur
Devraj Singh
DECLARATION
I the undersigned solemnly declare that the report of the practical
work entitled “Programming In Python” is based on my own work
carried out during the course of my study under the supervision of
Asst. Prof. Mrs. Poonam Yadav, SSMV, Junwani Bhilai, I assert
that the statements made and conclusions drawn are an outcome of
my practical work. I further declare that to the best of my knowledge
and belief the practical does not contain any part of any work which
has been submitted for the award of B.C.A or any other
degree/diploma/certificate in Hemchand Yadav University,
Durg(C.G.) or any other University of India or abroad.
(Signature of the
Candidate) Kritika
Shrivastav
<Roll No.>
INDEX
PAGE DATE OF DATE OF
[Link] AIM SIGN
.NO PRACTICA SUBMISSION
L
1. Write a program
that reads an integer
value and prints —
leap year
or —not a leap year.
2. Write a program that
takes a positive
integer a and then
produces n lines of
output shown
as follows
3. Write a program
to create the
following Pattern
For example enter
a size: 5 –
*
**
***
****
*****
4. Write a function that
takes an integer n as
input and calculates
the value of 1 + 1/1!
+ 1/2!
+ 1/n!
5. Write a function that
takes an integer input
and calculates the
factorial of that
number,
6. Write a function that
takes a string input
and checks if it is a
palindrome or not.
7. Write a list function to
convert a string into a
list, as in list (-abc)
gives
[a, b, c].
8. Write a program to
generate Fibonacci
series.
9. Write a program to
check whether the
input
number is even or odd.
10. Write a program to
compare three
numbers and print the
largest
one.
11. Write a program to
print factors of a
given number
12. Write a method to
calculate GCD of two
numbers.
13. Write a program to
create Stack Class and
implement all its
methods, (Use Lists)
14. Write a program to
create Queue Class
and implement all its
methods, (Use Lists)
15. Write a program
to implement
linear and
binary search on lists
16. Write a program to
sort a list using
insertion sort and
bubble sort
and selection sort.
ASSIGNMENT -1
1. Write a program that reads an integer value and prints —leap year or —
not a leap year.
Code:
# User enters the year
year = int(input("Enter Year: "))
# Leap Year Check
if year % 4 == 0 and year % 100 != 0:
print(year, "is a Leap Year")
elif year % 100 == 0:
print(year, "is not a Leap Year")
elif year % 400 ==0:
print(year, "is a Leap Year")
else:
print(year, "is not a Leap Year")
Output:
Enter Year: 2004
2004 is a Leap Year
ASSIGNMENT -2
[Link] a program that takes a positive integer a and then produces n lines
of output shown as follows.
Code:
# Reading a value from user
n = int(input(“Enter No of Lines:”))
# Looping for n times
for i in range(1,n+1):
# Looping from 1 to i inclusive
for j in range(1,i+1):
# Printing value of j
print(j,end="")
# Looping from i-1 to 1 inclusive
for j in range(i - 1, 0, -1):
# Printing value of j
print(j, end="")
# Printing new line
print()
Output:
121
12321
1234321
ASSIGNMENT -3
3. Write a program to create the following Pattern For example enter a
size: 5 –
*
**
***
****
*****
code:
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print("\n")
Output:
*
**
***
****
*****
ASSIGNMENT -4
4. Write a function that takes an integer n as input and calculates
the value of 1 + 1/1! + 1/2! + 1/n!
Code:
n = int(input("Enter the value of n: "))
sum = 0
for i in range(n + 1):
fact = 1
for j in range(1, i) :
fact *= j
term = 1 / fact
sum += term
Output:
Sum = 3.708333333333333
ASSIGNMENT -5
5. Write a function that takes an integer input and calculates the
factorial of that number.
Code:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(“Factorial : ”,factorial(n))
Output:
Input a number to compute the factiorial : 4
Factorial : 24
ASSIGNMENT- 6
6. Write a function that takes a string input and checks if it is a
palindrome or not.
Code:
string=raw_input("Enter string:")
if(string==string[::-1]):
print("The string is a palindrome")
else:
print("The string isn't a palindrome")
Output:
Case 1:
Enter string:malayalam
The string is a palindrome
Case 2:
Enter string:hello
The string isn't a palindrome
ASSIGNMENT- 7
7. Write a list function to convert a string into a list, as in list (-abc) gives [a,
b, c].
Code:
def Convert(string):
li = list([Link](" "))
return li
# Driver code str1 = "abc"
print(Convert(str1))
Output:
['a', 'b', 'c']
ASSIGNMENT -8
8. Write a program to generate Fibonacci series. Code:
n = input('Enter the number of terms')
def fibo(n):
if n <= 1:
return n else:
return(fibo(n-1) + fibo(n-2))
for i in range(int(n)):
print(fibo(i), end=' ')
Output:
Enter the number of terms 6
011235
ASSIGNMENT -9
9. Write a program to check whether the input number is even or
odd.
Code:
# Python program to check if the input number is odd or even.
num = int(input("Enter a number: "))
if (num % 2) == 0:
print("{0} is Even".format(num))
else:
print("{0} is Odd".format(num))
Output:
Enter a number: 43
43 is Odd
Output:
Enter a number: 18
18 is Even
ASSIGNMENT -10
10. Write a program to compare three numbers and print the largest
one.
Code:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(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 1:
Enter first number: 10 Enter second
number: 12
Enter third number: 14 The largest
number is 14.0
Output 2:
Enter first number: -1 Enter
second number: 0 Enter
third number: -3 The largest
number is 0.0
ASSIGNMENT-11
11. Write a program to print factors of a given number.
Code:
# This function computes the factor of the argument passed
def print_factors(x):
print("The factors of",x,"are:")
for i in range(1, x + 1):
if x % i == 0: print(i)
num = 320
print_factors(num)
output:
The factor of 320 are:
1
10
16
20
32
40
64
80
160
120
ASSIGNMENT-12
12. Write a method to calculate GCD of two numbers
Code:
# Python code to demonstrate the working of gcd() # importing "math" for
mathematical operations import math
# prints 12
print("The gcd of 60 and 48 is : ", end="")
print([Link](60, 48))
Output:
The gcd of 60 and 48 is : 12
ASSIGNMENT-13
13. Write a program to create Stack Class and implement all its
methods, (Use Lists).
Code:
# Creating a stack
def create_stack():
stack = []
return stack
# Creating an empty stack
def check_empty(stack):
return len(stack) == 0
# Adding items into the stack
def push(stack, item):
[Link](item)
print("pushed item: " + item)
# Removing an element from the stack def
pop(stack):
if (check_empty(stack)):
return "stack is empty"
return [Link]()
stack = create_stack()
push(stack, str(1))
push(stack, str(2))
push(stack, str(3))
push(stack, str(4))
print("popped item: " + pop(stack))
print("stack after popping an element: " + str(stack))
Output:
pushed item: 1
pushed item: 2
pushed item: 3
pushed item: 4
popped item: 4
stack after popping an element: [‘1’, ‘2’ , ‘3’]
ASSIGNMENT-14
14. Write a program to create Queue Class and implement all its
methods, (Use Lists)Code:
# demonstrate queue implementation # using list
# Initializing a queue queue = []
# Adding elements to the queue
[Link]('a')
[Link]('b')
[Link]('c')
print("Initial queue")
print(queue)
# Removing elements from the queue
print("\nElements dequeued from queue")
print([Link](0))
print([Link](0))
print([Link](0))
print("\nQueue after removing elements")
print(queue)
# Uncommenting print([Link](0)) # will raise
and IndexError
# as the queue is now empty
Output:
Initial queue ['a', 'b', 'c']
Elements dequeued from queue a
b
c
Queue after removing elements []
ASSIGNMENT -15
15. Write a program to implement linear and binary search on lists
Code:
#Program: Linear and Binary Search
defLinearSearch(array, n, k):
for j inrange(0, n):
if (array[j] == k):
return j
return-1
array = [1, 3, 5, 7, 9]
k =7
n =len(array)
result =LinearSearch(array, n, k)
if(result ==-1):
print("Element not found")
else:
print("Element found at index: ", result)
Program: Binary Search in python
def binarySearch(arr, k, low, high):
while low <= high:
mid = low + (high - low)//2
if arr[mid] == k:
return mid
elif arr[mid] < k:
low = mid +1
else:
high = mid -1
return-1
arr= [1, 3, 5, 7, 9]
k =5
result =binarySearch(arr, k, 0, len(arr)-1)
if result !=-1:
print("Element is present at index "+str(result))
else:
print("Not found")
Output:
Element is present at index 3
ASSIGNMENT -16
16. Write a program to sort a list using insertion sort and bubble sort and
selection sort.
Code:
# Python program for implementation of Insertion Sort
# Function to do insertion sort
def insertionSort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]
# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i-1
while j >=0 and key <arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
# Driver code to test above
arr = [12, 11, 13, 10, 22]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])
output:
# Python program for implementation of Bubble Sort
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements for i
in range(n-1):
# range(n) also work but outer loop will #
repeat one time more than needed.
# Last i elements are already in place for j
in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater #
than the next element
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:")
for i in range(len(arr)):
print ("% d" % arr[i]),
output:
# program for implementation of Bubble Sort
def selection_sort(alist):
for i in range(0, len(alist) - 1): smallest = i
for j in range(i + 1, len(alist)):
if alist[j] < alist[smallest]: smallest = j
alist[i], alist[smallest] = alist[smallest], alist[i]
alist = input('Enter the list of alist = [int(x) numbers: ').split()
for x in alist] selection_sort(alist)
print('Sorted list: ', end='') print(alist)
output:
Case 1:
Enter the list of numbers: 3 1452 6
Sorted list: [1, 2, 3, 4, 5, 6]
Case 2:
Enter the list of numbers: 2 10 5 38 17
Sorted list: [1, 2, 5, 7, 10, 38]
Case 3:
Enter the list of numbers: 5 3 2 1 0
Sorted list: [0, 1, 2, 3, 5]