Python Practical Report
Python Practical Report
A
PRACTICAL REPORT
ON
PROGRAMMING IN PYTHON
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:
else:
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
Output-
Program-3
Aim: - Write a program to create the following pattern for example enter a size:5-
*
**
***
****
*****
Coding-
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
Coding:-
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-
print(n)
list=n.split()
print(list)
Output-
Program-8
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-
if (num % 2 == 0):
else:
Output-
Program-10
Aim:-Write a program to compare three numbers and print the largest one.
Coding-
large=a
if large<b:
large=b
if large<c:
large=c
Output-
Program-11
Coding-
print("Factors of ",x,"is:")
if x % i==0:
print(i)
Output-
Program-12
Coding-
num1=int(input("Enter num"))
num2=int(input("Enter num"))
if num1<num2:
smaller=num1
else:
smaller=num2
gcd=i
Output-
Program-13
Aim:- Write a program to create Stack and implement all its methods, (Use Lists).
Coding-
stack=[]
def push():
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:
if choice==1:
push()
elif choice==2:
pop_element()
elif choice==3:
break
else:
Aim:- Write a program to create Queue and implement all its methods, (Use Lists)
Coding-
queue=[]
def enqueue():
queue.append(element)
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:
Coding-
def search(list1,key):
for i in range(len(list1)):
if key==list1[i]:
break
else:
list1=[34,23,5,6,7,23,8]
print(list1)
search(list1,key)
Output-
[34, 23, 5, 6, 7, 23, 8]
enter the key element=8
key element is found
Program-16
Coding-
def binarysearch(list1,key):
low=0
high=len(list1)-1
found=False
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:
list1=[23,1,4,2,3]
list1.sort()
print(list)
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
Coding-
def insertionsort(my_list):
current_element=my_list[index]
pos=index
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
Coding-
list1=[]
print("enter values:")
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-