2023
PROGRAMMING LAB IN PYTHON
Assignment No - 1
Q1. Write a program that reads an integer value and prints —leap year
or —not a leap year.
Coding –
#Program to check whether the entered Year is Leap Year or Not
year = int(input("Enter Year:"))
if (year%4 == 0) and (year%100 != 0):
print(f"{year} is a leap year")
else:
if (year%400 == 0):
print(f"{year} is a leap year")
else:
print(f"{year} is not a leap year")
Output –
Ayush Dhankar(BCA-III ) Page 1
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 2
Q2. Write a program which takes a positive integer as an input and
produces n lines of output as shown below -
5
55
555
5555
55555
555555
Coding –
num = int(input("Enter any number : "))
n = int(input("Enter number of lines : "))
for i in range(n):
for j in range(i+1):
print(num, end=" ")
print()
Output –
Ayush Dhankar(BCA-III ) Page 2
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 3
Q3. Write a program to print an star pattern.
Coding –
n = int(input("Enter the size of pattern : "))
for i in range (n):
for j in range(i+1):
print("*",end="")
print()
Output –
Ayush Dhankar(BCA-III ) Page 3
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 4
Q4. Write a program to print the sum of the series 1 + 1/1! +
1/2! +...+ 1/n! .
Coding –
#Function for calculating factorial
def factorial(n):
fact=1
for i in range(1,n+1):
fact *= i
return fact
n = int(input("Enter no. of terms : "))
sum=0
for i in range(n+1):
sum += 1/factorial(i) #Calculating sum of the series
print(f"Sum of the series upto {n}th term is {sum}")
Output –
Ayush Dhankar(BCA-III ) Page 4
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 5
Q5. Write a function that takes an integer input and calculates
the factorial of that number.
Coding –
#Program to calculate the Factorial
def factorial(n):
fact=1
for i in range(1, n+1):
fact *= i
return fact
n = int(input("Enter a number to get its factorial : "))
print(f"Factorial of {n} is {factorial(n)}")
Output –
Ayush Dhankar(BCA-III ) Page 5
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 6
Q6. Write a function that takes a string input and checks if it is a
palindrome or not.
Coding –
def is_Palindrome(str):
l = len(str)
j = l-1
a=0
for i in range (int(l/2)):
if str[i]==str[j]:
a += 1
if a==(int(l/2)):
print(f"'{str}' is a pallindrome string.")
else:
print(f"'{str}' is a not pallindrome string.")
str = input("Enter a word: ")
is_Palindrome(str)
Output–
Ayush Dhankar(BCA-III ) Page 6
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 7
Q7. Write a list function to convert a string into a list, as in list
(abc) gives [a, b, c].
Coding –
def str_to_list(str):
str_list = []
for i in str:
str_list.append(i)
return str_list
str = input("Enter a string : ")
print(f"String as a list: {str_to_list(str)}")
Output –
Ayush Dhankar(BCA-III ) Page 7
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 8
Q8. Write a program to generate Fibonacci series.
Coding –
#Program to Print fibonacci series
n = int(input("Enter number of terms : "))
a = 0; b = 1
print(a, b, end = " ")
for i in range(n-2):
c=a+b
print(c, end = " ")
a=b
b=c
Output–
Ayush Dhankar(BCA-III ) Page 8
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 9
Q9. Write a program to check whether the input number is even or
odd.
Coding –
#Program check whether the number is even or odd
num = int(input("Enter the number : "))
if num%2 == 0:
print(f"{num} is an Even number")
else:
print(f"{num} is an Odd number")
Output–
Ayush Dhankar(BCA-III ) Page 9
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 10
Q10. Write a program to compare three numbers and print the
largest one.
Coding –
num1 = int(input("Enter num1 : "))
num2 = int(input("Enter num2 : "))
num3 = int(input("Enter num3 : "))
if num1>num2 and num1>num3:
print(f"{num1} is the Largest")
elif num2>num1 and num2>num3:
print(f"{num2} is the Largest")
else:
print(f"{num3} is the Largest")
Output–
Ayush Dhankar(BCA-III ) Page 10
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 11
Q11. Write a program to print factors of a given number.
Coding –
#Program to get the factors of a number
num = int(input("Enter Number to get its factors : "))
fact = 2
print(f"Factors of {num} are: ",end="")
while (num >= fact):
if num%fact == 0:
print(fact,end=" ")
num /= fact
else:
fact += 1
print()
Output–
Ayush Dhankar(BCA-III ) Page 11
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 12
Q12. Write a method to calculate GCD of two numbers.
Coding –
# Program to calculate GCD of two numbers
def gcd(a, b):
# Everything divides 0
if (a == 0):
return b
if (b == 0):
return a
# Base case
if (a == b):
return a
# a is greater
if (a > b):
return gcd(a - b, b)
return gcd(a, b - a)
a = int(input("Enter the first number: "))
Ayush Dhankar(BCA-III ) Page 12
2023
PROGRAMMING LAB IN PYTHON
b = int(input("Enter the second number: "))
print(f'GCD({a}, {b}) = {gcd(a,b)}')
Output–
Ayush Dhankar(BCA-III ) Page 13
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 13
Q13. Write a program to create Stack Class and implement all its
methods, (Use Lists).
Coding –
class Stack:
list = []
ptr = -1
def push(self, a):
self.list.append(a)
self.ptr += 1
def pop(self): if (self.ptr == -1):
print("Stack underflow")
return
self.ptr -= 1
return (self.list.pop())
def Peek(self):
print(f"The value at the top of the stack is : {self.list[self.ptr]}")
s1 = Stack()
s1.push(1)
s1.push(2)
s1.push(3)
print(f"Stack elements: {s1.list}")
s1.push(4)
print(f"Stack elements after push: {s1.list}")
Ayush Dhankar(BCA-III ) Page 14
2023
PROGRAMMING LAB IN PYTHON
s1.pop()
print(f"Stack elements after pop: {s1.list}")
s1.Peek()
Output–
Ayush Dhankar(BCA-III ) Page 15
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 14
Q14. Write a program to create Queue Class and implement all its
methods, (Use Lists).
Coding –
class Queue:
list = []
head = -1
tail = -1
def Enqueue(self, a):
self.list.append(a)
self.tail += 1
def Dequeue(self):
if (self.head == self.tail):
print("Underflow")
return
self.head += 1
self.list.pop(self.head)
Ayush Dhankar(BCA-III ) Page 16
2023
PROGRAMMING LAB IN PYTHON
def Front(self):
print(f"The value at the front of the queue is : {self.list[(self.head)]}")
def Rear(self):
print(f"The value at the rear of the queue is : {self.list[(self.tail)-1]}")
s1 = Queue()
s1.Enqueue(1)
s1.Enqueue(2)
s1.Enqueue(3)
print(f"Queue elements: {s1.list}")
s1.Enqueue(4)
print(f"Queue elements after enqueue: {s1.list}")
s1.Dequeue()
print(f"Queue elements after dequeue: {s1.list}")
s1.Front()
s1.Rear()
Output–
Ayush Dhankar(BCA-III ) Page 17
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 15
Q15. Write a program to implement linear and binary search on lists.
Coding –
l = [1,3,4,7,9]
def linear_Search(l,k):
for i in range(len(l)):
if k==l[i]:
print(f"Element '{k}' found at index {i}")
return
print("Element not found")
def binary_Search(l,k):
start = 0; end = len(l)-1
while(start <= end):
m = int((start + end) / 2)
if (l[m]==k):
print(f"Element '{k}' found at index {m}")
return
elif (k > l[m]):
start = m + 1
Ayush Dhankar(BCA-III ) Page 18
2023
PROGRAMMING LAB IN PYTHON
elif (k < l[m]):
end = m - 1
print("Element not found")
print("List to be searched : ",l)
print("\nLinear search result:")
linear_Search(l,3)
print("\nBinary search result:")
binary_Search(l,9)
Output–
Ayush Dhankar(BCA-III ) Page 19
2023
PROGRAMMING LAB IN PYTHON
Assignment No - 16
Q16. Write a program to sort a list using insertion sort and bubble
sort and selection sort.
Coding –
def insertion_Sort(l):
for i in range(1,len(l)):
curr = l[i]
j = i-1
while (l[j]>curr and j>=0):
l[j+1] = l[j]
j -= 1
l[j+1] = curr
return l
def selection_Sort(l):
Ayush Dhankar(BCA-III ) Page 20
2023
PROGRAMMING LAB IN PYTHON
n = len(l)
for i in range(0,n-1):
j = i+1
while (j<n):
if l[j] < l[i]:
temp = l[j]
l[j] = l[i]
l[i] = temp
j += 1
return l
def bubble_Sort(l):
n = len(l)
for i in range(0,n-1):
k=1
for j in range(0,n-i-1):
if l[j] > l[k]:
temp = l[j]
l[j] = l[k]
l[k] = temp
k += 1
return l
def prnt(a):
a = []
return a
l = [10,1,11,7,93,0]
print(f"List before sorting: {l}")
print(f"List after Insertion sort: {insertion_Sort(l)}")
l = [10,1,11,7,93,0]
print(f"List before sorting: {l}")
print(f"List after Selection sort: {selection_Sort(l)}")
l = [10,1,11,7,93,0]
print(f"List before sorting: {l}")
print(f"List after Bubble sort: {bubble_Sort(l)}")
Ayush Dhankar(BCA-III ) Page 21
2023
PROGRAMMING LAB IN PYTHON
Output–
Ayush Dhankar(BCA-III ) Page 22