Python_AD
Python_AD
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
Output –
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 : "))
for i in range(n):
for j in range(i+1):
print()
Output –
Assignment No - 3
Q3. Write a program to print an star pattern.
Coding –
n = int(input("Enter the size of pattern : "))
for j in range(i+1):
print("*",end="")
print()
Output –
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
Output –
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
fact *= i
return fact
Output –
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
if str[i]==str[j]:
a += 1
if a==(int(l/2)):
else:
is_Palindrome(str)
Output–
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
Output –
Assignment No - 8
Q8. Write a program to generate Fibonacci series.
Coding –
#Program to Print fibonacci series
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–
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–
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 : "))
else:
print(f"{num3} is the Largest")
Output–
Assignment No - 11
Q11. Write a program to print factors of a given number.
Coding –
#Program to get the factors of a number
Output–
Assignment No - 12
Q12. Write a method to calculate GCD of two numbers.
Coding –
# Program to calculate GCD of two numbers
# 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)
Output–
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 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}")
s1.pop()
print(f"Stack elements after pop: {s1.list}")
s1.Peek()
Output–
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
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)
def Front(self):
def Rear(self):
s1 = Queue()
s1.Enqueue(1)
s1.Enqueue(2)
s1.Enqueue(3)
s1.Enqueue(4)
s1.Dequeue()
s1.Front()
s1.Rear()
Output–
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
Output–
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
return l
def selection_Sort(l):
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)}")
Output–