Python programs
Python programs
OUTPUT:
Enter the Temperature in Centigrade:
Centigrade=97
centigrade 97 equal to farenheit is 206.6
Enter the Temperature in Farenheit:
Farenheit=206.6
farenheit 206.6 equal to Centigrade is 97.0
1
Program 2:
OUTPUT:
factorial of 5 is 120
2
Program 3:
#Fibonacci Series up to a limit
print('Enter a number to print Fibonacci Series upto that Number')
n = int(input('Enter the Number:'))
t1,t2 = 1,1
print('Fibonacci Series')
print(t1,t2)
i=1
whilei<=n:
t3 = t1+t2
print(t3)
t1,t2 = t2,t3
i = i+1
print('End of the series')
OUTPUT:
3
Program 4:
OUTPUT:
OUTPUT:
5
Program 6:
#Print largest and smallest of items in the list.
lst=[]
print('How many elements ?')
n = int(input())
fori in range(n):
print('Enter Element:')
lst.append(int(input()))
print('The Given List =',lst)
big = lst[0]
small = lst[0]
fori in range(1,n):
iflst[i]>big: big= lst[i]
iflst[i]<small: small=lst[i]
print('Maximum is :',big)
print('Minimum is :',small)
OUTPUT:
How many elements ?
5
Enter Element:
1
Enter Element:
2
Enter Element:
3
Enter Element:
4
Enter Element:
6
5
The Given List = [1, 2, 3, 4, 5]
Maximum is : 5
Minimum is : 1
7
Program:7
OUTPUT:
8
Program: 8
#Addition, Subtraction of Matrices
x = [[1,2,3],[4,5,6],[7,8,9]]
y = [[7,8,9],[4,5,6],[1,2,3]]
a=[[0,0,0],[0,0,0],[0,0,0]]
b= [[0,0,0],[0,0,0],[0,0,0]]
c=[[0,0,0],[0,0,0],[0,0,0]]
fori in range(len(x[0])):
for j in range(len(y[0])):
a[i][j]=x[i][j]+y[i][j]
b[i][j]=x[i][j]-y[i][j]
print("Addition of Matrices=")
for r in a:
print(r)
print()
print("Subtraction of Two Matrices=")
for k in b:
print(k)
OUTPUT:
Addition of Matrices=
[8, 10, 12]
[8, 10, 12]
[8, 10, 12]
Subtraction of Two Matrices=
[-6, -6, -6]
[0, 0, 0]
[6, 6, 6]
9
Program 9:
#Transpose Of a Matrix
x =[[12,7,3],[4,5,6],[7,8,9]]
t = [[0,0,0],[0,0,0],[0,0,0]]
fori in range(len(x[0])):
for j in range(len(x[0])):
t[i][j]=x[j][i]
print("Transpose Matrix is:")
for r in t:
print(r)
OUTPUT:
10
Program 10:
OUTPUT:
Enter the Number of Elements 5
Enter a Number5
Enter a Number45
Enter a Number6
Enter a Number78
Enter a Number90
Second largest Element is 78
11
Program-11
#Counting the number of elements in List
list1 = [10, 20, 30, 40, 50, 40, 40, 60, 70]
l = 40
r = 80
print("The given List")
print(list1)
c=0
fori in list1:
ifi>=l and i<=r:
c = c+1
print("Number of elements in List within a range", c)
12
Program-12
13
Program-13
# A function to test whether a number is even or odd
Def even_odd(num):
If num % 2==0:
Print(num,” is odd”)
Else:
Print(num, “ is odd”)
# call the function
even_odd(12)
even_odd(13)
OUTPUT
12 is even
13 is odd
14
Program – 14
# function to check string is palindrome or not
defisPalindrome(str):
# main function
s =input(‘Enter a String:’)
ans =isPalindrome(s)
if(ans):
print("Given string is Palindrome")
else:
print("Given string is not a Palindrome")
OUTPUT:
Enter a String: Malayalam
Given string is palindrome
15
Program 15
# Program to demonstrate on scope of a variable
a,b = 5,6
Def fun1():
a,b = 1,2
Print(“ a =%d and b = %d”%(a,b)
b= 10
def fun2():
a =25
fun1()
print(“a =%d and b =%d”%(a,b))
b=7
fun1()
print(“a=%d and b =%d”%(a,b))
a = 30
fun2()
print(“a=%d and k = %d”%(a,b))
OUTPUT:
a =1 and b = 2
a= 5 and b =7
a= 1 and b = 2
a = 25 and b =7
a= 30 and b =7
Output:
a=1 and b=2
16
16. Program to find roots of quadratic equation
import cmath
a=float(input('enter a:'))
b=float(input('enter b:'))
c=float(input('enter c:'))
d=(b*b)-(4*a*c)
sol1=(-b-cmath.sqrt(d))/(2*a)
sol2=(-b+cmath.sqrt(d))/(2*a)
print('The solution are{0} and {1}'.format(sol1,sol2))
output:
enter a:8
enter b:5
enter c:99
The solution are(-0.3125-3.503904072602445j) and (-0.3125+3.503904072602445j)
17
17. #Program on break and continue
OUTPUT:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
var = 0
output = []
while var <= 20 :
if var == 15 :
var=var+1
continue
output.append(var)
var=var+1
print(output)
OUTPUT
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20]
18
18. #Program on recursion.
def list_sum_recursion(a_list):
if len(a_list)==0:
return 0
return a_list[0]+list_sum_recursion(a_list[1:len(a_list)])
a_list=[10,20,30,40,50,60,70,80,90]
total=list_sum_recursion(a_list)
print("sum of the elements in a list =",total)
OUTPUT:
sum of the elements in a list = 450
19
19. Read , write and append a file program
20
20. Python Program on Exception
21
21. Display invalid mark exception if mark is greater than 100.
class invalid_marksexception(Exception):
pass
try:
if marks>100 or marks<0:
raise invalid_marksexception()
except invalid_marksexception:
print ("Marks should be in 1 to 100 ")
Enter student marks: 102
Marks should be in 1 to 100
22