St. Paul'S P.U.College of Science & Commerce
St. Paul'S P.U.College of Science & Commerce
COLLEGE OF
SCIENCE & COMMERCE
( CO-EDUCATION )
CAMP -BELAGAVI
Computer Science
Certificate
Examiner
Internal:_______________________
External:_______________________
…………………..
CONTENTS
Prog Date Sign
No ProgramName
PART-A
01 19/7/24 Write a program to swap two numbers using a third variable.
02 8/7/24 Write a program to enter two integers and perform all arithmetic
operations on them.
03 8/7/24 Write a Python program to accept length and width of a rectangle
and compute its perimeter and area.
08 6/9/24 Write a program to find the grade of a student when grades are
allocated as given in the table below.
Percentage Of Grade
marks
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
50% to 60% E
PROGRAM 1:
print("Value of x:", x)
print("Value of y:", y)
OUTPUT:
Enter the first value 34
Enter the second value 65
Values of variables before swapping
Value of x: 34
Value of y: 65
Values of variables after swapping
Value of x: 65
Value of y: 34
ALGORITHM:
Step 1: Read two numbers into variables x and y.
Step 2: Store the value of x in a temporary variable temp.
Step 3: Assign the value of y to x.
Step 4: Assign the value of temp (original x) to y.
Step 5: Display the swapped values of x and y.
Step 6: Stop.
Python Lab
PROGRAM 2:
print("Addition: ",num1+num2)
print("Subtraction: ",num1-num2)
print("Multiplication: ",num1*num2)
print("Division: ",num1/num2)
OUTPUT:
Enter first number: 34
Enter second number: 66
Printing the result for all arithmetic operations:-
Addition: 100
Subtraction: -32
Multiplication: 2244
Division: 0.5151515151515151
Modulus: 34
ALGORITHM:
Step 1: Start.
Step 2: Input the first number num1
Step 3: Input the second number num2.
Step 4: Perform num1+num2 and display the result.
Step 5: Perform num1-num2 and display the result.
Step 6: Perform num1*num2 and display the result.
Step 7: Perform num1/num2 and display the result.
Step 8: Perform num1%num2 and display the result.
Step 9: Stop.
Python Lab
PROGRAM 3:
print(‘AREA=’, area)
print(‘PERIMETER=’, perim)
OUTPUT
Enter length: 4
Enter width: 5
AREA=20
PERIMETER=18
ALGORITHM:
Step 1: Read the length and width of the rectangle in length and width.
Step 2: Calculate the area using the formula area = length * width.
Step 3: Display the area.
Step 4: Calculate the perimeter using the formula perimeter = 2 * (length + width).
Step 5: Display the perimeter.
Step 6: Stop.
Python Lab
PROGRAM 4:
SI=(P*R*T)/100
print('PRINCIPLE AMOUNT=', P)
print('RATE OF INTEREST=', R)
print('TIME=', T)
OUTPUT
Enter principle amount: 10000.00
Enter rate: 2.5
Enter time:1.0
PRINCIPLE AMOUNT=10000.00
RATE OF INTEREST=2.5
TIME=1.0
SIMPLE INTEREST= 250.00
AMOUNT PAYABLE=10250.00
ALGORITHM
Step 1: Read the principal amount into P.
Step 2: Read the rate of interest into R.
Step 3: Read the time in years into T.
Step 4: Calculate the Simple Interest (SI) using the formula SI = (P * R * T) / 100.
Step 5: Display the principal amount P, rate of interest R, and time T.
Step 6: Display the calculated simple interest SI.
Step 7: Calculate the total amount payable as AMOUNT = P + SI.
Step 8: Display the total amount payable AMOUNT.
Step 9: Stop.
Python Lab
PROGRAM 5:
OUPUT
enter first number:12
enter second number:18
enter third number:11
18 is the largest
ALGORITHM
Step 1: Read the first number into a.
Step 2: Read the second number into b.
Step 3: Read the third number into c.
Step 4: If a > b, then go to Step 5; otherwise, go to Step 7.
Step 5: If a > c, display a is the largest; otherwise, display c is the largest.
Step 6: Stop.
Step 7: If b > c, display b is the largest; otherwise, display c is the largest.
Step 8: Stop.
Python Lab
PROGRAM 6:
Write a program that takes the name and age of the user as input
and displays a message whether the user is eligible to apply for a
driving license or not. (the eligible age is 18 years).
input("enter name:")
x=int(input("enter age:"))
if(x>=18):
print(“eligible for license”)
else:
print(“ not eligible for license")
OUPUT
enter name:naresh
enter age:23
23 eligible for license
enter name:aarya
enter age:4
4 not eligible for license
ALGORITHM:
PROGRAM 7:
OUPUT
enter the number:10
enter the number:11
enter the number:5
enter the number:3
enter the number:2
the minimum number is 2
the maximum number is 11
ALGORITHM:
Step 1: Initialize minimum = 0 and maximum = 0.
Step 2: Start a loop for a in the range 0 to 4.
Step 3: Read a number into x.
Step 4: If a == 0, set minimum = x and maximum = x.
Step 5: If x < minimum, set minimum = x.
Step 6: If x > maximum, set maximum = x.
Step 7: End the loop after 5 iterations.
Step 8: Display the minimum number.
Step 9: Display the maximum number.
Step 10: Stop.
Python Lab
PROGRAM 8:
total=(sub1+sub2+sub3+sub4+sub5+sub6)
print("TOTAL=",total)
perc=total/6
print("PERCENTAGE=",perc)
if(perc>=90):
print("Grade: A")
else:
print("Grade: E")
Python Lab
OUPUT
enter marks of the first subject: 45
enter marks of the second subject: 55
enter marks of the third subject: 65
enter marks of the fourth subject: 75
enter marks of the fifth subject: 85
enter marks of the sixth subject: 95
TOTAL= 420
PERCENTAGE= 70.0
Grade: C
ALGORITHM
Step 1: Read marks of six subjects into sub1, sub2, sub3, sub4, sub5, and sub6.
Step 2: Calculate the total marks as total = sub1 + sub2 + sub3 + sub4 + sub5 + sub6.
Step 3: Display the total marks.
Step 4: Calculate the percentage as perc = total / 6.
Step 5: Display the percentage.
Step 6: If perc >= 90, display "Grade: A".
Step 7: Else if perc >= 80, display "Grade: B".
Step 8: Else if perc >= 70, display "Grade: C".
Step 9: Else if perc >= 60, display "Grade: D".
Step 10: Else, display "Grade: E".
Step 11: Stop.
Python Lab
PROGRAM 9:
OUTPUT
enter number to print the tables:4
4x1=4
4x2=8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
ALGORITHM
PROGRAM 10:
OUTPUT
enter number:123456
21
ALGORITHM
PROGRAM 11 :
n=int(input("enter number:"))
temp=n
rev=0
while(n>0):
digit=n%10
rev=rev*10+digit
n=n//10
if(temp==rev):
print("the number is a palindrome!")
else:
print("the number is not a palindrome!")
OUTPUT 1
enter number:1234
the number is not a palindrome!
OUTPUT 2
enter number:1221
the number is a palindrome!
ALGORITHM
PROGRAM 12 :
n=5
for i in range(n,0,-1):
for j in range(1,i+1):
print(j,end="")
print("\r")
OUTPUT
12345
1234
123
12
1
ALGORITHM
Step 1: Initialize n = 5.
Step 2: Loop i from n to 1.
Step 3: For each i, loop j from 1 to i.
Step 4: Print j with space.
Step 5: Move to the next line after each inner loop.
Step 6: Stop.
Python Lab
PART - B
Python Lab
PROGRAM 13:
Write a program that uses a user defined function that accepts name
and gender (as M for Male, F for Female) and prefixes Mr./Ms.
based on the gender.
OUTPUT 1
enetr your name:naresh
enter your gender: M for Male, and F for female: M
Hello, Mr. naresh
OUTPUT 2
enetr your name:aarya
enter your gender: M for Male, and F for female: f
Hello, Ms. Aarya
OUTPUT 3
enetr your name:xyz
enter your gender: M for Male, and F for female:b
Please enter only M or F in gender
ALGORITHM
Step 8: Stop.
PROGRAM 14:
import math
def equationroots(a,b,c):
#calculating discriminant using formula
disc=b*b-4*a*c
sqrt_val=math.sqrt(abs(disc))
#checking condition for discriminant
if disc>0:
print("real and different roots")
print((-b+sqrt_val)/(2*a))
print((-b-sqrt_val)/(2*a))
elif disc==0:
print("real and equal roots")
print(-b/(2*a))
print(-b/(2*a))
else:
print("complex roots")
print(-b/(2*a),”+I”,sqrt_val)
print(-b/(2*a),”-I”,sqrt_val)
a=int(input("enter first coefficient:"))
b=int(input("enter second coefficient:"))
c=int(input("enter third coefficient:"))
if a==0:
print("input correct coefficient for quadratic equation")
else:
equationroots(a,b,c)
OUTPUT 1
enter first coefficient:1
enter second coefficient:2
enter third coefficient:1
real and equal roots
-1.0
-1.0
OUTPUT 2
enter first coefficient:5
enter second coefficient:1
Python Lab
OUTPUT 3
enter first coefficient:1
enter second coefficient:5
enter third coefficient:2
real and different roots
-0.4384471871911697
-4.561552812808831
OUTPUT 4
enter first coefficient:0
enter second coefficient:1
enter third coefficient:2
input correct coefficient for quadratic equation
ALGORITHM
PROGRAM 15:
OUTPUT
enter number 1:100
enter number 2:200
Before swapping numbers:
Number 1: 100
Number 2: 200
After swapping numbers:
Number 1: 200
Number 2: 100
ALGORITHM
PROGRAM 16:
Write a program to input line(s) of text from the user until enter is
pressed. Count the total number of characters in the text (including
white spaces),total number of alphabets, total number of digits, total
number of special symbols and total number of words in the given
text. (Assume that each word is separated by one space).
#Count the total number of alphabets,digit and special characters by looping through
each character and incrementing the respective variable value
totalAlpha = totalDigit = totalSpecial = 0
for a in userInput:
if a.isalpha():
totalAlpha += 1
elif a.isdigit():
totalDigit += 1
else:
totalSpecial += 1
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1
print("Total Words in the Input :",(totalSpace + 1))
OUTPUT 1
Write a sentence: HEY ARE YOU
Total Characters: 11
Total Alphabets: 9
Total Digits: 0
Total Special Characters: 2
Total Words in the Input : 3
Python Lab
OUTPUT2
Write a sentence: COMPUTER SCIENCE PUC-1
Total Characters: 22
Total Alphabets: 18
Total Digits: 1
Total Special Characters: 3
Total Words in the Input : 3
ALGORITHM
Step 1: INPUT a sentence in userInput.
Step 2: SET totalAlpha = 0, totalDigit = 0, totalSpecial = 0, totalSpace = 0.
Step 3: CALCULATE totalChar = len(userInput) and PRINT it.
Step 4: FOR each character in userInput:
IF alphabet, INCREMENT totalAlpha.
ELSE IF digit, INCREMENT totalDigit.
ELSE IF space, INCREMENT totalSpace.
ELSE, INCREMENT totalSpecial.
Step 5: PRINT totalAlpha, totalDigit, totalSpecial.
Step 6: CALCULATE total words as totalSpace + 1 and PRINT it.
Step 7: STOP.
Python Lab
PROGRAM17:
Write a user defined function to convert a string with more than one
word into title case string where string is passed as parameter. (Title
case means that the first letter of each word is capitalised)
def convertToTitle(string):
titleString = string.title()
print('The input string in title case is:',titleString)
userInput = input("Write a sentence: ")
#Counting the number of space to get the number of words
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1
#If the string is already in title case
if(userInput.istitle()):
print("The String is already in title case")
#If the string is not in title case and consists of more than one word
elif(totalSpace > 0):
convertToTitle(userInput)
#If the string is of one word only
else:
print("The String is of one word only")
OUTPUT 1
Write a sentence: good evening!
The input string in title case is: Good Evening!
OUTPUT 2
Write a sentence: Good Morning
The String is already in title case
OUTPUT 3
Write a sentence: science
The String is of one word only
ALGORITHM
Step 1: INPUT a string in userInput.
Step 2: SET totalSpace = 0.
Step 3: FOR each character in userInput, IF it is a space, INCREMENT totalSpace.
Step 4: IF the string is already in title case
PRINT
Step 5: ELSE IF totalSpace > 0
PRINT the result.
Step 6: ELSE
PRINT "The String is of one word only".
Python Lab
Step 7: STOP.
PROGRAM 18:
OUTPUT
Enter a sentence: WELCOME TO THE WORLD OF COMPUTERS
The new sentence is: WELCOME-TO-THE-WORLD-OF-COMPUTERS
ALGORITHM
Step 1: INPUT a sentence
Step 2: REPLACE spaces and STORE the result in RESULT.
Step 3: PRINT AND store in RESULT.
Step 4: STOP.
Python Lab
PROGRAM 19:
Write a program to find the number of times an element occurs in the list.
#defining a list
list1 = [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
#printing the list for the user
print("The list is:",list1)
#asking the element to count
inp = int(input("Which element occurrence would you like to count? "))
#using the count function
count = list1.count(inp)
#printing the output
print("The count of element",inp,"in the list is:",count)
OUTPUT 1
The list is: [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
which element occurrence would you like to count?
10 The count of element 10 in the list is: 2
OUTPUT 2
The list is: [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
Which element occurrence would you like to count?
50 The count of element 50 in the list is: 3
ALGORITHM
Step 1: DEFINE a list list1 with elements.
Step 2: PRINT the list
Step 3: INPUT the element to count and store in inp.
Step 4: USE the count function to find the occurrences of inp in list1 and STORE the
result in count.
Step 5: PRINT the count of the element.
Step 6: STOP.
Python Lab
PROGRAM 20:
Write a function that returns the largest element of the list passed as
parameter using max() function of the list.
OUTPUT
The elements of the list [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
The largest number of the list: 60
ALGORITHM
Step 1: DEFINE list list1 with elements.
Step 2: PRINT the elements of the list.
Step 3: FIND the largest number in the list using the max() function and STORE it in
max_num.
Step 4: PRINT the largest max_num.
Step 5: STOP.
Python Lab
PROGRAM 21:
def removeDup(list1):
length = len(list1)
newList = []
for a in range(length):
#Checking if an element is not in the new List .This will reject duplicate values
if list1[a] not in newList:
newList.append(list1[a])
return newList
list1 = []
#Asking for number of elements to be added in the list
inp = int(input("How many elements do you want to add in the list? "))
#Taking the input from user
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
print("The list entered is:",list1)
print("The list without any duplicate element is:",removeDup(list1))
OUTPUT
How many elements do you want to add in the list? 8
Enter the elements: 1
Enter the elements: 2
Enter the elements: 1
Enter the elements: 3
Enter the elements: 4
Enter the elements: 3
nter the elements: 5
Enter the elements: 6
The list entered is: [1, 2, 1, 3, 4, 3, 5, 6]
The list without any duplicate element is: [1, 2, 3, 4, 5, 6]
ALGORITHM
Step 1: INPUT the number of elements to add in the list as inp.
Step 2: FOR i from 0 to inp - 1, REPEAT steps 3 and 4.Step 3: INPUT an element a.
Step 4: APPEND a to list1.
Step 5: PRINT the list list1.
Step 6: SET newList to an empty list.
Step 7: FOR each element in list1, IF the element is NOT in newList, APPEND it to
newList.
Python Lab
emails = tuple()
username = tuple()
domainname = tuple()
#This will split the email id into two parts, username and domain and return a list
spl = emid.split("@")
#assigning returned list first part to username and second part to domain name
username = username + (spl[0],)
domainname = domainname + (spl[1],)
OUTPUT-
How many email ids you want to enter?: 3
> [email protected]
> [email protected]
> [email protected]
The email ids in the tuple are:
Python Lab
ALGORITHM
Step 1: INPUT the number of email IDs n.
Step 2: INITIALIZE three empty tuples: emails, username, and domain name.
Step 3: FOR i from 0 to n-1, REPEAT steps 4 to 7.
Step 4: INPUT an email ID emid.
Step 5: APPEND emid to the emails tuple.
Step 6: SPLIT emid into two parts using @ and STORE the first part in username and
the second part in domain name.
Step 7: APPEND the username part to the username tuple and the domain name part
to the domain name tuple.
Step 8: PRINT the emails tuple.
Step 9: PRINT the username tuple.
Step 10: PRINT the domain name tuple.
Step 11: STOP.
Python Lab
PROGRAM 23:
name = tuple()
#Create empty tuple 'name' to store the values
n = int(input("How many names do you want to enter?: "))
if search in name:
print("The name",search,"is present in the tuple")
else:
print("The name",search,"is not found in the tuple")
OUTPUT1
How many names do you want to enter?: 3
> naresh
> akash
> priya
The names entered in the tuple are:
('naresh', 'akash', 'priya')
Enter the name of the student you want to search? priya
The name priya is present in the tuple
OUTPUT2
How many names do you want to enter?: 3
> naresh
> akash
> priya
The names entered in the tuple are:
('naresh', 'akash', 'priya')
Enter the name of the student you want to search? vidhan
Python Lab
ALGORITHM
Step 1: INPUT the number of names n.
Step 2: INITIALIZE an empty tuple name.
Step 3: FOR i from 0 to n-1, REPEAT steps 4 and 5.
Step 4: INPUT a name num.
Step 5: APPEND num to the name tuple.
Step 6: PRINT the name tuple.
Step 7: INPUT the name to search as search.
Step 8: IF search is present in the name tuple, PRINT that the name is present.
Step 9: ELSE, PRINT that the name is not found.
Step 10: STOP.
Python Lab
PROGRAM 24:
if character in myDict:
myDict[character]+=1
else:
myDict[character]=1
OUTPUT
The input string is: "welcome to cs department"
The dictionary created from characters of the string is:
{'"': 2, 'w': 1, 'e': 4, 'l': 1, 'c': 2, 'o': 2, 'm': 2, ' ': 3, 't': 3, 's': 1, 'd': 1, 'p': 1, 'a': 1, 'r': 1, 'n':
1}
ALGORITHM
Step 1: INPUT a string myStr.
Step 2: INITIALIZE an empty dictionary myDict.
Step 3: FOR each character in myStr, REPEAT steps 4 to 6.
Step 4: IF the character is already in myDict, INCREMENT its value by 1.
Step 5: ELSE, ADD the character as a key in myDict with value 1.
Step 6: CONTINUE the loop.
Step 7: PRINT the dictionary myDict.
Step 8: STOP.
Python Lab
OR
for i in range(n):
print("Enter Details of student No.", i+1)
rno = int(input("Roll No: "))
name = input("Name: ")
marks = int(input("Marks: "))
result[rno] = [name, marks]
print(result)
print(result[student][0])
OUTPUT
Enter number of students: 3
Enter Details of student No. 1
Roll No: 121
Name: amit
Marks: 67
Enter Details of student No. 2
Roll No: 122
Name: sumit
Marks: 78
Enter Details of student No. 3
Roll No: 123
Name: akshay
Marks: 90
{121: ['amit', 67], 122: ['sumit', 78], 123: ['akshay', 90]}
sumit
Akshay
ALGORITHM
Step 1: INPUT n
Step 2: INITIALIZE an empty dictionary result.
Step 3: FOR each student, INPUT rno, name, and marks, and STORE in result.
Step 4: PRINT result.
Step 5: FOR each student, IF marks > 75, PRINT the name.