Professional Documents
Culture Documents
1.write a program to sum all the elements from n1 to n2 where n1 and n2 are
positive integers.
n1=int(input('enter the first number:'))
if n1>n2:
t=n1
n1=n2
n2=t
sum=0
for i in range(n1,n2+1):
sum+=i
Output:
enter the first number:1
2.Input an array of n numbers and find separate the sum of positive numbers
and negative numbers.
n=int(input('enter the no of items in the list:'))
nums=[]
for i in range(n):
nums.append(int(input()))
nc=0
pc=0
zc=0
sump=0
PYTHON LAB MANUAL
sumn=0
if elt==0:
zc+=1
elif elt<0:
nc+=1
sumn+=elt
else:
pc+=1
sump+=elt
Output:
enter the no of items in the list:4
enter 4 numbers
-8
no of zeros is: 1
nums=[]
for i in range(n):
nums.append(int(input()))
found=False
for i in range(n):
if elt==nums[i]:
found=True
pos=i+1
break
if found:
else:
Output:
enter size of the list:4
enter 4 numbers
2 is found at position 4
PYTHON LAB MANUAL
if high<low:
return -1
mid=(low+high)//2
if elt==a[mid]:
return mid+1
elif elt<a[mid]:
return bsearch(elt,a,low,mid-1)
else:
return bsearch(elt,a,mid+1,high)
a=[]
for i in range(n):
a.append(int(input()))
a.sort()
for i in a:
print(i)
print(a)
p=bsearch(elt,a,0,n-1)
if p<0:
else:
PYTHON LAB MANUAL
Output:
enter size of the list:4
enter 4 numbers
[2, 3, 6, 8]
[2, 3, 6, 8]
[2, 3, 6, 8]
[2, 3, 6, 8]
6 is found at position 3.
def __init__(self):
self.top=-1
self.st=[]
def readsize(self):
def push(self):
if self.top==self.n-1:
print('stack is full')
else:
self.st.append(el)
self.top+=1
def pop(self):
if self.top==-1:
print('stack is empty')
else:
el=self.st[self.top]
del self.st[self.top]
self.top-=1
def display(self):
if self.top==-1:
print('stack is empty')
else:
for i in range(self.top,-1,-1):
print(self.st[i],'')
s=stack()
s.readsize()
while True:
PYTHON LAB MANUAL
if ch==1:
s.push()
elif ch==2:
s.pop()
elif ch==3:
s.display()
else:
break.
Output:
enter size of stack:4
1.push
2.pop
3.display
4.exit
1.push
2.pop
3.display
4.exit
1.push
2.pop
3.display
4.exit
1.push
2.pop
3.display
4.exit
element of stack is
1.push
2.pop
3.display
4.exit
1.push
2.pop
3.display
4.exit
PYTHON LAB MANUAL
1.push
2.pop
3.display
4.exit
element of stack is
2.
strlist=str.split()
n=len(strlist)
st=[]
for i in range(n):
if strlist[i].isdigit():
st.append(int(strlist[i]))
elif strlist[i]=='+':
a=st.pop()
b=st.pop()
st.append(a+b)
elif strlist[i]=='-':
a=st.pop()
b=st.pop()
st.append(a-b)
elif strlist[i]=='*':
PYTHON LAB MANUAL
a=st.pop()
b=st.pop()
st.append(a*b)
elif strlist[i]=='/':
a=st.pop()
b=st.pop()
st.append(a/b)
return st.pop()
val=postevaluation(str)
print('ans=',val).
Output:
enter any postfix expression:4 5 *
ans= 20.
m=int(m)
n=int(n)
p=int(p)
q=int(q)
if n!=p:
else:
for i in range(m):
for j in range(n):
a[i][j]=int(input())
for i in range(p):
for j in range(q):
b[i][j]=int(input())
for i in range(m):
for j in range(q):
for k in range(n):
c[i][j]+=a[i][k]*b[k][j]
print('matrix a is:')
for i in range(len(a)):
print(a[i])
print('matrix b is:')
for i in range(len(b)):
print(b[i])
print('matrix c is:')
for i in range(len(c)):
print(c[i]).
Output:
enter order of a matrix a:3 3
matrix a is:
[2, 3, 4]
[5, 6, 2]
[2, 1, 6]
matrix b is:
[7, 8, 9]
[2, 1, 4]
PYTHON LAB MANUAL
[1, 2, 4]
matrix c is:
if a==0:
r=-c/b
print('root is:'.r)
else:
d=b*b-4*a*c
if d==0:
elif d>0:
r1=(-b+math.sqrt(d))/(2*a)
r2=(-b+math.sqrt(d))/(2*a)
print('root=',r1,'root2=',r2)
else:
rp=-b/(2*a)
ip=math.sqrt(abs(d))/(2*a)
print('root1',rp,'+i',ip)
print('root2',rp,'-i',ip).
Output:
enter the value of a:1
b=[]
n1=len(a)
for i in range(0,n1):
if a[i]<el:
b.append(a[i])
else:
break
b.append(el)
if i!=n-1:
b.append(a[i])
n2=i+1
for i in range(n2,n1):
b.append(a[i])
return b
PYTHON LAB MANUAL
print('enter',n,'element')
a=[]
for i in range(n):
a.append(int(input()))
a.sort()
Output:
enter size of array:5
enter 5 element
return(s[::-1])
def ispalindrome(s):
rev=sreverse(s)
if s==rev:
PYTHON LAB MANUAL
return True
else:
return False
ans=ispalindrome(s)
if ans:
print(s,'is a palindrome')
else:
Output:
enter any string:madam
madam is a palindrome.
for x in word:
d['letters']+=1
if x=='a' or x=='e' or x=='i' or x=='o' or x=='u' or x=='A' or x=='E' or x=='I' or x=='O' or x=='U':
d['vowels']+=1
d['percentage']=(d['vowels']/d['letters'])*100
print('no of letters=',d['letters'])
print('no of vowels=',d['vowels'])
print('percentage of vowels=',d['percentage']).
Output:
enter any word:navya
PYTHON LAB MANUAL
no of letters= 5
no of vowels= 2
c=0
alphabet='abcdefghijklmnopqrstuvwxyz'
pharseletter=''
for ch in pharse:
pharseletter+=ch
for ch in pharseletter:
if ch==letter:
c+=1
if c==26:
return True
else:
return False
if ispangram(pharse):
else:
Output:
enter any pharse:the quick brown fox jumps over the lazy dog
print('5.exit')
if choice==1:
fp=open('foo.txt','r')
print('opening mode',fp.mode)
fp.close()
elif choice==2:
fp=open('foo.txt','w')
fp.write(str)
fp.close()
elif choice==3:
fp=open('foo.txt','r')
fp.close()
elif choice==4:
fp=open('foo.txt','a')
pos=fp.seek(0,0)
fp.write(str)
fp.close()
else:
break
Output:
1.open file in read mode
5.exit
5.exit
opening mode r
ubdt college
5.exit
5.exit
5.exit
def readtime(self):
self.hour=int(input('enter hour:'))
self.minute=int(input('enter minute:'))
self.second=int(input('enter second:'))
def addtime(self,other):
finishtime=time()
finishtime.hour=self.hour+other.hour
finishtime.minute=self.minute+other.minute
finishtime.second=self.second+other.second
if finishtime.second>=60:
finishtime.second-=60
finishtime.minute+=1
if finishtime.minute>=60:
finishtime.minute-=60
finishtime.hour+=1
return finishtime
PYTHON LAB MANUAL
def __str__(self):
return '{0:2d}:{1:2d}:{2:2d}'.format(self.hour,self.minute,self.second)
currenttime=time()
breadtime=time()
currenttime.readtime()
breadtime.readtime()
finishtime=time()
finishtime=currenttime.addtime(breadtime)
print('currenttime time:',currenttime)
Output:
enter current time:
enter hour:2
enter minute:13
enter second:40
enter hour:3
enter minute:12
enter second:16