PYTHON LAB MANUAL Name: Jitendra Kumar Roll No.: 05035304421 Section: 2
PYTHON LAB MANUAL Name: Jitendra Kumar Roll No.: 05035304421 Section: 2
Program :
OUTPUT :
def evenFibSum(limit) :
if (limit < 2) :
return 0
ef1 = 0
ef2 = 2
sm= ef1 + ef2
return sm
limit = 1000
print(evenFibSum(limit))
OUTPUT :
Program :
def display(n1, n2):
results = []
for i in range(1000, 2000+1):
if (i%7==0) and (i%5!=0):
results.append(i)
return results
n1 = 1000
n2 = 2000
print(display(n1,n2))
OUTPUT :
Program :
def product(list):
p =1
for i in list:
p *= i
print(p)
return p
arr= [1,2,3,4,5,6,7,8,9,10]
c=product(arr) ###### OUTPUT : >
Q 5.Write a function reverse to reverse a list. Without using the
reverse function.
Program :
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print("Before : ")
print(numbers)
L = len(numbers)
for i in range(int(L/2)):
n = numbers[i]
numbers[i] = numbers[L-i-1]
numbers[L-i-1] = n
print("After : ")
print(numbers)
OUTPUT :
Program :
def recur_fibo(n):
if n <= 1:
return n
else:
return(recur_fibo(n-1) + recur_fibo(n-2))
nterms = 10
if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(recur_fibo(i))
OUTPUT:
Program :
tp=(1,2,3,4,5,6,7,8,9,10)
tp1=tp[:5]
tp2=tp[5:]
print(tp1)
print(tp2)
OUTPUT :
Program :
def deletespace(name):
if(len(name)==0):
return ""
else:
str=""
if(ord[name[0]])>chr(65) and ord(name[0])<chr(122):
return str+deletespace(name[1:])
name=input("Please enter the name..")
deletespace(name)
data = f.read()
line = data.splitlines()
words = data.split()
OUTPUT :
Q 11.Write a Python class which has two methods get_String
and print_String. get_String accept a string from the user and
print_String print the stringin upper case
Program:
class IOString():
def __init__(self):
self.str1 = ""
def get_String(self):
self.str1 = input()
def print_String(self):
print(self.str1.upper())
str1 = IOString()
str1.get_String()
str1.print_String()
OUTPUT :
Q 12.Write a Python class named Circle constructed by a radius
and two methods which will compute the area and the perimeter
of a circle
Program:
class Circle():
def __init__(self, r):
self.radius = r
def area(self):
return self.radius**2*3.14
def perimeter(self):
return 2*self.radius*3.14
NewCircle = Circle(8)
print(NewCircle.area())
print(NewCircle.perimeter())
OUTPUT :
Q 13..Write a Python class to reverse a string word by word :
Input string : 'hello Python'
Expected Output : 'Python hello'
Program :
string = "my name is Jitendra kumar sharma"
s = string.split()[::-1]
l = []
for i in s:
l.append(i)
print("Original : "+string)
print("Reversed : ")
print(" ".join(l))
OUTPUT :
Program :
def isbalanced(s):
c= 0
ans=False
for i in s:
if i == "(":
c += 1
elif i == ")":
c-= 1
if c < 0:
return ans
if c==0:
return not ans
return ans
s="{[]}"
print("Given string is balanced :",isbalanced(s))
OUTPUT :
Q 19.Implement a python script to check the element is in the
list or not by using Linear search & Binary search.
Program :
OUTPUT :
Program :
#Bubble sort
def bubblesrt(list):
for iter_num in range(len(list)-1,0,-1):
for idx in range(iter_num):
if list[idx]>list[idx+1]:
temp = list[idx]
list[idx] = list[idx+1]
list[idx+1] = temp
list = [19,2,31,45,6,11,121,27]
bubblesrt(list)
print("Bubble Sort : ")
print(list)
print()
#merger sort
arr = [19,2,31,45,6,11,121,27]
n = len(arr)
# print("Given array is")
# for i in range(n):
# print("%d" % arr[i],end=" ")
mergeSort(arr, 0, n-1)
print("\n\nSorted array is (Merge Sort) ")
for i in range(n):
print("%d" % arr[i],end=" ")
def insertionSort(array):
for step in range(1, len(array)):
key = array[step]
j = step - 1
array[j + 1] = key
data = [19,2,31,45,6,11,121,27]
insertionSort(data)
print()
print()
print('Insertion sort : ')
print(data)
data =[19,2,31,45,6,11,121,27]
size = len(data)
selectionSort(data, size)
print()
print()
print('Selection sort :')
print(data)
OUTPUT :
Q 23.Write a python code for simple GUI calculator using Tk.
Program :
from tkinter import *
def iCalc(source, side):
storeObj = Frame(source, borderwidth=4, bd=4, bg="powder
blue")
storeObj.pack(side=side, expand =YES, fill =BOTH)
return storeObj
def button(source, side, text, command=None):
storeObj = Button(source, text=text, command=command)
storeObj.pack(side=side, expand = YES, fill=BOTH)
return storeObj
class app(Frame):
def __init__(self):
Frame.__init__(self)
self.option_add('*Font', 'arial 20 bold')
self.pack(expand = YES, fill =BOTH)
self.master.title('Calculator')
if __name__=='__main__':
app().mainloop()
OUTPUT :