2. PYTHON LAB
2
Table of Contents
FIND THE LARGEST AMONG THREE NUMBERS .................................................................................... 3
FIND MEAN,MEDIAN,MODE FROM GIVEN NUMBER............................................................................ 4
SWAP TWO VARIABLES......................................................................................................................... 5
CHECK ARMSTRONG NUMBER.............................................................................................................. 6
TO FIND THE SQUAR ROOT ................................................................................................................... 7
TO CHECK IF A NUMBER IS ODD OR EVEN ............................................................................................ 8
REVERSE A NUMBER............................................................................................................................. 9
TO CHECK PALINDROME..................................................................................................................... 10
TO CHECK LEAP YEAR.......................................................................................................................... 11
FIND THE AREA OF TRIANGLE ............................................................................................................. 12
NUMBER OF CHARACTER IN THE STRING AND STORE THEM IN A DICTIONARY DATA STRUCTURE.... 13
FIND THE PRIME NUMBER.................................................................................................................. 14
TO FIND LENGTH OF A STRING............................................................................................................ 15
TO FIND THE ADDITION, SUBTRACTION, MULTIPICATION,DIVISION .................................................. 16
FIND THE VALUE OF CHARACTER........................................................................................................ 19
THE DISPLAY CALENDAR ..................................................................................................................... 20
COMPUTE THE POWER OF A NUMBER ............................................................................................... 21
TO CHECK IF A NUMBER IS POSITIVE, NEGATIVE, OR ZERO ................................................................ 22
DISPLAY THE MULTIPLICATION TABLE.............................................................................................. 23
FIND ALL DUPLICATES IN THE LIST ...................................................................................................... 24
Find the factorial of a number ............................................................................................................ 25
ADD TWO MATRICES .......................................................................................................................... 26
COUNT THE NUMBER OF DIGITSPRESENT IN A NUMBER ................................................................... 27
FIND ALL THE UNIQUE ELEMENTS OF A LIST....................................................................................... 28
CONVERT DECIMAL TO BINARY,OCTAL AND HEXADECIMAL............................................................... 29
CONVERT CELSIUS TO FAHRENHEIT.................................................................................................... 30
CONVERT KILOMETERS TO MILES ....................................................................................................... 31
3. PYTHON LAB
3
EX:NO:1
FIND THE LARGEST AMONG THREE NUMBERS
DATE:
SOURCE CODE:
a = int(input("A:"))
b = int(input("B:"))
c = int(input("C:"))
if(a > b and a > b):
print("Largest : ",a)
elif(b > a and b > c):
print("Largest : ",b)
elif(c > a and c > b):
print("Largest : ",c)
OUTPUT:
A:36
B:82
C:91
Largest : 91
4. PYTHON LAB
4
EX:NO:2
FIND MEAN,MEDIAN,MODE FROM GIVEN NUMBER
DATE:
SOURCE CODE:
print("FIND MEAN,MEDIAN,MODE")
print("**********************")
from statistics import mean, median, mode
l = [ 36, 56, 76, 82, 27]
print("MEAN:",mean(l))
print("MEDIAN:",median(l))
print("MODE:",mode(l))
print("**********************")
OUTPUT:
FIND MEAN,MEDIAN,MODE
**********************
MEAN: 55.4
MEDIAN: 56
MODE: 36
5. PYTHON LAB
5
EX:NO:3
SWAP TWO VARIABLES
DATE:
SORCE CODE:
print("SWAP THE VARIABLES")
print("*******************")
a = 6
b = 3
temp = a
a = b
b = temp
print("The Value of a After Swapping:",format(a))
print("The Value of a After Swapping:",format(b))
OUTPUT:
SWAP THE VARIABLES
*******************
The Value of a After Swapping: 3
The Value of a After Swapping: 6
6. PYTHON LAB
6
EX:NO:4
CHECK ARMSTRONG NUMBER
DATE:
SOURCE CODE:
print("CHECK ARMSTRONG NUMBER")
print("_______________________")
num = int(input("Enter the number:"))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"Is An Amstrong...")
else:
print(num,"Is An Amstrong...")
OUTPUT:
CHECK ARMSTRONG NUMBER
_______________________
Enter the number:356
356 Is An Amstrong...
7. PYTHON LAB
7
EX:NO:5
TO FIND THE SQUAR ROOT
DATE:
SOURE CODE:
print(" The Square Root")
print("~~~~~~~~~~~~~~~~~")
import cmath
n = int(input("N:"))
result = cmath.sqrt(n)
print("Square root of",n,"is",result)
OUTPUT:
The Square Root
~~~~~~~~~~~~~~~~
N :6
Square root of 6 is (2.449489742783178+0j)
8. PYTHON LAB
8
EX:NO:6
TO CHECK IF A NUMBER IS ODD OR EVEN
DATE:
SOURCE CODE:
print("ODD ARE EVEN NUMBER")
print("___________________")
num = int(input("Enter the number :"))
if( num % 2) == 0:
print(" {0} is even". format(num))
else:
print(" {0} is odd". format(num))
OUTPUT:
ODD ARE EVEN NUMBER
___________________
Enter the number :36
36 is even
9. PYTHON LAB
9
EX:NO:7
REVERSE A NUMBER
DATE:
SOURCE CODE:
print("REVERSE A NUMBER")
print("*****************")
num = int(input("Enter the number:"))
reversed_num = 0
while num != 0:
digit = num % 10
reversed_num = reversed_num * 10 + digit
num //= 10
print("Reverse the number :" +str(reversed_num))
OUTPUT:
REVERSE A NUMBER
*****************
Enter the number:987654321
Reverse the number :123456789
10. PYTHON LAB
10
EX:NO:8
TO CHECK PALINDROME
DATE:
SOURCE CODE:
print("TO CHECK PALINDROME")
print("*******************")
s = str(input("ENTER THE STRING:"))
def ispalindrome(s):
return s == s [ : :-1]
ans = ispalindrome(s)
if ans:
print(s,"is a palindrom")
else:
print(s,"is a palindrom")
OUTPUT:
TO CHECK PALINDROME
*******************
ENTER THE STRING:AMMA
AMMA is a palindrom
11. PYTHON LAB
11
EX:NO:9
TO CHECK LEAP YEAR
DATE:
SOURCE CODE:
print("CHECK LEAP YEAR")
print("*****************")
year = int(input("ENTER THE YEAR :"))
if (year % 4) == 0:
if (year % 100) == 0:
if(year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is a not a leap year".format(year))
OUTPUT:
CHECK LEAP YEAR
*****************
ENTER THE YEAR :2022
2022 is a not a leap year
12. PYTHON LAB
12
EX:NO:10
FIND THE AREA OF TRIANGLE
DATE:
SOURCE CODE:
print("AREA OF TRIANGLE")
print("*****************")
a = int(input("Enter the Value of A:"))
b = int(input("Enter the Value of B:"))
c = int(input("Enter the Value of C:"))
s = (a + b + c)/2
area = (s*(s-a)*(s-b)*(s-c)**0.5)
print("The Area of the Triangle is:",area)
OUTPUT:
AREA OF TRIANGLE
*****************
Enter the Value of A:6
Enter the Value of B:5
Enter the Value of C:3
The Area of the Triangle is: 28.0
13. PYTHON LAB
13
EX:NO:11
NUMBER OF CHARACTER IN THE STRING AND STORE
THEM IN A DICTIONARY DATA STRUCTURE
DATE:
SOURCE CODE:
str = input("Enter a string:")
dict = {}
for n in str:
keys = dict.keys()
if n in keys:
dict[n] += 1
else:
dict[n] = 1
print(dict)
OUTPUT:
Enter a string : AMMA
{'A': 1}
{'A': 1, 'M': 1}
{'A': 1, 'M': 2}
{'A': 2, 'M': 2}
14. PYTHON LAB
14
EX:NO:12
FIND THE PRIME NUMBER
DATE:
SOURCE CODE:
print("FIND THE PRIME NUMBER")
print("**********************")
x = int(input("Enter any positive number to chect whether it
is prime or not:"))
for i in range(2,x):
if x % 1 == 0:
print(x,"is not a prime number")
break
else:
print(x,"is a prime number:")
OUTPUT:
FIND THE PRIME NUMBER
Enter any positive number to check whether it is prime or
not:35
35 is not a prime number
15. PYTHON LAB
15
EX:NO:13
TO FIND LENGTH OF A STRING
DATE:
SOURCE CODE:
a = str(input("Enter the string:"))
b = 0
for i in a:
b += 1
print(b)
OUTPUT:
Enter the string: INDIA
5
16. PYTHON LAB
16
EX:NO:14
TO FIND THE ADDITION, SUBTRACTION,
MULTIPICATION,DIVISION
DATE:
SOURCE CODE:
def add(a,b):
return a+b
def sub(c,d):
return c-d
def mul(r,f):
return e*f
def div(g,h):
return g/h
print("********************************")
print("1.TO THE PROGRAM ADDITION")
print("2.TO THE PROGRAM SUBTRACTION")
print("3.TO THE PROGRAM MULTIPLICATION")
print("4.TO THE PROGRAM DIVISION")
print("*******************************")
choice = int(input("Enter your choice:"))
17. PYTHON LAB
17
if (choice == 1):
a = int(input("Enter 1st value:"))
b = int(input("enter 2nd value:"))
print(add(a,b))
elif (choice == 2):
c = int(input("Enter 1st value:"))
d = int(input("Enter 2nd value:"))
print(sub(c,d))
elif choice == 3:
e = int(input("Enter 1st value:"))
f = int(input("Enter 2nd value:"))
print(mul(e,f))
elif (choice == 4):
g = int(input("Enter 1st value:"))
h = int(input("Enter 2nd value:"))
print(div(g,h))
else:
print("Wrong choice")
18. PYTHON LAB
18
OUTPUT:
*******************************
1.TO THE PROGRAM ADDITION
2.TO THE PROGRAM SUBTRACTION
3.TO THE PROGRAM MULTIPLICATION
4.TO THE PROGRAM DIVISION
*******************************
Enter your choice:3
Enter 1st value:33
Enter 2nd value:65
2145
19. PYTHON LAB
19
EX:NO:15
FIND THE VALUE OF CHARACTER
DATE:
SOURCE CODE:
print("ASCII VALUE")
print("~~~~~~~~~~~~~")
c = str(input("Enter the Value :"))
print("The ASCII value of " + c + " is ", ord(c))
OUTPUT:
ASCII VALUE
~~~~~~~~~~~~~
Enter the Value :E
The ASCII value of E is 69
20. PYTHON LAB
20
EX:NO:16
THE DISPLAY CALENDAR
DATE:
SOURCE CODE:
import calendar
yy = int(input("Enter year:"))
mm = int(input("Enter month:"))
print(calendar.month(yy,mm))
OUTPUT:
Enter year:2002
Enter month:2
February 2002
Mo Tu We Th Fr Sa Su
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
21. PYTHON LAB
21
EX:NO:17
COMPUTE THE POWER OF A NUMBER
DATE:
SOURCE CODE:
print("THE POWER OF NUMBER")
print("*******************")
base = 3
exponent = 4
result = 1
while exponent != 0:
result *= base
exponent -= 1
print(" ANSWER = ",str (result))
OUTPUT:
THE POWER OF NUMBER
*******************
ANSWER = 81
22. PYTHON LAB
22
EX:NO:18
TO CHECK IF A NUMBER IS POSITIVE, NEGATIVE, OR
ZERO
DATE:
SOURCE CODE:
def Numbercheck(a):
if a < 0:
print("Number Given by you is Negative")
elif a > 0:
print("Number Given by you is Positive")
else:
print("Number Given by you is Zero")
a = float(input("Enter a number as input value:"))
Numbercheck(a)
OUTPUT:
Enter a number as input value:6
Number Given by you is Positive
23. PYTHON LAB
23
EX:NO:19
DISPLAY THE MULTIPLICATION TABLE
DATE:
SOURCE CODE:
num = 12
for i in range(1,11):
print(i,'X',num,'=',num*i)
OUTPUT:
1 X 12 = 12
2 X 12 = 24
3 X 12 = 36
4 X 12 = 48
5 X 12 = 60
6 X 12 = 72
7 X 12 = 84
8 X 12 = 96
9 X 12 = 108
10 X 12 = 120
24. PYTHON LAB
24
EX:NO:20
FIND ALL DUPLICATES IN THE LIST
DATE:
SOURCE CODE:
def findduplicates(list):
b = []
for i in list:
if(i not in b):
b.append(i)
if(b == list):
print("There are not duplicates in the list")
else:
print(" There are duplicates in the list")
OUTPUT:
a = [3,5,6,7,8]
findduplicates(a)
There are no duplicates in the list
25. PYTHON LAB
25
Ex:no:21
Find the factorial of a number
Date:
Source code:
num = int(input("Enter a numbers:"))
factorial = 1
if num < 0:
print("Factorial does not exist for negative numbers")
elif num == 0:
print("The Factorial of a 0 is /")
else:
for i in range(1,num+1):
factorial *= i
print("The Factorial of",num,"is",factorial)
OUTPUT:
Enter a numbers:5
The Factorial of 5 is 120
26. PYTHON LAB
26
EX:NO:22
ADD TWO MATRICES
DATE:
Source code:
x =[[3,5,6],[6,3,1],[7,8,9]]
y=[[6,5,3],[1,6,3],[9,8,7]]
result=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(x)):
for j in range(len(y)):
result[i][j]=x[i][j]+y[i][j]
for r in result:
print(r)
OUTPUT:
[9, 10, 9]
[7, 9, 4]
[16, 16, 16]
27. PYTHON LAB
27
EX:NO:23
COUNT THE NUMBER OF DIGITSPRESENT IN A
NUMBER
DATE:
SOURCE CODE:
num = 3651
count = 0
while num != 0:
num //=10
count += 1
print("Number of Digits:",count)
OUTPUT:
Number of Digits: 4
28. PYTHON LAB
28
EX:NO:24
FIND ALL THE UNIQUE ELEMENTS OF A LIST
DATE:
SOURCE CODE:
def findunique(list):
b = []
for i in list:
if(i not in b):
b.append(i)
if(b == list):
print("There are unique in the list")
else:
print("There are not unique in the list")
OUTPUT:
a = [3,5,24,6,34,89]
findunique(a)
There are unique in the list
b = [3,3,3,6,8,2]
findunique(b)
There are not unique in the list
29. PYTHON LAB
29
EX:NO:25
CONVERT DECIMAL TO BINARY,OCTAL AND
HEXADECIMAL
DATE:
SOURCE CODE:
dec = 344
print("The decimal value of",dec,"is:")
print(bin(dec),"in Binary")
print(oct(dec),"in Octal")
print(hex(dec),"in Hexadecimal")
OUTPUT:
The decimal value of 344 is:
0b101011000 in Binary
0o530 in Octal
0x158 in Hexadecimal
30. PYTHON LAB
30
EX:NO:25(a)
CONVERT CELSIUS TO FAHRENHEIT
DATE:
SOURCE CODE:
celsius = int(input("Enter the temperature in celsius:"))
f = (celsius * 9/5) + 32
print("Tempreature in Farenheit is:",f)
OUTPUT:
Enter the temperature in celsius:6
Tempreature in Farenheit is: 42.8
31. PYTHON LAB
31
EX:NO:25(B)
CONVERT KILOMETERS TO MILES
DATE:
SOURCE CODE:
kilometers = float(input("Enter value in kilometers:"))
conv_fac = 0.621371
miles = kilometers * conv_fac
print( "% 0.2f kilometers is equal to % 0.2f miles" %
(kilometers,miles))
OUTPUT:
Enter value in kilometers:6.00
6.00 kilometers is equal to 3.73 miles