0% found this document useful (0 votes)
4 views3 pages

Python Lab Programs

The document contains multiple Python programs demonstrating various data structures and concepts, including generators, stacks, queues, binary trees, and packages. Each program includes user interaction for operations such as pushing and popping elements in stacks, enqueuing and dequeuing in queues, and inserting elements in binary trees. Additionally, it showcases how to create and use a package with multiple modules for arithmetic and geometry operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Lab Programs

The document contains multiple Python programs demonstrating various data structures and concepts, including generators, stacks, queues, binary trees, and packages. Each program includes user interaction for operations such as pushing and popping elements in stacks, enqueuing and dequeuing in queues, and inserting elements in binary trees. Additionally, it showcases how to create and use a package with multiple modules for arithmetic and geometry operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#PROGRAM 38: Write a program to illustrate the concept of generators.

def fib():
a,b=0,1
while True:
yield a
a,b=b,a+b
print("Generation of Fibonacci series:")
for f in fib():
if f>100:
break
print(f,end=" ")

OUTPUT
Generation of Fibonacci series:
0 1 1 2 3 5 8 13 21 34 55 89

#PROGRAM39 Write a program to illustrate to show how to simulate a stack,using


lists.
l=eval(input("Enter list:"))
print(l)
print("Enter choice: [Link]\[Link]")
while(True):
c=int(input("Enter choice:"))
if c==1:
n=input("Enter element to append")
[Link](n)
print("After push operation list is:",l)
elif c==2:
[Link]()
print("After pop operation list is:",l)
else:
break

OUTPUT

Enter list:[1,2,3]
[1, 2, 3]
Enter choice: [Link] [Link]
Enter choice:1
Enter element to append4
After push operation list is: [1, 2, 3, '4']
Enter choice:1
Enter element to append5
After push operation list is: [1, 2, 3, '4', '5']
Enter choice:2
After pop operation list is: [1, 2, 3, '4']
Enter choice:2
After pop operation list is: [1, 2, 3]
Enter choice:3

#PROGRAM 40:Write a program to show how to simulate a queue,using list


l=eval(input("Enter list:"))
print(l)
print("Enter choice: [Link]\[Link]")
while(True):
c=int(input("Enter choice:"))
if c==1:
n=input("Enter element to append")
[Link](n)
print("After Enqueuing operation list is:",l)
elif c==2:
[Link](0)
print("After Dequeuing operation list is:",l)
else:
break

OUTPUT

Enter list:[1,2,3]
[1, 2, 3]
Enter choice: [Link] [Link]
Enter choice:1
Enter element to append4
After Enqueuing operation list is: [1, 2, 3, '4']
Enter choice:1
Enter element to append5
After Enqueuing operation list is: [1, 2, 3, '4', '5']
Enter choice:2
After Dequeuing operation list is: [2, 3, '4', '5']
Enter choice:2
After Dequeuing operation list is: [3, '4', '5']
Enter choice:3

PROGRAM 41:Write a program to show how to simulate a binary tree,using lists.

import os
def create():
l=list()
return l
def enterno(l,k):
if(len(l)==0):
[Link](list())
[Link](k)
[Link](list())
elif(l[1]<k):
enterno(l[2],k)
else:
enterno(l[0],k)
return 0
while(True):
i=int(input("[Link] 2:enter no [Link] "))
if(i==1):
t=create()
if(i==2):
k=int(input("Enter the element to insert:"))
enterno(t,k)
if(i==3):
print(t)
if(i>3):
os._exit(0)

OUTPUT

[Link] 2:enter no [Link] 1


[Link] 2:enter no [Link] 2
Enter the element to insert:15
[Link] 2:enter no [Link] 2
Enter the element to insert:20
[Link] 2:enter no [Link] 2
Enter the element to insert:35
[Link] 2:enter no [Link] 2
Enter the element to insert:5
[Link] 2:enter no [Link] 2
Enter the element to insert:12
[Link] 2:enter no [Link] 3
[[[], 5, [[], 12, []]], 15, [[], 20, [[], 35, []]]]
[Link] 2:enter no [Link] 4

#PROGRAM 42:Create a package with multiple modules and demonstrate how to use the
package.

#mypack/[Link]
def add(a, b):
return a + b

def subtract(a, b):


return a - b

#mypack/[Link]
import math

def area_of_circle(radius):
return [Link] * radius ** 2

def perimeter_of_square(side):
return 4 * side

#mypack/__init__.py
from .arithmetic import add, subtract
from .geometry import area_of_circle, perimeter_of_square

#[Link]
import mypack

print("Add:",[Link](2, 3))
print("Subtract:", [Link](10, 4))
print("Area of Circle:", mypack.area_of_circle(5))
print("Perimeter of Square:", mypack.perimeter_of_square(6))

output
Add: 5
Subtract: 6
Area of Circle: 78.53981633974483
Perimeter of Square: 24

You might also like