23UPCS35 - Python Programming Lab
23UPCS35 - Python Programming Lab
MGR
GOVERNMENT ARTS & SCIENCE
COLLEGE MADHANUR
PRACTICAL RECORD
2024-2025
Class :………………………………………………………………………………………………
PURATCHI THALAIVAR DR. MGR
GOVERNMENT ARTS & SCIENCE
COLLEGE MADHANUR
BONAFIDE CERTIFICATE
Certified to be the bonafide record of work done by …….………………………
(Reg. No………..…………….) in the Laboratory of this College and submitted
for the Semester Practical Examination in ………………………………………
during the Academic Year …………………....
Examiners : 1.
2.
CONTENTS
Output :
Enter radius of a Circle : 15.23
Area = 728.3321060000001
Circumference = 95.6444
# Biggest of Two numbers
a=int(input("Enter first number :"))
b=int(input("Enter second number :"))
if (a>b) :
print(str(a)+" is biggest")
else :
print(str(b)+" is biggest")
Output 1:
Enter first number :10
Enter second number :20
20 is biggest
Output 2:
Enter first number :52
Enter second number :23
52 is biggest
# Number conversions
n=int(input("Enter a Number : "))
print("Given Number = ",n)
print("Binary Equivalent = ",bin(n))
print("Octal Equivalent = ",oct(n))
print("Hexadecimal Equivalent = ",hex(n))
Output:
Enter a Number : 15
Given Number = 15
Binary Equivalent = 0b1111
Octal Equivalent = 0o17
Hexadecimal Equivalent = 0xf
# Relational Operators
x=10
y=20
print(x>y)
print(x<=y)
print(x<y)
print(x>=y)
print(x==y)
print(x!=y)
Output :
False
True
True
False
False
True
# Biggest of Three numbers
a=int(input("Enter first number :"))
b=int(input("Enter second number :"))
c=int(input("Enter third number :"))
if (a>b and a>c) :
print(str(a)+" is biggest")
elif (b>a and b>c) :
print(str(b)+" is biggest")
else :
print(str(c)+" is biggest")
Output :
Enter first number :10
Enter second number :20
Enter third number :30
30 is biggest
# Salary Computation
bp=int(input("Enter basic salary : "))
hra=bp*10/100
ta=bp*5/100
pf=bp*10/100
gross=bp+hra+ta
net=gross-pf
print("Gross Salary = {:.2f}".format(gross))
print("Net Salary = {:.2f}".format(net))
Output :
Enter basic salary : 15000
Gross Salary = 17250.00
Net Salary = 15750.00
# Leap year Checking
year = int(input('Enter a year : '))
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
print(f'{year} is a Leap Year')
else:
print(f'{year} is not a Leap Year')
else:
print(f'{year} is a Leap Year')
else:
print(f'{year} is not a Leap Year')
Output 1:
Enter a year : 2014
2014 is not a Leap Year
Output 2:
Enter a year : 2016
2016 is a Leap Year
# Sum of digits of a Number
n = int(input("Enter a Number : "))
sum = 0
while (n > 0):
sum += n % 10
n //= 10
print("Sum of digits =",sum)
Output 1 :
Enter a Number : 1234567890
Sum of digits = 45
Output 2:
Enter a Number : 54897
Sum of digits = 33
# Fibonacci Series
n=int(input("Enter Number of terms : "))
a=0
b=1
print(a)
print(b)
n-=2
while (n>0) :
c=a+b
print(c)
a=b
b=c
n-=1
Output :
Enter Number of terms : 10
0
1
1
2
3
5
8
13
21
34
# Prime Number checking
n=int(input("Enter a Number : "))
f=1
for i in range(2,n) :
if (n%i == 0) :
f=0
if (f) :
print("Given Number is a Prime")
else:
print("Given Number is not a Prime")
Output 1:
Enter a Number : 23
Given Number is a Prime
Output 2:
Enter a Number : 14
Given Number is not a Prime
# Armstrong Number Checking
n = int(input("Enter a Number : "))
sum = 0
m=n
while (n>0) :
t = n%10
sum += t**3
n //= 10
if (sum == m) :
print("Given Number is a Armstrong Number")
else :
print("Given Number is not a Armstrong Number")
Output 1:
Enter a Number : 153
Given Number is a Armstrong Number
Output 2:
Enter a Number : 110
Given Number is not a Armstrong Number
# Default Arguments
def area(r=1) :
print(3.14*r*r)
Output :
Enter Radius : 10.25
Area of Circle :
329.89625
Area of Circle (default argument) :
3.14
# Variable length Arguments
def funct(*a) :
sum=0
for i in a :
sum += i
return sum
print(funct())
print(funct(10))
print(funct(10,20))
print(funct(10,20,30))
print(funct(10,20,30,40))
print(funct(10,20,30,40,50))
Output :
0
10
30
60
100
150
# Keyword Arguments
def function(a,b) :
print(a*a+b)
Output :
Enter first value : 10
Enter second value : 20
Normal Function call :
120
Keyword Arguments :
410
# Factorial using Recursive Function
def fact(x) :
if (x == 1) :
return 1
else :
return x*fact(x-1)
Output :
Enter a Number : 6
Factorial of Given Number = 720
# GCD of two numbers using Recursive Function
def gcd(x,y) :
if (y == 0) :
return x
else :
return gcd (y,x%y)
Output :
Enter the first number : 60
Enter the second number : 48
GCD = 12
# Program to sum the elements of an Array
n=int(input("Enter the Number of elements : "))
a=[]
sum=0
for i in range (0,n) :
x = int(input("Enter a Number : "))
a.append(x)
print("Elements of Array : ")
for i in range (0,n) :
print(a[i])
for i in range(0,n) :
sum+= a.pop()
print("Sum of elements : ",sum)
Output :
Enter the Number of elements : 5
Enter a Number : 10
Enter a Number : 20
Enter a Number : 30
Enter a Number : 40
Enter a Number : 50
Elements of Array :
10
20
30
40
50
Sum of elements : 150
# Biggest and Smallest element in an array
n=int(input("Enter the Number of elements : "))
a=[]
for i in range (0,n) :
x = int(input("Enter a Number : "))
a.append(x)
big=small=a[0]
for i in range (1,n) :
if (big < a[i]) :
big = a[i]
if (small > a[i]) :
small = a[i]
print("Biggest Element : ",big)
print("Smallest Element : ",small)
Output :
Enter the Number of elements : 5
Enter a Number : 56
Enter a Number : -23
Enter a Number : 891
Enter a Number : 5
Enter a Number : -256
Biggest Element : 89
Smallest Element : -256
# Matrix Addition
n=int(input("Enter size of the Array : "))
a=[]
b=[]
c=[]
print("Enter the First Array : ")
for i in range(0,n):
row=[]
for j in range(0,n):
x=int(input())
row.append(x)
a.append(row)
print("Enter the Second Array : ")
for i in range(0,n):
row=[]
for j in range(0,n):
x=int(input())
row.append(x)
b.append(row)
for i in range(0,n):
row=[]
for j in range(0,n): row.append(a[i][j]+b[i]
[j])
c.append(row)
Addition Matrix :
10 10 10
10 10 10
10 10 10
# Program to reverse the string & Palindrome checking
s = input(“Enter a String : “)
rev = “”
for i in s :
rev = i + rev
if (rev == s) :
print (“Given String is a Palindrome”)
else :
print (“Given String is not a Palindrome”)
Output 1 :
Enter a String : madam
Given String is a Palindrome
Output 2 :
Enter a String : india
Given String is not a Palindrome
# String comparison
s1=input("Enter first String : ")
s2=input("Enter second String : ")
if (s1 == s2) :
print("Both Strings are equal")
elif (s1 != s2) :
print("Both Strings are not equal")
if (s1 < s2) :
print("String1 comes before String2 lexicographically")
elif (s1 > s2) :
print("String2 comes before String1 lexicographically")
Output 1 :
Enter first String : india
Enter second String : country
Both Strings are not equal
String2 comes before String1 lexicographically
Output 2:
Enter first String : india
Enter second String : india
Both Strings are equal
# Counting Vowels and Consonants
print("Enter a String : ")
s=input()
vowset="aeiouAEIOU"
v=c=0
for a in s :
if a in vowset :
v+=1
c=len(s)-v
print("Total Vowels : ",v)
print("Total Consonants : ",c)
Output :
Enter a String :
india is my country
Total Vowels : 6
Total Consonants : 13
# Module to find factorial – factmod.py
def fact(x):
f=1
for i in range (1,x+1):
f=f*i
return f
import factmod
n = int(input("Enter a Number to find Factorial : "))
print(f'The Factorial of {n} is {factmod.fact(n)}')
Output :
Enter a Number to find Factorial : 5
The Factorial of 5 is 120
# Module for arithmetical functions – arith.py
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
Output :
Output :
Output :
Enter number of Marks : 5
Enter the Marks : 56
Enter the Marks : 45
Enter the Marks : 70
Enter the Marks : 63
Enter the Marks : 37
Given Tuple of marks : (56, 45, 70, 63, 37)
Total Marks : 271
# Finding the value for the given key using Dictionary
d={}
n=int(input("Enter the number of Students : "))
for i in range(n) :
k = int(input("Enter the Register Number : "))
v = int(input("Enter the Marks obtained : "))
d[k]=v
x=int(input("Enter the Register Number to find : "))
print("Given Data : ")
for i in d :
print (f"{i} => {d[i]}")
if x in d:
print("Mark available : ",d[x])
else :
print("Given Register Number not available")
Output :
Given Data :
1120 => 45
1121 => 57
1122 => 73
1123 => 35
1124 => 69
Mark available : 35
# File Handling Functions
f=open("sample.txt","w")
f.writelines("india is my country. ")
f.close()
f = open("sample.txt", "a")
f.writelines("New text appended.")
f.close()
Output :
india is my country. New text appended.
# Opening an existing file and printing the contents
f=input("Enter the file name to read : ")
with open(f) as x :
l=x.readlines()
for i in l :
print(i)
Output :
india is my country
pack my box with five dozen liquor jugs