Record Programs With Changess (1)
Record Programs With Changess (1)
Record Programs With Changess (1)
CODE:
c=a+b
OUTPUT:
CODE:
c=a
a=b
b=c
OUTPUT:
CODE:
if(a>b):
else:
OUTPUT:
CODE:
a=int(input("Enter a number:"))
if(a%2==0):
else:
OUTPUT:
Enter a number:43
43 is an odd number
Experiment 3(A) Factorial of a number
CODE:
n=int(input("Enter a number:"))
i=1
fact=1
while(i<=n):
fact=fact*i
i=i+1
OUTPUT:
Enter a number:5
CODE:
n=int(input("Enter a number:"))
i=1
while(i<=n):
print(i)
i=i+1
OUTPUT:
Enter a number:7
7
Experiment 4(A) Electricity Billing
CODE:
if(u<0):
else:
Name: ABD
CODE:
i=0
total=0
itemlist=[]
qtylist=[]
ratelist=[]
while(i<n):
itemlist.append(item)
qtylist.append(qty)
ratelist.append(rate)
i=i+1
for x in range(n):
print(itemlist[x],"X",qtylist[x],"x",ratelist[x],"=",qtylist[x]*ratelist[x])
total=total+(qtylist[x]*ratelist[x])
print("Total=",total)
OUTPUT:
Apple X 5 x 25 = 125
Chips X 10 x 10 = 100
Total= 225
Experiment 5(A) Circulate the value of n variables
CODE:
l=l[i:]+l[:i]
print(l)
OUTPUT:
CODE:
d=(((x2-x1)**2)+((y2-y1)**2))**0.5
OUTPUT:
CODE:
s=0
for i in range(n+1):
s+=i
OUTPUT:
CODE:
a=0
b=1
i=3
print(a,end='')
print(b,end='')
while(i<=n):
c=a+b
print(c,end='')
a=b
b=c
i=i+1
OUTPUT:
0112358
Experiment 6(C) Pascal’s Triangle
CODE:
def fact(n):
if(n==0):
return 1
else:
return(n*fact(n-1))
print("PASCAL TRIANGLE")
i=0
while(i<row):
j=0
while(j<=row-i-1):
print(end=' ')
j=j+1
k=0
while(k<=i):
print(fact(i)//(fact(k)*fact(i-k)),end=' ')
k=k+1
print()
i+=1
OUTPUT:
PASCAL TRIANGLE
11
121
1331
14641
Experiment 6(D) Pyramid Pattern
CODE:
i=1
while (i<=n):
j=0
while (j<i):
print("*",end=" ")
j += 1
i += 1
print()
OUTPUT:
**
***
****
*****
Experiment 7(A) Palindrome
CODE:
string=input("Enter a string:")
if(string==string[::-1]):
print(string,'is a palindrome')
else:
OUTPUT(1):
Enter a string:madam
madam is a palindrome
OUTPUT(2):
Enter a string:python
CODE:
string=input("Enter a string:")
reverse=''
count=len(string)
while(count>0):
reverse+=string[count-1]
count-=1
OUTPUT:
Enter a string:python
CODE:
s=input('Enter a string:')
c=0
for i in range(len(s)):
if(s[i]!=' '):
c+=1
OUTPUT:
CODE:
str=input('Enter a string:')
result=str.replace('o','a')
OUTPUT:
Enter a string:opple
CODE:
def fact(n):
if(n==1):
return 1
else:
return(n*fact(n-1))
i=int(input('Enter a number:'))
OUTPUT:
Enter a number:5
CODE:
def largest(l):
max=l[0]
for i in l:
if(i>max):
max=i
return max
l=eval(input("Enter a list:"))
OUTPUT:
Enter a list:[12,14,54,66,37,8]
CODE:
def triangle(b,h):
a =0.5*b*h
def circle(r):
a =3.14*r*l
a =l*b
def cone(r,l):
a=3.14*r*(r+l)
def cylinder(h,r):
a=2*3.14*r*(h+r)
print('1.Triangle \n2.Circle\n3.Rectangle\n4.Cone\n5.Cylinder')
if (i==1):
triangle(b,h)
elif(i==2):
circle(r)
elif(i==3):
rectangle(l,b)
elif(i==4):
cone(r,l)
elif(i==5):
cylinder(h,r)
else:
print('#####Invalid input#####')
OUTPUT(1):
1. Triangle
2.Circle
3. Rectangle
4.Cone
5.Cylinder
Enter your choice:1
OUTPUT(2):
1.Triangle
2.Circle
3.Rectangle
4.Cone
5.Cylinder
CODE:
cost=(100,500,200,800)
qty=[0,0,0,0]
c=1
i=0
while (c!=0):
x=int(input("enter quantity:"))
qty[c-1]=x
elif(c==5):
print("wood cost=",cost[0],"quantity=",qty[0],
elif(c==6):
total=0
while(i<4):
total=total+cost[i]*qty[i]
i=i+1
elif(c!=0):
print("invalid choice")
OUTPUT:
5.display elements
0.for exit
enter quantity:200
5.display elements
0.for exit
enter quantity:450
5.display elements
0.for exit
5.display elements
0.for exit
5.display elements
0.for exit
0
Experiment 9(B) Binary search
CODE:
f=0
c=0
ln= len(l) - 1
while(f<=ln):
m =(f+ln)//2
if(l[m]==ele):
c=1
break
elif(l[m]<ele):
f = m+1
else:
ln= m-1
if (c==1):
else:
OUTPUT:
47 found at position 6
Experiment 9(C) Bubble sort
CODE:
def bubbleSort(arr):
n = len(arr)
for i in range(n-1):
bubbleSort(arr)
for i in range(len(arr)):
OUTPUT:
11 12 22 25 34 64 90
Experiment 10 Sets and Dictionary
CODE:
print(comps+":"+comps_desc[comps]+"\n")
OUTPUT:
Spark Plug:It comprises of combustion chamber valve train and spark plugs
Connecting Rod:It is made of metals,which are used for joining a rotating wheel
Piston Ring:It provides a sliding seal between the outer edge of the piston
CODE:
import numpy as np
l=[1,7,0,2,5,6]
array1=np.array(l)
print('array 1 is',array1)
array2=np.asarray(array1)
print('array 2 is',array2)
array2[3]=23
print()
print('After chances-')
print('array 1 is',array1)
print('array 2 is',array2)
OUTPUT:
array 1 is [1 7 0 2 5 6]
array 2 is [1 7 0 2 5 6]
After chances-
array 1 is [ 1 7 0 23 5 6]
array 2 is [ 1 7 0 23 5 6]
Experiment 11(B) Pandas
CODE:
import pandas as pd
s=pd.DataFrame(columns=['Maths','Physics','Chemistry','Python','English'],index=range(1,n+1))
for i in ['Maths','Physics','Chemistry','Python','English']:
for j in range(1,n+1):
print("For Rollno.",j)
s[i][j]=int(input("\t:"))
print(s)
OUTPUT:
For Rollno. 1
:67
For Rollno. 2
:78
For Rollno. 3
:45
For Rollno. 1
:77
For Rollno. 2
:65
For Rollno. 3
:89
For Rollno. 1
:44
For Rollno. 2
:90
For Rollno. 3
:50
For Rollno. 1
:63
For Rollno. 2
:81
For Rollno. 3
:49
For Rollno. 1
:41
For Rollno. 2
:77
For Rollno. 3
:94
Maths Physics Chemistry Python English
1 67 77 44 63 41
2 78 65 90 81 77
3 45 89 50 49 94
Experiment 11(C) Program using matplotlib
CODE:
x=[1,2,3]
y=[5,7,4]
x2=[1,2,3]
y2=[10,14,12]
plt.plot(x,y,label="Line1")
plt.plot(x2,y2,label="Line2")
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.title('LINE GRAPH')
plt.legend()
plt.show()
OUTPUT:
Experiment 11(D) Scipy
CODE:
def integrand(x,a,b):
return a*x**2+b
a=2
b=1
I= quad(integrand,0,1,args=(a,b))
print(I)
OUTPUT:
(1.6666666666666667, 1.8503717077085944e-14)
Experiment 12(A) Copy from one file to another
CODE:
f1=open("sample.txt","r")
f2=open("Copyfile.txt","w")
for line in f1:
f2.write(line)
f1.close()
f2.close()
OUTPUT:
The sample file has: A quick brown fox jumps over the lazy dog
The copyfile contains: A quick brown fox jumps over the lazy dog
Experiment 12(B) Number of words in a text file
CODE:
file=open("sample.txt","r")
num_words=0
words=line.split()
num_words=len(words)
OUTPUT:
The file sample.txt contains: A quick brown fox jumps over the lazy dog
no. of words: 9
Experiment 12(C) Longest word in a file
CODE:
def longest_words(filename):
words=infile.read().split()
max_len=len(max(words,key=len))
print(longest_words("sample.txt"))
OUTPUT:
The file sample.txt contains: A quick brown fox jumps over the lazy dog
[‘quick’,’brown’,’jumps’]
Experiment 13(A) Divide by 0 Error
CODE:
try:
except ZeroDivisionError:
except ValueError:
OUTPUT:
CODE:
try:
if(age>18):
print("Eligible to vote")
else:
except ValueError:
except IOError:
except:
OUTPUT:
CODE:
try:
if(marks>50):
print("pass")
else:
print("fail")
except ValueError:
except IOError:
except:
OUTPUT:
Fail
Experiment 14(A) Simulate bouncing ball using Pygame
CODE:
OUTPUT: