Python program
EX.NO :1
AIM:
Temperature conversion
PROGRAM:
print("1.Celcius to
Fahrenheit\n2.Fahrenheit to Celcius")
ch=int(input("Enter the
choice:"))
while(ch!=0):
if(ch==1):
c=float(input("Enter the value of
celcius:"))
f=(c*1.8)+32
print("Fahrenheit value:",f)
else:
f=float(input("Enter the value of
Fahrenheit:"))
c=(f-32)/1.8
print("Celcius value: ",c)
print("1.Celcius to Fahrenheit\n2.Fahrenheit to Celcius\n
0.Exit")
ch=int(input("Enter the choice:"))
OUTPUT:
1.Celcius
to Fahrenheit
2.Fahrenheit
to Celcius
Enter
the choice:1
Enter
the value of celcius:36.7
Fahrenheit
value: 98.06
1.Celcius
to Fahrenheit
2.Fahrenheit
to Celcius
0.Exit
Enter
the choice:2
Enter
the value of Fahrenheit:98.4
Celcius
value: 36.88888888888889
1.Celcius
to Fahrenheit
2.Fahrenheit
to Celcius
0.Exit
Enter
the choice:0
EX.NO :2
AIM:
Area
of shapes using user defined functions.
PROGRAM:
def
circle(r):
return 3.14*r*r
def
square(s):
return s*s
def
rectangle(l,b):
return l*b
def
triangle(b,h):
return b*h*0.5
print("1.Area
of circle\n2.Area of square\n3.Area of rectangle\n4.Area of triangle\n0.Exit
")
ch=int(input("Enter
the choice: "))
while(ch!=0):
if(ch==1):
a=circle(float(input("Radius:")))
print("Area of circle:",a)
elif(ch==2):
a=square(float(input("Side:")))
print("Area of square:",a)
elif(ch==3):
a=rectangle(float(input("Length:")),float(input("Breadth:")))
print("Area of rectangle:",a)
elif(ch==4):
a=triangle(float(input("Base:")),float(input("Height:")))
print("Area of triangle:",a)
print("\n1.Area of circle\n2.Area of
square\n3.Area of rectangle\n4.Area of triangle\n0.Exit ")
ch=int(input("Enter the choice:
"))
OUTPUT:
1.Area
of circle
2.Area
of square
3.Area
of rectangle
4.Area
of triangle
0.Exit
Enter
the choice: 1
Radius:3
Area
of circle: 28.259999999999998
1.Area
of circle
2.Area
of square
3.Area
of rectangle
4.Area
of triangle
0.Exit
Enter
the choice: 2
Side:2
Area
of square: 4.0
1.Area
of circle
2.Area
of square
3.Area
of rectangle
4.Area
of triangle
0.Exit
Enter
the choice: 3
Length:2
Breadth:3
Area
of rectangle: 6.0
1.Area
of circle
2.Area
of square
3.Area
of rectangle
4.Area
of triangle
0.Exit
Enter
the choice: 4
Base:2
Height:3
Area
of triangle: 3.0
1.Area
of circle
2.Area
of square
3.Area
of rectangle
4.Area
of triangle
0.Exit
Enter
the choice: 0
EX.NO :3
AIM:
To generate a Fibonacci series.
PROGRAM:
n=int(input("Enter
the number of terms : "))
a=0
b=1
print("Fibonacci
series upto %d terms:"%n)
for
x in range(0,n):
print(a)
s=a+b
a=b
b=s
OUTPUT:
Enter
the number of terms : 6
Fibonacci
series upto 6 terms:
0
1
1
2
3
5
EX.NO :4
AIM:
To generate a factorial for a given number.
PROGRAM:
n=int(input("Enter
the number: "))
fact=1
for
i in range(1,n+1):
fact=fact*i
print("Factorial
of %d : %d"%(n,fact))
OUTPUT:
Enter
the number: 6
Factorial
of 6 : 720
EX.NO :5
AIM:
Check whether the given number is Armstrong or not.
PROGRAM:
n=int(input("Enter
the number:"))
t=n
arm=0
while(t>0):
i=t%10
arm=arm+pow(i,3)
t//=10
if(arm==n):
print(n," is a armstrong number.")
else:
print(n," is not a armstrong
number.")
OUTPUT 1:
Enter
the number:153
153 is a armstrong number.
OUTPUT 2:
Enter
the number:154
154 is not a armstrong number.
EX.NO :6
AIM:
Check whether the given number is palindrome or not.
PROGRAM:
n=int(input("Enter
the number: "))
t=n
rev=0
while(t>0):
i=t%10
rev=rev*10+i
t//=10
if(rev==n):
print(n," is a palindrome
number.")
else:
print(n," is not a palindrome
number.")
OUTPUT 1:
Enter
the number: 1221
1221 is a palindrome number.
OUTPUT 2:
Enter
the number: 2221
2221 is not a palindrome number.
EX.NO :7
AIM:
Check whether the given number is perfect or not.
PROGRAM:
n=int(input("Enter
the number:"))
t=n
p=0
for
i in range(1,t):
if(t%i==0):
p+=i
if(p==n):
print(n," is a perfect number.")
else:
print(n," is not a perfect
number.")
OUTPUT 1:
Enter
the number:6
6 is a perfect number.
OUTPUT 2:
Enter
the number:12
12 is not a perfect number.
EX.NO :8
AIM:
Sort an array of numbers.
PROGRAM:
n=int(input("Enter
the number of elements:"))
list=[]
for
i in range(0,n):
e=int(input("Enter the number:"))
list.append(e)
print("sorted
array")
for
i in range(0,n):
for j in range(i+1,n):
if(list[i]>list[j]):
t=list[i]
list[i]=list[j]
list[j]=t
print(list[i])
OUTPUT:
Enter
the number of elements:5
Enter
the number:3
Enter
the number:2
Enter
the number:4
Enter
the number:1
Enter
the number:6
sorted
array
1
2
3
4
6
EX.NO :9
AIM:
Sum of series.
PROGRAM:
sum=1
print("Find
e^x value")
n=int(input("Enter
the value of x:"))
def
fact(n):
f=1
for i in range(1,n+1):
f=f*i
return f
for
i in range(1,n+1):
sum+=(pow(n,i)/fact(i))
print("Sum
of series:",sum)
OUTPUT:
Find
e^x value
Enter
the value of x:5
Sum
of series: 91.41666666666667
EX.NO :10
AIM:
Sum of two matrices.
PROGRAM:
def
display(a,m1,n1):
for i in range(m1):
for j in range(n1):
print(a[i][j],end=' ')
print()
m1=int(input("Enter
the number of rows of Matrix A:"))
n1=int(input("Enter
the number of columns of Matrix A:"))
a=[]
b=[]
for
i in range(m1):
t=[]
for j in range(n1):
t.append(float(input("Enter the
value of a%d%d: "%(i+1,j+1))))
a.append(t)
print()
m2=int(input("Enter
the number of rows of Matrix B:"))
n2=int(input("Enter
the number of columns of Matrix B:"))
for
i in range(m2):
t=[]
for j in range(n2):
t.append(float(input("Enter the
value of b%d%d: "%(i+1,j+1))))
b.append(t)
print()
s=[]
if(m1==m2)&(n1==n2):
for i in range(m1):
t=[]
for j in range(n1):
t.append(a[i][j]+b[i][j])
s.append(t)
print("Matrix A:")
display(a,m1,n1)
print("Matrix B:")
display(b,m2,n2)
print("Sum of Matrix A and Matrix
B:")
display(s,m1,n1)
else:
print("Sum of Matrix A and Matrix B is
not possible.")
OUTPUT:
Enter
the number of rows of Matrix A:2
Enter
the number of columns of Matrix A:2
Enter
the value of a11: 1
Enter
the value of a12: 2
Enter
the value of a21: 3
Enter
the value of a22: 4
Enter
the number of rows of Matrix B:2
Enter
the number of columns of Matrix B:2
Enter
the value of b11: 1
Enter
the value of b12: 2
Enter
the value of b21: 3
Enter
the value of b22: 4
Matrix
A:
1.0
2.0
3.0
4.0
Matrix
B:
1.0
2.0
3.0
4.0
Sum
of Matrix A and Matrix B:
2.0
4.0
6.0
8.0
EX.NO :11
AIM:
Product of two matrices.
PROGRAM:
def
display(a,m1,n1):
for i in range(m1):
for j in range(n1):
print(a[i][j],end=' ')
print()
m1=int(input("Enter
the number of rows of Matrix A:"))
n1=int(input("Enter
the number of columns of Matrix A:"))
a=[]
b=[]
for
i in range(m1):
t=[]
for j in range(n1):
t.append(int(input("Enter the
value of a%d%d :"%(i+1,j+1))))
a.append(t)
print()
m2=int(input("Enter
the number of rows of Matrix B:"))
n2=int(input("Enter
the number of columns of Matrix B:"))
for
i in range(m2):
t=[]
for j in range(n2):
t.append(int(input("Enter the
value of b%d%d: "%(i+1,j+1))))
b.append(t)
print()
m=[]
if(m2==n1):
for i in range(m1):
t=[]
for j in range(n2):
temp=0
for k in range(n1):
temp+=a[i][k]*b[k][j]
t.append(temp)
m.append(t)
print("Matrix A:")
display(a,m1,n1)
print("Matrix B:")
display(b,m2,n2)
print("Multiplication of Matrix A and
Matrix B:")
display(m,m1,n2)
else:
print("Multiplication of Matrix A and
Matrix B is not possible.")
OUTPUT:
Enter
the number of rows of Matrix A:2
Enter
the number of columns of Matrix A:2
Enter
the value of a11 :1
Enter
the value of a12 :2
Enter
the value of a21 :3
Enter
the value of a22 :4
Enter
the number of rows of Matrix B:2
Enter
the number of columns of Matrix B:2
Enter
the value of b11: 1
Enter
the value of b12: 2
Enter
the value of b21: 3
Enter
the value of b22: 4
Matrix
A:
1
2
3
4
Matrix
B:
1
2
3
4
Multiplication
of Matrix A and Matrix B:
7
10
15
22
EX.NO :12
AIM:
String function.
PROGRAM:
def
getstr():
str1=input("Enter the String:")
return str1
while
True:
print("\n1.Find length of the
String\n2.To uppercase\n3.To lowercase\n4.compare two strings\n5.Capitalizing
the string\n6.Reverse the String\n7.exit")
ch=int(input("Enter your
choice:"))
if(ch==1):
str1=getstr()
print("length of the
String:",len(str1))
elif(ch==2):
str1=getstr()
print("To
uppercase:",str1.upper())
elif(ch==3):
str1=getstr()
print("To
lowercase:",str1.lower())
elif(ch==4):
str1=getstr()
str2=getstr()
print("compare two
strings:",str1==str2)
elif(ch==5):
str1=getstr()
print("Capitalizing the
string:",str1.capitalize())
elif(ch==6):
str1=getstr()
print("Reverse the
String:",str1[::-1])
else:
break
OUTPUT:
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:1
Enter
the String:saran
length
of the String: 5
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:2
Enter
the String:saran
To
uppercase: SARAN
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:3
Enter
the String:Saran
To
lowercase: saran
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:4
Enter
the String:saran
Enter
the String:Saran
compare
two strings: False
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:4
Enter
the String:Saran
Enter
the String:Saran
compare
two strings: True
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:5
Enter
the String:saran
Capitalizing
the string: Saran
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:6
Enter
the String:saran
Reverse
the String: naras
1.Find
length of the String
2.To
uppercase
3.To
lowercase
4.compare
two strings
5.Capitalizing
the string
6.Reverse
the String
7.exit
Enter
your choice:7
EX.NO :13
AIM:
To swap two elements in list.
PROGRAM:
n=int(input("Enter
the number of elements:"))
list1=[]
for
i in range(0,n):
e=int(input("Enter the number:"))
list1.append(e)
i=int(input("Enter
the first index to swap:"))
j=int(input("Enter
the first index to swap:"))
print("Given
list:",list1)
list1[i],list1[j]=list1[j],list1[i]
print("After
swapping list:",list1)
OUTPUT:
Enter
the number of elements:5
Enter
the number:1
Enter
the number:2
Enter
the number:3
Enter
the number:4
Enter
the number:5
Enter
the first index to swap:3
Enter
the first index to swap:4
Given
list: [1, 2, 3, 4, 5]
After
swapping list: [1, 2, 3, 5, 4]
EX.NO :14
AIM:
Sort python dictionary by key or name.
PROGRAM:
dict1
= {}
n=int(input("ENTER
THE NUMBER OF ELEMENTS:"))
for
i in range(n):
dict1[input("Enter
Name:")]=int(input("Enter Register No.:"))
sorted_dict
= dict(sorted(dict1.items()))
print("Given
dictionary: ",dict1)
print("Sorted
dictionary using name: ",sorted_dict)
OUTPUT:
ENTER
THE NUMBER OF ELEMENTS:5
Enter
Register No.:1
enter
Name:saran
Enter
Register No.:2
enter
Name:arun
Enter
Register No.:3
enter
Name:karan
Enter
Register No.:4
enter
Name:karthi
Enter
Register No.:5
enter
Name:vijay
Given
dictionary: {'saran': 1, 'arun': 2,
'karan': 3, 'karthi': 4, 'vijay': 5}
Sorted
dictionary using name: {'arun': 2,
'karan': 3, 'karthi': 4, 'saran': 1, 'vijay': 5}
EX.NO :15
AIM:
Merging two dictionaries.
PROGRAM:
def
Merge(dict1, dict2):
return(dict2.update(dict1))
dict1
= {}
n=int(input("ENTER
THE NUMBER OF ELEMENTS in Dict1:"))
for
i in range(n):
dict1[input("Enter
Name:")]=int(input("Enter Register No.:"))
dict2
= {}
n=int(input("ENTER
THE NUMBER OF ELEMENTS in Dict2:"))
for
i in range(n):
dict2[input("Enter
Name:")]=int(input("Enter Register No.:"))
print(Merge(dict1,
dict2))
print(dict2)
OUTPUT:
ENTER
THE NUMBER OF ELEMENTS in Dict1:3
Enter
Register No.:1
Enter
Name:saran
Enter
Register No.:2
Enter
Name:maran
Enter
Register No.:3
Enter
Name:karan
ENTER
THE NUMBER OF ELEMENTS in Dict2:2
Enter
Register No.:1
Enter
Name:py
Enter
Register No.:2
Enter
Name:may
None
{'py':
1, 'may': 2, 'saran': 1, 'maran': 2, 'karan': 3}
EX.NO :16
AIM:
To count even and odd numbers in a list and print them
separately.
PROGRAM:
n=int(input("Enter
the number of elements:"))
list=[]
odd,even=0,0
for
i in range(0,n):
e=int(input("Enter the number:"))
list.append(e)
for
i in list:
if(i%2==0):
even+=1
else:
odd+=1
print("Even
numbers count:",even)
print("Odd
numbers count :",odd)
OUTPUT:
Enter
the number of elements:5
Enter
the number:1
Enter
the number:2
Enter
the number:3
Enter
the number:4
Enter
the number:5
Even
numbers count: 2
Odd
numbers count : 3
EX.NO :17
AIM:
Creating CSV file.
PROGRAM:
import
csv
c=int(input("Enter
the number of columns:"))
r=int(input("Enter
the number of data:"))
data=[]
for
i in range(r):
row=[]
print(f"Enter the data of row
{i+1}")
for j in range(c):
d=input(f"Enter the value of
column {j+1}:")
row.append(d)
data.append(row)
filename
=input("Enter the filename without extension:")+'.csv'
with
open(filename, 'w',newline='') as csvfile:
csvwriter= csv.writer(csvfile)
csvwriter.writerows(data)
OUTPUT:
Enter
the number of columns:3
Enter
the number of data:5
Enter
the data of row 1
Enter
the value of column 1:Name
Enter
the value of column 2:Age
Enter
the value of column 3:City
Enter
the data of row 2
Enter
the value of column 1:Saran
Enter
the value of column 2:22
Enter
the value of column 3:Sattur
Enter
the data of row 3
Enter
the value of column 1:Karan
Enter
the value of column 2:23
Enter
the value of column 3:Sivakasi
Enter
the data of row 4
Enter
the value of column 1:Vetri
Enter
the value of column 2:22
Enter
the value of column 3:Madurai
Enter
the data of row 5
Enter
the value of column 1:Kathir
Enter
the value of column 2:21
Enter
the value of column 3:Virudhunagar
Enter
the filename without extension:student
EX.NO :18
AIM:
Display the contents from already created CSV file
student details.
PROGRAM:
import
csv
filename
=input("Enter the filename without extension:")+'.csv'
with
open(filename, 'r') as csvfile:
read=csv.reader(csvfile)
for row in read:
print(row)
OUTPUT:
Enter
the filename without extension:student
['Name',
'Age', 'City']
['Saran',
'22', 'Sattur']
['Karan',
'23', 'Sivakasi']
['Vetri',
'22', 'Madurai']
['Kathir',
'21', 'Virudhunagar']
Comments
Post a Comment