0% found this document useful (0 votes)
6 views26 pages

Python Programming Lab Assignments 2023

The document contains a series of programming assignments for a Python lab course for the academic year 2023-24. Each assignment includes a problem statement, source code, and expected output, covering various programming concepts such as leap year calculation, pattern printing, factorial computation, palindrome checking, and data structures like stacks and queues. The assignments aim to enhance students' programming skills through practical coding exercises.

Uploaded by

guneetbhatia2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

Python Programming Lab Assignments 2023

The document contains a series of programming assignments for a Python lab course for the academic year 2023-24. Each assignment includes a problem statement, source code, and expected output, covering various programming concepts such as leap year calculation, pattern printing, factorial computation, palindrome checking, and data structures like stacks and queues. The assignments aim to enhance students' programming skills through practical coding exercises.

Uploaded by

guneetbhatia2005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 1
Q1. Write a program that reads an integer value and prints —leap year or —
not a leap year.

Source code –
#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 –

Page 1
PROGRAMMING LAB IN PYTHON 2023-24

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

Source code–

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 –

Page 2
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 3
Q3. Write a program to print an star pattern.
Source code–

n = int(input("Enter the size of pattern :

")) for i in range (n):

for j in range(i+1):

print("*",end="")

print()

Output –

Page 3
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 4
Q4. Write a program to print the sum of the series 1 + 1/1! + 1/2!
+...+ 1/n! .

Source code–
#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 –

Page 4
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 5
Q5. Write a function that takes an integer input and calculates the
factorial of that number.
Source code–

#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 –

Page 5
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 6
Q6. Write a function that takes a string input and checks if it is a
palindrome or not.
Source code–

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–

Page 6
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 7
Q7. Write a list function to convert a string into a list, as in list (abc)
gives [a, b, c].

Source code–
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 –

Page 7
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 8
Q8. Write a program to generate Fibonacci series.

Source code–

#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–

Page 8
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 9
Q9. Write a program to check whether the input number is even or
odd.

Source code–

#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–

Page 9
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 10
Q10. Write a program to compare three numbers and print the
largest one.

Source code–

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–

Page 10
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 11
Q11. Write a program to print factors of a given number.

Source code–

#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–

Page 11
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 12
Q12. Write a method to calculate GCD of two numbers.

Source code–

# Program to calculate GCD of two

numbers def gcd(a, b):

# Everything divides

0 if (a == 0):

return b

if (b ==

0): return

# 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: "))

Page 12
b = int(input("Enter the second number: "))

print(f'GCD({a}, {b}) = {gcd(a,b)}')

Page 13
PROGRAMMING LAB IN PYTHON 2023-24

Output–

Page 14
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 13
Q13. Write a program to create Stack Class and implement all its
methods, (Use Lists).

Source code–

class Stack:
list = []
ptr = -1
def push(self, a):
[Link](a)
[Link] += 1

def pop(self): if ([Link] == -1):


print("Stack underflow")
return
[Link] -=
1
return ([Link]())

def Peek(self):
print(f"The value at the top of the stack is : {[Link][[Link]]}")

s1 = Stack()

[Link](1)
[Link](2)
[Link](3)
print(f"Stack elements: {[Link]}")
[Link](4)
print(f"Stack elements after push: {[Link]}")

[Link]()
print(f"Stack elements after pop:

{[Link]}") [Link]()

Page 15
PROGRAMMING LAB IN PYTHON 2023-24

Output–

Page 16
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 14
Q14. Write a program to create Queue Class and implement all its
methods, (Use Lists).

Source code–

class Queue:

list = []

head = -1

tail = -1

def Enqueue(self, a):

[Link](a)

[Link] += 1

def Dequeue(self):

if ([Link] == [Link]):

print("Underflow")

return

[Link] += 1

[Link]([Link])

def Front(self):

print(f"The value at the front of the queue is : {[Link][([Link])]}")

def Rear(self):

print(f"The value at the rear of the queue is : {[Link][([Link])-1]}")

Page 17
PROGRAMMING LAB IN PYTHON 2023-24

s1 = Queue()

[Link](1)

[Link](2)

[Link](3)

print(f"Queue elements: {[Link]}")

[Link](4)

print(f"Queue elements after enqueue: {[Link]}")

[Link]()

print(f"Queue elements after dequeue: {[Link]}")

[Link]()

[Link]()

Output–

Page 18
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 15
Q15. Write a program to implement linear and binary search on lists.

Source code–

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
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)

Page 19
PROGRAMMING LAB IN PYTHON 2023-24

Output–

Page 20
PROGRAMMING LAB IN PYTHON 2023-24

Assignment No - 16
Q16. Write a program to sort a list using insertion sort and bubble
sort and selection sort.

Source code–

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):
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]:
Page 21
temp = l[j]

Page 22
PROGRAMMING LAB IN PYTHON 2023-24

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–

Page 23
PROGRAMMING LAB IN PYTHON 2023-24

[Link] a programme to find greatest of three numbers, inputed


by the users

Source 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):

print("first number is greatest")

elif(num2 > num1 and num2 > num3):

print("second number is greatest")

else:
print ("Third Number is greatest")

Page 24
[Link] a programme to calculate simple interests.

Source code: -
# Function to calculate simple interest
def simple_interest(principal, rate, time):
SI = (principal * rate * time) /
100 return SI

# Input values
P = float(input("Enter the principal amount: "))
R = float(input("Enter the rate of interest: "))
T = float(input("Enter the time period in years: "))

# Calculate simple interest


interest = simple_interest(P, R, T)

# Output the result


print(f"The simple interest is: {interest}")

output: -

Page 25
[Link] a programme to calculate factorial of given number.

Source code: -
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)

# Example usage:
number = int(input("Enter a number to calculate its factorial: "))
print(f"The factorial of {number} is {factorial(number)}")

output: -

Page 26

You might also like