Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

23UPCS35 - Python Programming Lab

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

23UPCS35 - Python Programming Lab

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

PURATCHI THALAIVAR DR.

MGR
GOVERNMENT ARTS & SCIENCE
COLLEGE MADHANUR

DEPARTMENT OF COMPUTER SCIENCE


B.Sc., COMPUTER SCIENCE

PRACTICAL RECORD
2024-2025

Reg. No. :…………………………..….Name: ……..…..


………………………………………
Sub. Code:…………………………………………………………………………...…………………
Sub. Title:………………………………………………………………………………………………

Class :………………………………………………………………………………………………
PURATCHI THALAIVAR DR. MGR
GOVERNMENT ARTS & SCIENCE
COLLEGE MADHANUR

DEPARTMENT OF COMPUTER SCIENCE


B.Sc., COMPUTER SCIENCE

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 …………………....

Staff In-charge Head of the Department

Submitted for the Practical Examination held on ………………………

Examiners : 1.

2.
CONTENTS

S.No. Date Exercise Page No.


Variables, Constants, I/O Statements
a. Area and Circumference of Circle
1
b. Biggest of Two Numbers
c. Number Conversion
Operators
a. Relational Operators
2
b. Biggest of Three Numbers
c. Salary Computation
Conditional / Loop Statements
a. Finding Leap Year
b. Sum of digits of a Number
3
c. Fibonacci Series
d. Prime Number Checking
e. Armstrong Number Checking
Functions
a. Default Arguments
4
b. Variable Length Arguments
c. Keyword Arguments
Recursion
5 a. Factorial of a Number
b. GCD of Two Numbers
Arrays
a. Sum of Elements in an Array
6
b. Biggest /Smallest Element in an
Array
c. Matrix Addition
S.No. Date Exercise Page No.
Strings
a. Palindrome Checking
7 b. String Comparison
c. Counting Vowels and Consonants
Modules
8 d. Module with One Function
e. Module with Multiple Functions
Lists
9
a. Printing Odd/Even Numbers in a List
Tuples
10
a. Sum of Elements in a Tuple
Dictionaries
11
a. Finding Value for a Key
File Handling
12 a. File Handling Functions
b. Copying Contents of a File to another
# Area and Circumference of a Circle
r=float(input("Enter radius of a Circle : "))
a=3.14*r*r
c=2*3.14*r
print("Area = ",a)
print("Circumference =",c)

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)

a = float(input("Enter Radius : "))


print("Area of Circle : ")
area(a)
print("Area of Circle (default argument) : ")
area()

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)

x=int(input("Enter first value : "))


y=int(input("Enter second value : "))
print("Normal Function call : ")
function(x,y)
print("Keyword Arguments : ")
function(a=y,b=x)

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)

n=int(input("Enter a Number : "))


print("Factorial of Given Number = ",fact(n))

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)

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


b=int(input("Enter the second number : "))
print("GCD = ",gcd(a,b))

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)

print("Addition Matrix : ")


for i in range(0,n):
for j in range(0,n):
print (c[i][j], end="\t")
print()
Output :

Enter size of the Array : 3


Enter the First Array :
1
2
3
4
5
6
7
8
9
Enter the Second Array :
9
8
7
6
5
4
3
2
1

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

# Main program to use module

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

# Main program to use module


from arith import *
a = int(input("Enter First Number : "))
b = int(input("Enter Second Number : "))
print(f"Addition = {add(a,b)}")
print(f"Subtraction = {sub(a,b)}")
print(f"Multiplication = {mul(a,b)}")
print(f"Division = {div(a,b)}")

Output :

Enter First Number : 20


Enter Second Number : 10
Addition = 30
Subtraction = 10
Multiplication = 200
Division = 2.0
# Printing Odd and Even numbers from a list
n=int(input("Enter the size of the List : "))
print("Enter the List Numbers : ")
l=[]
for i in range(n):
x=int(input())
l.append(x)
print("Given List of Numbers :",l)
print("Odd Numbers in the List : ")
for i in l :
if (i % 2 == 1) :
print (i)
print("Even Numbers in the List : ")
for i in l :
if (i % 2 == 0) :
print (i)

Output :

Enter the size of the List :


7
Enter the List Numbers :
23
46
109
-28
54
9
77
Given List of Numbers :
[23, 46, 109, -28, 54, 9, 77]
Odd Numbers in the List :
23
109
9
77
Even Numbers in the List :
46
-28
54
# Sum of Elements in a Tuple
n=int(input("Enter number of Marks : "))
l=[]
for i in range(n) :
x=int(input("Enter the Marks : "))
l.append(x)
t=tuple(l)
print("Given Tuple of marks : ",t)
sum=0
for i in range(len(t)) :
sum+=t[i]
print ("Total Marks : ",sum)

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 :

Enter the number of Students : 5


Enter the Register Number : 1120
Enter the Marks obtained : 45
Enter the Register Number : 1121
Enter the Marks obtained : 57
Enter the Register Number : 1122
Enter the Marks obtained : 73
Enter the Register Number : 1123
Enter the Marks obtained : 35
Enter the Register Number : 1124
Enter the Marks obtained : 69
Enter the Register Number to find : 1123

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

#open and read the file after the appending:


f = open("sample.txt", "r")
print(f.read())

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 :

Enter the file name to read : sample.txt

india is my country
pack my box with five dozen liquor jugs

You might also like