[go: up one dir, main page]

0% found this document useful (0 votes)
2 views47 pages

Record Programs With Changess (1)

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 47

Experiment 1(A) Addition of two numbers

CODE:

a=int(input("Enter 1st number:"))

b=int(input("Enter 2nd number:"))

c=a+b

print('The sum of',a,'and',b,'is:',c)

OUTPUT:

Enter 1st number:15

Enter 2nd number:20

The sum of 15 and 20 is: 35


Experiment 1(B) Swapping of two numbers

CODE:

a=int(input("Enter 1st number:"))

b=int(input("Enter 2nd number:"))

c=a

a=b

b=c

print("The value of a is:",a)

print("The value of b is:",b)

OUTPUT:

Enter 1st number:10

Enter 2nd number:20

The value of a is: 20

The value of b is: 10


Experiment 2(A) Greatest of two numbers

CODE:

a=int(input("Enter the 1st number:"))

b=int(input("Enter the 2nd number:"))

if(a>b):

print(a,"is the greatest number")

else:

print(b,"is the greatest number")

OUTPUT:

Enter the 1st number:52

Enter the 2nd number:41

52 is the greatest number


Experiment 2(B) Number is odd or even

CODE:

a=int(input("Enter a number:"))

if(a%2==0):

print(a,"is an even number")

else:

print(a,"is an odd number")

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

print("The factorial of",n,"is:",fact)

OUTPUT:

Enter a number:5

The factorial of 5 is: 120


Experiment 3(B) Print numbers from 1 to n

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:

n=input("Enter the name:")

a=input("Enter the address:")

m=input("Enter the month of billing:")

y=input("Enter the year of billing:")

u=int(input("Enter the units of electricity consumed:"))

if(u<0):

print("Name:",n,"\nAddress:",a,"\nMonth of billing:",m,"\nYear of billing:",y,"\nAmount to be paid


is:300/-")

elif(u>0 and u<=100):

print("Name:",n,"\nAddress:",a,"\nMonth of billing:",m,"\nYear of billing:",y,"\nAmount to be paid


is:",u*10)

elif(u>100 and u<=500):

print("Name:",n,"\nAddress:",a,"\nMonth of billing:",m,"\nYear of billing:",y,"\nAmount to be paid


is:",u*20)

elif(u>500 and u<=1000):

print("Name:",n,"\nAddress:",a,"\nMonth of billing:",m,"\nYear of billing:",y,"\nAmount to be paid


is:",u*30)

else:

print("Name:",n,"\nAddress:",a,"\nMonth of billing:",m,"\nYear of billing:",y,"\nAmount to be paid


is:",u*50)
OUTPUT:

Enter the name:ABD

Enter the address:127/33 QWERT Street,chennai-00

Enter the month of billing:December

Enter the year of billing:2023

Enter the units of electricity consumed:250

Name: ABD

Address: 127/33 QWERT Street,chennai-00

Month of billing: December

Year of billing: 2023

Amount to be paid is: 5000


Experiment 4(B) Retail shop billing

CODE:

n=int(input("Enter the number of items:"))

i=0

total=0

itemlist=[]

qtylist=[]

ratelist=[]

while(i<n):

item=input("Enter the item name:")

qty=int(input("Enter the quantity:"))

rate=int(input("Enter the rate of the item:"))

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:

Enter the number of items:2

Enter the item name:Apple

Enter the quantity:5

Enter the rate of the item:25

Enter the item name:Chips

Enter the quantity:10

Enter the rate of the item:10

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=eval(input("Enter the list:"))

i=int(input("Enter the value i:"))

l=l[i:]+l[:i]

print(l)

OUTPUT:

Enter the list:[10,11,12,13,14,15,16,17]

Enter the value i:4

[14, 15, 16, 17, 10, 11, 12, 13]


Experiment 5(B) Distance between two points

CODE:

x1=int(input("Enter the point X1:"))

x2=int(input("Enter the point X2:"))

y1=int(input("Enter the point Y1:"))

y2=int(input("Enter the point Y2:"))

d=(((x2-x1)**2)+((y2-y1)**2))**0.5

print("The distance between the two points is:",d)

OUTPUT:

Enter the point X1:1

Enter the point X2:2

Enter the point Y1:3

Enter the point Y2:4

The distance between the two points is: 1.4142135623730951


Experiment 6(A) Number series

CODE:

n=int(input("Enter the number:"))

s=0

for i in range(n+1):

s+=i

print("the sum till",n,"is:",s)

OUTPUT:

Enter the number:5

the sum till 5 is: 15


Experiment 6(B) Fibbonaci series

CODE:

n=int(input("Enter the number n:"))

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:

Enter the number n:7

0112358
Experiment 6(C) Pascal’s Triangle

CODE:

def fact(n):

if(n==0):

return 1

else:

return(n*fact(n-1))

row=int(input("Enter the number of rows:"))

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:

Enter the number of rows:5

PASCAL TRIANGLE

11

121

1331

14641
Experiment 6(D) Pyramid Pattern

CODE:

n = int(input("Enter the number of rows:"))

i=1

while (i<=n):

print(" "*(n-i),end=" ")

j=0

while (j<i):

print("*",end=" ")

j += 1

i += 1

print()

OUTPUT:

Enter the number of rows:5

**

***

****

*****
Experiment 7(A) Palindrome

CODE:

string=input("Enter a string:")

if(string==string[::-1]):

print(string,'is a palindrome')

else:

print(string,'is a not palindrome')

OUTPUT(1):

Enter a string:madam

madam is a palindrome

OUTPUT(2):

Enter a string:python

python is a not palindrome


Experiment 7(B) Reverse of a string

CODE:

string=input("Enter a string:")

print("The orginal string is-", string)

reverse=''

count=len(string)

while(count>0):

reverse+=string[count-1]

count-=1

print('The reverse string is-',reverse)

OUTPUT:

Enter a string:python

The orginal string is- python

The reverse string is- nohtyp


Experiment 7(C) string count

CODE:

s=input('Enter a string:')

c=0

for i in range(len(s)):

if(s[i]!=' '):

c+=1

print("Total number of characters in the string","",s,"",'is:',c)

OUTPUT:

Enter a string:welcome to python

Total number of characters in the string welcome to python is: 15


Experiment 7(D) Replace a character in a string

CODE:

str=input('Enter a string:')

result=str.replace('o','a')

print('string without replacing is:',str)

print('string with replacing is:',result)

OUTPUT:

Enter a string:opple

string without replacing is: opple

string with replacing is: apple


Experiment 8(A) Factorial using recursion

CODE:

def fact(n):

if(n==1):

return 1

else:

return(n*fact(n-1))

i=int(input('Enter a number:'))

print('The factorial of',i,'is',fact(i))

OUTPUT:

Enter a number:5

The factorial of 5 is 120


Experiment 8(B) Largest element in a list

CODE:

def largest(l):

max=l[0]

for i in l:

if(i>max):

max=i

return max

l=eval(input("Enter a list:"))

print('The largest element present in the list is:',largest(l))

OUTPUT:

Enter a list:[12,14,54,66,37,8]

The largest element present in the list is: 66


Experiment 8(C) Area of shapes

CODE:

def triangle(b,h):

a =0.5*b*h

print('Area of the triangle is:',a)

def circle(r):

a =3.14*r*l

print('Area of the circle is:',a)

def rectangle (l, b):

a =l*b

print('Area of rectangle is:',a)

def cone(r,l):

a=3.14*r*(r+l)

print('Area of the right cone is:',a)

def cylinder(h,r):

a=2*3.14*r*(h+r)

print('Area of the cylinder is:',a)

print('1.Triangle \n2.Circle\n3.Rectangle\n4.Cone\n5.Cylinder')

i=int(input('Enter your choice:'))

if (i==1):

b=int(input('Enter the base:'))

h=int(input('Enter the height:'))

triangle(b,h)
elif(i==2):

r=int(input('Enter the radius:'))

circle(r)

elif(i==3):

l=int(input('Enter the length:'))

b=int(input('Enter the breadth:'))

rectangle(l,b)

elif(i==4):

r=int(input('Enter the radius:'))

l=int(input('Enter the slant height:'))

cone(r,l)

elif(i==5):

h=int(input('Enter the height:'))

r=int(input('Enter the radius:'))

cylinder(h,r)

else:

print('#####Invalid input#####')

OUTPUT(1):

1. Triangle

2.Circle

3. Rectangle

4.Cone

5.Cylinder
Enter your choice:1

Enter the base:5

Enter the height:10

Area of the triangle is: 25.0

OUTPUT(2):

1.Triangle

2.Circle

3.Rectangle

4.Cone

5.Cylinder

Enter your choice:4

Enter the radius:10

Enter the slant height:30

Area of the right cone is: 1256.0


Experiment 9(A) Materials of a building using tuples

CODE:

cost=(100,500,200,800)

qty=[0,0,0,0]

c=1

i=0

while (c!=0):

c=int(input(" enter your choice\n\t1.change the quantity of wood\n\t2.change the quantity of


steel\n\t3.change the quantity of concrete\n\t4.change the quantity of paint\n\t5.display
elements\n\t6.calculate total cost \n\t0.for exit\n\t"))

if(c>0 and c<5):

x=int(input("enter quantity:"))

qty[c-1]=x

elif(c==5):

print("wood cost=",cost[0],"quantity=",qty[0],

"\n steel cost=",cost[1],"quantity=",qty[1],

"In concrete cost=", cost[2],"quantity=",qty[2],

"\n paint cost=",cost[3],"quantity=",qty[3])

elif(c==6):

total=0

while(i<4):

total=total+cost[i]*qty[i]

i=i+1

print("total cost of combination=",total)

elif(c!=0):
print("invalid choice")

OUTPUT:

enter your choice

1.change the quantity of wood

2.change the quantity of steel

3.change the quantity of concrete

4.change the quantity of paint

5.display elements

6.calculate total cost

0.for exit

enter quantity:200

enter your choice

1.change the quantity of wood

2.change the quantity of steel

3.change the quantity of concrete

4.change the quantity of paint

5.display elements

6.calculate total cost

0.for exit

enter quantity:450

enter your choice

1.change the quantity of wood


2.change the quantity of steel

3.change the quantity of concrete

4.change the quantity of paint

5.display elements

6.calculate total cost

0.for exit

wood cost= 100 quantity= 200

steel cost= 500 quantity= 0 In concrete cost= 200 quantity= 450

paint cost= 800 quantity= 0

enter your choice

1.change the quantity of wood

2.change the quantity of steel

3.change the quantity of concrete

4.change the quantity of paint

5.display elements

6.calculate total cost

0.for exit

total cost of combination= 110000

enter your choice

1.change the quantity of wood

2.change the quantity of steel

3.change the quantity of concrete


4.change the quantity of paint

5.display elements

6.calculate total cost

0.for exit

0
Experiment 9(B) Binary search

CODE:

l=eval(input('Enter a sorted list:'))

f=0

c=0

ele=int(input('Enter the element to be searched:'))

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):

print(ele, 'found at position', m+1)

else:

print(ele, 'not found')

OUTPUT:

Enter a sorted list:[13,15,19,22,32,47,59,62]

Enter the element to be searched:47

47 found at position 6
Experiment 9(C) Bubble sort

CODE:

def bubbleSort(arr):

n = len(arr)

for i in range(n-1):

for j in range(0, n-i-1):

if arr[j] > arr[j + 1]:

arr[j], arr[j + 1] = arr[j + 1], arr[j]

arr = [64, 34, 25, 12, 22, 11, 90]

bubbleSort(arr)

print ("Sorted array is:")

for i in range(len(arr)):

print ("%d" % arr[i],end=" ")

OUTPUT:

Sorted array is:

11 12 22 25 34 64 90
Experiment 10 Sets and Dictionary

CODE:

comps=["Spark Plug","Piston","Connecting Rod","Piston Ring","Driving Shaft"]

comps_desc={"Spark Plug":"It comprises of combustion chamber valve train and spark


plugs","Piston":"Piston is a cylindrical plug which is used for moving up and down","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 ","Driving Shaft":"Power is transmitted to the
differential through a propeller shaft"}

for comps in comps_desc:

print(comps+":"+comps_desc[comps]+"\n")

OUTPUT:

Spark Plug:It comprises of combustion chamber valve train and spark plugs

Piston:Piston is a cylindrical plug which is used for moving up and down

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

Driving Shaft:Power is transmitted to the differential through a propeller shaft


Experiment 11(A) NumPy

CODE:

import numpy as np

l=[1,7,0,2,5,6]

array1=np.array(l)

print('The original list is:',l)

print('array 1 is',array1)

array2=np.asarray(array1)

print('array 2 is',array2)

array2[3]=23

print()

print('After chances-')

print('The original list is:',l)

print('array 1 is',array1)

print('array 2 is',array2)

OUTPUT:

The original list is: [1, 7, 0, 2, 5, 6]

array 1 is [1 7 0 2 5 6]

array 2 is [1 7 0 2 5 6]

After chances-

The original list is: [1, 7, 0, 2, 5, 6]

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

n=int(input("Enter the number of students in the class:"))

s=pd.DataFrame(columns=['Maths','Physics','Chemistry','Python','English'],index=range(1,n+1))

for i in ['Maths','Physics','Chemistry','Python','English']:

print("Enter the marks for ",i,"subjects:-")

for j in range(1,n+1):

print("For Rollno.",j)

s[i][j]=int(input("\t:"))

print(s)

OUTPUT:

Enter the number of students in the class:3

Enter the marks for Maths subjects:-

For Rollno. 1

:67

For Rollno. 2

:78

For Rollno. 3

:45

Enter the marks for Physics subjects:-

For Rollno. 1

:77

For Rollno. 2
:65

For Rollno. 3

:89

Enter the marks for Chemistry subjects:-

For Rollno. 1

:44

For Rollno. 2

:90

For Rollno. 3

:50

Enter the marks for Python subjects:-

For Rollno. 1

:63

For Rollno. 2

:81

For Rollno. 3

:49

Enter the marks for English subjects:-

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:

import matplotlib.pyplot as plt

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:

from scipy.integrate import quad

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

for line in file:

words=line.split()

num_words=len(words)

print("The no. of words=",num_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):

with open(filename,"r") as infile:

words=infile.read().split()

max_len=len(max(words,key=len))

return [word for word in words if len(word)==max_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:

x=int(input("Enter the 1st number:"))

y=int(input("Enter the 2nd number:"))

print("The result is:",x/y)

except ZeroDivisionError:

print("Cannot divide by zero")

except ValueError:

print("Enter an integer value")

OUTPUT:

Enter the 1st number:77

Enter the 2nd number:0

Cannot divide by zero


Experiment 13(B) Voting Eligibility

CODE:

try:

age=int(input("Enter your age:"))

if(age>18):

print("Eligible to vote")

else:

print("Not eligible to vote")

except ValueError:

print("Age must be in valid number")

except IOError:

print("Enter the correct value")

except:

print("An Error occured")

OUTPUT:

Enter your age:shihbi

Age must be in valid number


Experiment 13(C) Marks Validation

CODE:

try:

marks=int(input("Enter the student:"))

if(marks>50):

print("pass")

else:

print("fail")

except ValueError:

print("Marks must be in valid number")

except IOError:

print("Enter correct value")

except:

print("An Error occured")

OUTPUT:

Enter the student:50

Fail
Experiment 14(A) Simulate bouncing ball using Pygame

CODE:

import pygame, sys, time, random


from pygame.locals import *
from time import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Bounce")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
info = pygame.display.Info()
sw = info.current_w
sh = info.current_h
y= 0
yy= sh
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
sleep(.006)
y+= 1
if y>sh:
pygame.draw.circle(windowSurface, GREEN , (250,yy), 13, 0)
sleep(.0001)
yy +=-1
if yy<-.00001:
y=0
y+= 1
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

OUTPUT:

You might also like