DEPARTMENT OF COMPUTER SCIENCE
A
PRACTICAL REPORT
ON
PROGRAMMING IN PYTHON
GUIDED BY: SUBMITTED BY:
________________ Roll No. ____________
Name:______________
INDEX
EXPERIMENT SUBMITION
S.NO AIM OF PROGRAM
DATE DATE
Write a Program that reads an integer value
1 and print leap year or not a leap year.
Write a program that takes two numbers as
2 print the sum of these numbers.
Write a program to create the following
pattern for example enter a size: 5-
*
**
3
***
****
*****
Write a function that takes an integer n as
input and calculates the value of 1+1/1!
4
+1/2! +1/n!.
Write a function that takes an integer input
5 and calculate the factorial of that number.
Write a function that takes a string input
6 and checks if it is a palindrome or not.
Write a list function to convert into list as
7 in list (-abc) gives [a,b,c]
Write a program to generate Fibonacci
8
series.
Write a program to check whether the input
9 number is even or odd.
Write a program to compare three numbers
10 and print the largest one.
Write a program to print factors of given
11 numbers.
Write a method to calculate GCD of two
12
numbers.
Write a program to create Stack and
13 implement all its methods, (Use Lists).
Write a program to create Queue and
14
implement all its methods, (Use Lists).
Write a program to implement linear search.
15
Write a program to implement binary search.
16
Write a program to sort a list using insertion
17 sort.
Write a program to sort a list using bubble
18 sort.
Program-1
Aim:-Write a Program that reads an integer value and prints leap year or not a leap
year.
coding-
year=int(input("Enter year:"))
if year%4==0:
print("this year is leap year")
else:
print("this year is not leap year")
Output-
Program-2
Aim: - Write a program that takes two numbers as print the sum of these numbers.
coding-
a=int(input("Enter number:"))
b=int(input("Enter number:"))
c=a+b
print("sum will be:",c)
Output-
Program-3
Aim: - Write a program to create the following pattern for example enter a size:5-
*
**
***
****
*****
Coding-
num=int(input("Enter the number of row:"))
for i in range(1,num+1):
for j in range(1,i+1):
print("*",end=" ")
print()
Output-
Program-4
Aim: - Write a function that takes an integer n as input and calculates the value of
1+1/1! +1/2! +1/n!
Coding:-
n=int(input("Enter a nth term="))
fact=1
sum=1
for i in range(1,n+1):
fact=fact*i
sum=sum+1/fact
print("sum of sequence=",sum)
Output-
Program-5
Aim: - Write a function that takes an integer input and calculate the factorial of that
number.
Coding-
i=int(input("Enter Number:"))
fac=1
while(i>0):
fac=fac*i
i=i-1
print("factorial=",fac)
Output-
Program-6
Aim:- Write a function that takes a string input and checks if it is a palindrome or
not.
Coding-
i=int(input("Enter Number"))
rev=0
x=i
while(i>0):
rev=(rev*10)+i%10
i=i//10
if(x==rev):
print("pallindrome number")
else:
print("not pallindrome number")
Output-
Program-7
Aim:-Write a list function to convert into list as in list (-abc) gives [a,b,c]
Coding-
n=input("Enter the String")
print(n)
list=n.split()
print(list)
Output-
Program-8
Aim:-Write a program to generate Fibonacci series
Coding-
n=int(input("Enter Number"))
x=0
y=1
z=0
while(z<=n):
print(z)
x=y
y=z
z=x+y
Output-
Program-9
Aim:- Write a program to check whether the input number is even or odd.
Coding-
num=int(input("Enter the number to be checked:"))
if (num % 2 == 0):
print (num, " is even number")
else:
print(num, "is odd number")
Output-
Program-10
Aim:-Write a program to compare three numbers and print the largest one.
Coding-
a=int(input("Enter first number"))
b=int(input("Enter second number"))
c=int(input("Enter third number"))
large=a
if large<b:
large=b
if large<c:
large=c
print("The large Number is",large)
Output-
Program-11
Aim:-Write a program to print factors of given numbers
Coding-
x=int(input("Enter the number"))
print("Factors of ",x,"is:")
for i in range (1,x+1):
if x % i==0:
print(i)
Output-
Program-12
Aim:-Write a method to calculate GCD of two numbers
Coding-
num1=int(input("Enter num"))
num2=int(input("Enter num"))
if num1<num2:
smaller=num1
else:
smaller=num2
for i in range (1,smaller+1):
if num1%i==0 and num2%i==0:
gcd=i
print("the gcds is", gcd)
Output-
Program-13
Aim:- Write a program to create Stack and implement all its methods, (Use Lists).
Coding-
stack=[]
def push():
element= input("Enter the element:")
stack.append(element)
print(stack)
def pop_element():
if not stack:
print("stack is empty")
else:
e=stack.pop()
print("removed element",e)
print(stack)
while True:
print("select the operation 1.push 2.pop 3.quit")
choice=int(input())
if choice==1:
push()
elif choice==2:
pop_element()
elif choice==3:
break
else:
print("enter the correct operation")
Program-14
Aim:- Write a program to create Queue and implement all its methods, (Use Lists)
Coding-
queue=[]
def enqueue():
element=input("Enter the element:")
queue.append(element)
print(element,"is added to queue")
def dequeue():
if not queue:
print("queue is empty")
else:
e=queue.pop(0)
print("removed element",e)
def display():
print(queue)
while True:
print("select the operation 1.add 2.remove 3.show 4.quit")
choice=int(input())
if choice==1:
enqueue()
elif choice==2:
dequeue()
elif choice==3:
display()
elif choice==4:
break
else:
print("Enter the correct option")
Output-
Program-15
Aim:- Write a program to implement linear search.
Coding-
def search(list1,key):
for i in range(len(list1)):
if key==list1[i]:
print("key element is found")
break
else:
print("element is not found")
list1=[34,23,5,6,7,23,8]
print(list1)
key=int(input("enter the key element"))
search(list1,key)
Output-
[34, 23, 5, 6, 7, 23, 8]
enter the key element=8
key element is found
Program-16
Aim:- Write a program to implement binary search.
Coding-
def binarysearch(list1,key):
low=0
high=len(list1)-1
found=False
while low<=high and not found:
mid=(low+high)//2
if key==list1[mid]:
found=True
elif key>list1[mid]:
low=mid+1
else:
high=mid-1
if found==True:
print("element is found")
else:
print("element is not found")
list1=[23,1,4,2,3]
list1.sort()
print(list)
key=int(input("enter the key:"))
binarysearch(list1,key)
Output-
[23, 1, 4, 2, 3]
enter the key:9
element is not found
[23, 1, 4, 2, 3]
enter the key:4
element is found
Program-17
Aim:- Write a program to sort a list using insertion sort.
Coding-
def insertionsort(my_list):
for index in range(1,len(my_list)):
current_element=my_list[index]
pos=index
while current_element<my_list[pos-1] and pos>0:
my_list[pos]=my_list[pos-1]
pos=pos-1
my_list[pos]=current_element
list1=[2,4,3,5,1]
insertionsort(list1)
print(list1)
Output-
[1, 2, 3, 4, 5]
Program-18
Aim:- Write a program to sort a list using bubble sort.
Coding-
list1=[]
num=int(input("how many number you want to enter:"))
print("enter values:")
for k in range (num):
list1.append(int(input()))
print("unsorted list:",list1)
for j in range(len(list1)-1,0,-1):
for i in range(j):
if list1[i]>list1[i+1]:
list1[i],list1[i+1]=list1[i+1],list1[i]
print(list)
else:
print(list1)
print()
print("sorted list:",list1)
Output-