0% found this document useful (0 votes)
82 views24 pages

Ayushi

The document provides a list of 16 programming projects in Python. Each project provides a question or problem and sample code to solve that problem. The projects cover topics like checking if a year is a leap year, generating patterns, calculating factorials and series, checking palindromes, and sorting lists. The document is intended to provide hands-on programming practice for students in areas like loops, functions, strings, lists, and more.

Uploaded by

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

Ayushi

The document provides a list of 16 programming projects in Python. Each project provides a question or problem and sample code to solve that problem. The projects cover topics like checking if a year is a leap year, generating patterns, calculating factorials and series, checking palindromes, and sorting lists. The document is intended to provide hands-on programming practice for students in areas like loops, functions, strings, lists, and more.

Uploaded by

Sahil Sinha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Programming In Python BCA-III (2022-23)

S List Of Practical Pag Date Signatur


No. e e
No.

1 Write a program that reads an integer value and prints —leap year 2 04-7-22
or —not a leap year.

2 Write a program that takes a positive integer and then produces n 3 07-7-22
lines of output shown as follows.

3 Write a program to create the following Pattern 4 12-7-22


For example enter a size: 5 -
*
**
**
****
*****

4 Write a function that takes an integer n as input and calculates the 5 15-7-22
value of 1 + 1/1! + 1/2! + 1/n!

5 Write a function that takes an integer input and calculates the 6 19-7-22
factorial of that number.

6 Write a function that takes a string input and checks if it is a 7 25-7-22


palindrome or not.

7 Write a list function to convert a string into a list, as in list (-abc) 8 29-7-22
gives [a, b, c].

8 Write a program to generate Fibonacci series. 9 04-8-22

9 Write a program to check whether the input number is even or odd. 10 10-8-22

10 Write a program to compare three numbers and print the largest 11 17-8-22
one.

11 Write a program to print factors of a given number. 12 25-8-22

12 Write a method to calculate GCD of two numbers. 13 6-9-22

13 Write a program to create Stack Class and implement all its methods, 14 13-9-22
(Use Lists).

Ayushi Singh Page 1


Programming In Python BCA-III (2022-23)

14 Write a program to create Queue Class and implement all its 16 22-9-22
methods, (Use Lists)

15 Write a program to implement linear and binary search on lists, 19 6-10-22

16 Write a program to sort a list using insertion sort and bubble sort 22 14-10-
and selection sort.
22

Project -1

Q[1] Write a program that reads an integer value and prints —leap year or —not a leap
year.

year = int(input("Enter a year: "))

# Check if the year is a leap year

if (year % 4 == 0 or year % 100 == 0 and year % 400 == 0):

print(year, "is a leap year")

else:

print(year, "is not a leap year")

Output:-

Ayushi Singh Page 2


Programming In Python BCA-III (2022-23)

Enter a year: 2020


2020 is a leap year

  Enter a year: 2023


2023 is not a leap year

Project -2

Q[2] Write a program that takes a positive integer and then produces n lines of output
shown as follows.

n = int(input("Enter a positive integer: "))

for i in range(1, n+1):

print(i, "This is line number", i)

Output:-

Ayushi Singh Page 3


Programming In Python BCA-III (2022-23)

Project -3

Q[3] Write a program to create the following Pattern for example enter a size: 5

*
* *
* * *
* * * *
* * * * *

n = int(input("Enter the number :5"))

# Loop to print half pyramid pattern

for i in range(1, n+1):

for j in range(1, i+1):

print("*", end=" ")

print()

Output:-

 Enter the number :5


   
     *

Ayushi Singh Page 4


Programming In Python BCA-III (2022-23)

     * *
     * * *
     * * * *
     * * * * *

Project -4

Q[4] Write a function that takes an integer n as input and calculates the value of
1 + 1/1! + 1/2! + 1/n!

def calculate_sum():

# Get user input for n

n = int(input("Enter an integer n: "))

# Initialize sum to 0

sum = 0

# Iterate from 1 to n

for i in range(1, n+1):

# Calculate the factorial of i

factorial = 1

for j in range(1, i+1):

factorial *= j

# Add 1/i! to the sum

sum += 1/factorial

# Add 1 to the sum and return the result

return sum + 1

# simply call this function

result = calculate_sum()

print(result)

Ayushi Singh Page 5


Programming In Python BCA-III (2022-23)

Output :-
Enter an integer n: 5

2.716666666666667

Project - 5

Q[5] Write a function that takes an integer input and calculate the factorial of that number.

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n-1)

# get input from the user

n = int (input ("Enter a positive integer: "))

# calculates and print the factorial

result = factorial (n)

print(f"The factorial of {n} is {result}.")

Output :-

Ayushi Singh Page 6


Programming In Python BCA-III (2022-23)

Enter a positive integer: 5

The factorial of 5 is 120.

Project - 6

Q[6] Write a function that takes a string input and checks if it is a palindrome or not.

string = input(("Enter a string :"))  

if (string==string[::-1]) :  

      print("The letter is a palindrome")  
else:  
      print("The letter is not a palindrome")

Output :-

Ayushi Singh Page 7


Programming In Python BCA-III (2022-23)

Project – 7

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

def string_to_list():

# Get user input string

input_string = input("Enter a string: ")

# Create an empty list

char_list = []

# Iterate through each character in the string

for char in input_string:

# Add the current character to the list

char_list.append(char)

# Return the list of characters

return char_list

char_list = string_to_list()

print(char_list)

Output :-

Enter a string: abc

['a', 'b', 'c']

Ayushi Singh Page 8


Programming In Python BCA-III (2022-23)

Project - 8

Q[8] Write a program to generate Fibonacci series.

nterms = int(input("How many terms? "))

# first two terms

n1, n2 = 0, 1

count = 0

# check if the number of terms is valid

if nterms <= 0:

print("Please enter a positive integer")

elif nterms == 1:

print("Fibonacci sequence upto", nterms, ":")

print(n1)

else:

print("Fibonacci sequence")
while count < nterms:
print(n1)

nth = n1 + n2

# update values

n1 = n2

n2 = nth

count += 1

Output :-
How many terms? 5

Fibonacci sequence

Ayushi Singh Page 9


Programming In Python BCA-III (2022-23)

Project – 9

Q[9] Write a program to check whether the input number is even or odd.

num = int(input("Enter a number: "))

if num % 2 == 0:

    print("The number is even.")

else:

    print("The number is odd.")

Output:-

Enter a number: 3
    The number is odd.

    Enter a number: 2
    The number is even.

Ayushi Singh Page 10


Programming In Python BCA-III (2022-23)

Project – 10

Q[10] Write a program to compare three numbers and print the largest one.

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

num3 = int(input("Enter the third number: "))

if num1 > num2 and num1 > num3:

print(num1, "is the largest number.")

elif num2 > num1 and num2 > num3:

print(num2, "is the largest number.")

else:

print(num3, "is the largest number.")

Output :

Enter the first number: 5


Enter the second number: 7
Enter the third number: 2

7 is the largest number.

Ayushi Singh Page 11


Programming In Python BCA-III (2022-23)

Project – 11

Q[11] Write a program to print factors of a given number.

num = int(input("Enter a number: "))

for i in range(1, num + 1):

if num % i == 0:

print(i)

Output :

Enter a number: 15

1
3
5
15

Ayushi Singh Page 12


Programming In Python BCA-III (2022-23)

Project – 12

Q[12] Write a method to calculate GCD of two numbers.

def gcd(a, b):

while b != 0:

a, b = b, a % b

return a

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

print("The GCD of", num1, "and", num2, "is", gcd(num1, num2))

Output :

Enter the first number: 15


Enter the second number: 20

The GCD of 15 and 20 is 5

Ayushi Singh Page 13


Programming In Python BCA-III (2022-23)

Project – 13

Q[13] Write a program to create Stack Class and implement all its methods, (Use Lists).

class Stack:
def __init__(self):
self.items = []

def is_empty(self):
return not self.items

def push(self, item):


self.items.append(item)

def pop(self):
return self.items.pop()

def peek(self):
return self.items[-1]

def size(self):
return len(self.items)

stack = Stack()

while True:
print("Enter 1 to push an element to the stack.")
print("Enter 2 to pop an element from the stack.")
print("Enter 3 to peek at the top element of the stack.")
print("Enter 4 to check the size of the stack.")
print("Enter 5 to quit.")

choice = int(input())

if choice == 1:

element = int(input("Enter the element to push: "))

Ayushi Singh Page 14


Programming In Python BCA-III (2022-23)

stack.push(element)

elif choice == 2:

if stack.is_empty():

print("Cannot pop from an empty stack.")

else:

print("Popped element:", stack.pop())

elif choice == 3:

if stack.is_empty():

print("Cannot peek at an empty stack.")

else:

print("Top element:", stack.peek())

elif choice == 4:

print("Size of stack:", stack.size())

else:

break

Output :-

Enter 1 to push an element to the stack.


Enter 2 to pop an element from the stack.
Enter 3 to peek at the top element of the stack.
Enter 4 to check the size of the stack.
Enter 5 to quit.
1
Enter the element to push: 2

2
Popped element: 2

3
Cannot peek at an empty stack.

4
Size of stack: 0

Ayushi Singh Page 15


Programming In Python BCA-III (2022-23)

Project – 14

Q[14] Write a program to create Queue Class and implement all its methods, (Use Lists)

class Queue:

def __init__(self):

self.queue = []

def enqueue(self, item):

self.queue.append(item)

def dequeue(self):

return self.queue.pop(0)

def peek(self):

return self.queue[0]

def is_empty(self):

return len(self.queue) == 0

def size(self):

return len(self.queue)

# Test the Queue class

queue = Queue()

while True:

print("Enter 1 to enqueue an element into the queue")

print("Enter 2 to dequeue an element from the queue")

print("Enter 3 to peek at the front element of the queue")

Ayushi Singh Page 16


Programming In Python BCA-III (2022-23)

print("Enter 4 to check if the queue is empty")

print("Enter 5 to check the size of the queue")

print("Enter 0 to exit")

choice = int(input())

if choice == 1:

value = int(input("Enter an integer to enqueue into the queue: "))

queue.enqueue(value)

elif choice == 2:

if queue.is_empty():

print("The queue is empty")

else:

print(f"Dequeued {queue.dequeue()} from the queue")

elif choice == 3:

if queue.is_empty():

print("The queue is empty")

else:

print(f"Front element of the queue: {queue.peek()}")

elif choice == 4:

print(queue.is_empty())

elif choice == 5:

print(f"Size of the queue: {queue.size()}")

elif choice == 0:

break

else:

print("Invalid choice")

Ayushi Singh Page 17


Programming In Python BCA-III (2022-23)

Output :-

Enter 1 to enqueue an element into the queue


Enter 2 to dequeue an element from the queue
Enter 3 to peek at the front element of the queue
Enter 4 to check if the queue is empty
Enter 5 to check the size of the queue
Enter 0 to exit

1
Enter an integer to enqueue into the queue: 3

Enter 1 to enqueue an element into the queue


Enter 2 to dequeue an element from the queue
Enter 3 to peek at the front element of the queue
Enter 4 to check if the queue is empty
Enter 5 to check the size of the queue
Enter 0 to exit

2
Dequeued 3 from the queue

Enter 1 to enqueue an element into the queue


Enter 2 to dequeue an element from the queue
Enter 3 to peek at the front element of the queue
Enter 4 to check if the queue is empty
Enter 5 to check the size of the queue
Enter 0 to exit

3
The queue is empty
Enter 1 to enqueue an element into the queue
Enter 2 to dequeue an element from the queue
Enter 3 to peek at the front element of the queue
Enter 4 to check if the queue is empty
Enter 5 to check the size of the queue
Enter 0 to exit

4
True

Ayushi Singh Page 18


Programming In Python BCA-III (2022-23)

Project – 15

Q[15] Write a program to implement linear and binary search on lists.

def linear_search(arr, target):

for i in range(len(arr)):

if arr[i] == target:

return i

return -1

def binary_search(arr, target):

left = 0

right = len(arr) - 1

while left <= right:

mid = (left + right) // 2

if arr[mid] == target:

return mid

elif arr[mid] < target:

left = mid + 1

else:

right = mid - 1

return -1

# Test the search functions

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

while True:

print("Enter 1 to perform linear search")

Ayushi Singh Page 19


Programming In Python BCA-III (2022-23)

print("Enter 2 to perform binary search")

print("Enter 0 to exit")

choice = int(input())

if choice == 1:

target = int(input("Enter an integer to search for (linear search): "))

result = linear_search(arr, target)

if result == -1:

print("Element not found")

else:

print(f"Element found at index {result}")

elif choice == 2:

target = int(input("Enter an integer to search for (binary search): "))

result = binary_search(arr, target)

if result == -1:

print("Element not found")

else:

print(f"Element found at index {result}")

elif choice == 0:

break

else:

print("Invalid choice")

Ayushi Singh Page 20


Programming In Python BCA-III (2022-23)

Output :-

Enter 1 to perform linear search


Enter 2 to perform binary search
Enter 0 to exit

1
Enter an integer to search for (linear search): 4
Element found at index 3

Enter 1 to perform linear search


Enter 2 to perform binary search
Enter 0 to exit

2
Enter an integer to search for (binary search): 8
Element found at index 7

Project – 16
Ayushi Singh Page 21
Programming In Python BCA-III (2022-23)

# Write a program to sort a list using insertion sort and bubble sort and selection sort.

def insertion_sort(arr):

for i in range(1, len(arr)):

key = arr[i]

j=i-1

while j >= 0 and key < arr[j]:

arr[j+1] = arr[j]

j -= 1

arr[j+1] = key

return arr

def bubble_sort(arr):

for i in range(len(arr)):

for j in range(i+1, len(arr)):

if arr[i] > arr[j]:

arr[i], arr[j] = arr[j], arr[i]

return arr

def selection_sort(arr):

for i in range(len(arr)):

min_index = i

for j in range(i+1, len(arr)):

if arr[j] < arr[min_index]:

min_index = j

arr[i], arr[min_index] = arr[min_index], arr[i]

return arr

Ayushi Singh Page 22


Programming In Python BCA-III (2022-23)

# Test the sort functions

arr = [5, 2, 4, 1, 3]

while True:

print("Enter 1 to sort using insertion sort")

print("Enter 2 to sort using bubble sort")

print("Enter 3 to sort using selection sort")

print("Enter 0 to exit")

choice = int(input())

if choice == 1:

result = insertion_sort(arr)

print(result)

elif choice == 2:

result = bubble_sort(arr)

print(result)

elif choice == 3:

result = selection_sort(arr)

print(result)

elif choice == 0:

break

else:

print("Invalid choice")

Output :-
Ayushi Singh Page 23
Programming In Python BCA-III (2022-23)

Enter 1 to sort using insertion sort


Enter 2 to sort using bubble sort
Enter 3 to sort using selection sort
Enter 0 to exit

1
[1, 2, 3, 4, 5]

Enter 1 to sort using insertion sort


Enter 2 to sort using bubble sort
Enter 3 to sort using selection sort
Enter 0 to exit

2
[1, 2, 3, 4, 5]

Enter 1 to sort using insertion sort


Enter 2 to sort using bubble sort
Enter 3 to sort using selection sort
Enter 0 to exit

3
[1, 2, 3, 4, 5]

Enter 0 to exit

Ayushi Singh Page 24

You might also like