I COMPUTER
PUC SCIENCE
LAB
MANUAL
PART-A
List of programs to be conducted in practical sessions
1. Write a program to swap two numbers using a third variable.
2. Write a program to enter two integers and perform all arithmeticoperations on
them.
3. Write a Python program to accept length and width of a rectangle andcompute
its perimeter and area.
4. Write a Python program to calculate the amount payable if money hasbeen lent
on simple interest. Principal or money lent = P,
Rate of interest = R% per annum and Time = T years.Then
Simple Interest (SI) = (P x R x T)/ 100.
5. Write a Python program to find largest among three numbers.
6. Write a program that takes the name and age of the user as input anddisplays a
message whether the user is eligible to apply for a driving license or not. (the
eligible age is 18 years).
7. Write a program that prints minimum and maximum of five numbersentered by
the user.
8. Write a program to find the grade of a student when grades are allocated as
given in the table below. Percentage of Marks Grade Above90% A 80% to 90% B
70% to 80% C 60% to 70% D Below 60% E Percentage of the marks obtained by
the student is input to the program.
9. Write a function to print the table of a given number. The number hasto be
entered by the user.
10. Write a program to find the sum of digits of an integer number, inputby the
user.
11. Write a program to check whether an input number is a palindromeor not.
12. Write a program to print the following patterns:1 2 3
45
1234
123
12
1
PART – B
13. Write a program that uses a user defined function that accepts ame and gender
(as M for Male, F for Female) and prefixes Mr./Ms. based onthe gender.
14. Write a program that has a user defined function to accept the coefficients of a
quadratic equation in variables and calculates its discriminant. For example: if the
coefficients are stored in the variablesa, b, c then calculate discriminant as b2 -
4ac.Write the appropriate condition to check discriminant on positive, zero and
negative and output appropriate result.
15. Write a program that has a user defined function to accept 2 numbersas
parameters, if number 1 is less than number 2 then numbers are swapped and
returned, i.e., number 2 is returned in place of number1 and number 1 is reformed
in place of number 2, otherwise the same order is returned.
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).
17. Write a user defined function to convert a string with more than oneword into
title case string where string is passed as parameter. (Title case means that the first
letter of each word is capitalized)
18. Write a function that takes a sentence as an input parameter where each word in
the sentence is separated by a space. The function should replace each blank with a
hyphen and then return the modified sentence.
19. Write a program to find the number of times an element occurs inthe list.
20. Write a function that returns the largest element of the list passed asparameter.
21. Write a program to read a list of elements. Modify this list so that itdoes not
contain any duplicate elements, i.e., all elements occurring multiple times in the
list should appear only once. Chapter 10 : Tuples and Dictionaries
22. Write a program to read email IDs of n number of students and storethem in a
tuple. Create two new tuples, one to store only the usernames from the email IDs
and second to store domain names from the email IDs. Print all three tuples at the
end of the program. [Hint: You may use the function split ()]
23. Write a program to input names of n students and store them in a tuple. Also, input
a name from the user and find if this student is present in the tuple or not.
24. Write a Python program to create a dictionary from a string. Note: Track the count
of the letters from the string. Sample string: ‘2nd pu course’ Expected output : {‘ ‘:2,
‘2’:1, ‘n’:1, ‘d’:1, ‘o’:1, ‘p’:1, ‘u’:2, ’c’:1, ‘s’:1, ‘r’:1, ‘e’:1}
OR
Create a dictionary with the roll number, name and marks of n students in a class and
display the names of students who have marks above 75.
PYTHON PROGRAMMING
1. Write a program to swap two numbers using a third variable.
PROGRAM:
p = int( input("Please enter value for P: "))
q = int( input("Please enter value for Q: "))
temp=p
p=q
q=temp
print ("The Value of P after swapping: ", p)
print ("The Value of Q after swapping: ", q)
OUTPUT:
Please enter value for P: 20
Please enter value for Q: 40
The Value of P after swapping: 40
The Value of Q after swapping: 20
PYTHON PROGRAMMING
2. Write a program to enter two integers and perform all arithmetic operations
on them.
PROGRAM:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Printing the result for all arithmetic operations:-")
print("Addition: ",num1+num2)
print("Subtraction: ",num1-num2)
print("Multiplication: ",num1*num2)
print("Division: ",num1/num2)
print("Modulus: ", num1%num2)
OUTPUT:
Enter first number: 40
Enter second number: 30
Printing the result for all arithmetic operations:-
Addition: 70
Subtraction: 10
Multiplication: 1200
Division: 1.3333333333333333
Modulus: 10
PYTHON PROGRAMMING
3. Write a Python program to accept length and width of a rectangle and
compute its perimeter and area.
PROGRAM:
length = float(input("Enter length of the rectangle: "))
breadth = float(input("Enter breadth of the rectangle: "))
area = length * breadth
perimeter = 2 * (length + breadth)
print("Area of rectangle = ", area)
print("Perimeter of rectangle = ", perimeter)
OUTPUT:
Enter length of the rectangle: 2
Enter breadth of the rectangle: 3
Area of rectangle = 6.0
Perimeter of rectangle = 10.0
PYTHON PROGRAMMING
4. Write a Python program to calculate the amount payable if money has been
lent on simple interest. Principal or money lent = P,Rate of interest = R% per
annum and Time = T years. Then Simple Interest (SI) = (P x R x T)/ 100.
PROGRAM:
principal = float(input('Enter amount: '))
time = float(input('Enter time: '))
rate = float(input('Enter rate: '))
si = (principal*time*rate)/100
print('Simple interest is: ', si)
OUTPUT:
Enter amount: 1000
Enter time: 2
Enter rate: 3
Simple interest is: 60.0
PYTHON PROGRAMMING
5. Write a Python program to find largest among three numbers.
PROGRAM:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
largest=num1
if num2 > largest:
largest = num2
if num3 > largest:
largest = num3
print("The largest number is", largest)
OUTPUT:
Enter first number: 50
Enter second number: 20
Enter third number: 30
The largest number is 50.0
PYTHON PROGRAMMING
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).
PROGRAM:
name = input("What is your name? ")
age = int(input("What is your age? "))
if age >= 18:
print("You are eligible to apply for the driving license.")
else:
print("You are not eligible to apply for the driving license.")
OUTPUT-1:
What is your name? Raju
What is your age? 25
You are eligible to apply for the driving license.
OUTPUT-2:
What is your name? Kumar
What is your age? 15
You are not eligible to apply for the driving license.
PYTHON PROGRAMMING
7. Write a program that prints minimum and maximum of five numbers
entered by the user.
PROGRAM
smallest = 0
largest = 0
for a in range(0,5):
x = int(input("Enter the number: "))
if a == 0:
smallest = largest = x
if(x < smallest):
smallest = x
if(x > largest):
largest = x
print("The smallest number is",smallest)
print("The largest number is ",largest)
OUTPUT:
Enter the number: 10
Enter the number: 20
Enter the number: 30
Enter the number: 40
Enter the number: 65
The smallest number is 10
The largest number is 65
PYTHON PROGRAMMING
8. Write a python program to find the grade of a student when grades are
allocated as given in the table below. Percentage of
Marks Grade
Above 90% A
80% to 90% B
70% to 80% C
60% to 70% D
Below 60% E
Percentage of the marks obtained by the student is input to the program.
PROGRAM
n = float(input('Enter the percentage of the student: '))
if(n > 90):
print("Grade A")
elif(n > 80):
print("Grade B")
elif(n > 70):
print("Grade C")
elif(n >= 60):
print("Grade D")
else:
print("Grade E")
OUTPUT-1
Enter the percentage of the student: 98
Grade A
OUTPUT-2
Enter the percentage of the student: 85
Grade B
OUTPUT-3
Enter the percentage of the student: 75
Grade C
OUTPUT-4
Enter the percentage of the student: 65
Grade D
OUTPUT-5
Enter the percentage of the student: 35
Grade E
PYTHON PROGRAMMING
9. Write a python program to print the table of a given number. The
number has to be entered by the user.
PROGRAM
num = int(input("Enter the number: "))
count = 1
while count <= 10:
prd = num * count
print(num, 'x', count, '=', prd)
count += 1
OUTPUT
Enter the number: 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
PYTHON PROGRAMMING
10. Write a program to find the sum of digits of an integer number, input by
the user.
PROGRAM
sum = 0
n = int(input("Enter the number: "))
while n > 0:
digit = n % 10
sum = sum + digit
n = n//10
print("The sum of digits of the number is", sum)
OUTPUT1
Enter the number: 12345
The sum of digits of the number is 15
OUTPUT2
Enter the number: 3456
The sum of digits of the number is 18
PYTHON PROGRAMMING
11. Write a program to check whether an input number is a palindrome or not.
PROGRAM
n = int(input("Enter a number:"))
temp = n
reverse = 0
while(n>0):
digit = n % 10
reverse = reverse*10 + digit
n = n // 10
if(temp == reverse):
print("Palindrome")
else:
print("Not a Palindrome“)
OUTPUT1
Enter a number:121
Palindrome
OUTPUT2
Enter a number:1234
Not a Palindrome
PYTHON PROGRAMMING
12. Write a python program to print the following patterns:
12345
1234
123
12
1
PROGRAM
rows = int(input("Enter the number of rows: "))
for i in range(rows,0,-1):
for j in range(1,i+1):
print(j, end=" ")
print()
OUTPUT1
Enter the number of rows: 3
123
12
1
OUTPUT2
Enter the number of rows: 5
12345
1234
123
12
1
PYTHON PROGRAMMING
1. 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 on the basis of the
gender.
PROGRAM
def prefix(name,gender):
if (gender == 'M' or gender == 'm'):
print("Hello, Mr.",name)
elif (gender == 'F' or gender == 'f'):
print("Hello, Ms.",name)
else:
print("Please enter only M or F in gender")
name = input("Enter your name: ")
gender = input("Enter your gender: M for Male, and F for Female: ")
prefix(name,gender)
OUTPUT1
Enter your name: RAM
Enter your gender: M for Male, and F for Female: M
Hello, Mr. RAM
OUTPUT2
Enter your name: SITA
Enter your gender: M for Male, and F for Female: F
Hello, Ms. SITA
PYTHON PROGRAMMING
2. Write a program that has a user defined function to accept the coefficients of
a quadratic equation in variables and calculates its determinant. For example :
if the coefficients are stored in the variables a,b,c then calculate determinant as
b2 - 4ac. Write the appropriate condition to check determinants on positive, zero
and negative and output appropriate result.
PROGRAM
def discriminant(a, b, c):
d = b**2 - 4 * a * c
return d
print("For a quadratic equation in the form of ax^2 + bx + c = 0")
a = int(input("Enter the value of a: "))
b = int(input("Enter the value of b: "))
c = int(input("Enter the value of c: "))
det = discriminant(a,b,c)
if (det > 0):
print("The quadratic equation has two real roots.")
elif (det == 0):
print("The quadratic equation has one real root.")
else:
print("The quadratic equation doesn't have any real root.“)
OUTPUT1
For a quadratic equation in the form of ax^2 + bx + c = 0
Enter the value of a: 1
Enter the value of b: -9
Enter the value of c: 10
The quadratic equation has two real roots.
OUTPUT2
For a quadratic equation in the form of ax^2 + bx + c = 0
Enter the value of a: 1
Enter the value of b: 2
Syed Sir 14
PYTHON PROGRAMMING
Enter the value of c: 1
The quadratic equation has one real root.
3. Write a python program that has a user defined function to accept 2 numbers
as parameters, if number 1 is less than number 2 then numbers are swapped and
returned, i.e., number 2 is returned in place of number1 and number 1 is
reformed in place of number 2, otherwise the same order is returned.
PROGRAM
def swapN(a, b):
if(a < b):
return b,a
else:
return a,b
n1 = int(input("Enter Number 1: "))
n2 = int(input("Enter Number 2: "))
print("Entered values are :")
print("Number 1:",n1," Number 2: ",n2)
print("Returned value from swap function:")
n1, n2 = swapN(n1, n2)
print("Number 1:",n1," Number 2: ",n2)
OUTPUT1
Enter Number 1: 11
Enter Number 2: 45
Entered values are :
Number 1: 11 Number 2: 45
Returned value from swap function:
Number 1: 45 Number 2: 11
OUTPUT2
Enter Number 1: 34
Enter Number 2: 67
Entered values are :
Number 1: 34 Number 2: 67
Syed Sir 15
PYTHON PROGRAMMING
Returned value from swap function:
Number 1: 67 Number 2: 34
4. Write a python 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).
PROGRAM
userInput = input("Write a sentence: ")
totalChar = len(userInput)
print("Total Characters: ",totalChar)
totalAlpha = totalDigit = totalSpecial = 0
for a in userInput:
if a.isalpha():
totalAlpha += 1
elif a.isdigit():
totalDigit += 1
else:
totalSpecial += 1
print("Total Alphabets: ",totalAlpha)
print("Total Digits: ",totalDigit)
print("Total Special Characters: ",totalSpecial)
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1
print("Total Words in the Input :",(totalSpace + 1))
OUTPUT1
Write a sentence: HAI WELCOME TO PYTHON LAB
Total Characters: 25
Total Alphabets: 21
Total Digits: 0
Syed Sir 16
PYTHON PROGRAMMING
Total Special Characters: 1
Total Words in the Input : 5
5. 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)
PROGRAM
def convertToTitle(string):
titleString = string.title();
print("The input string in title case is:",titleString)
userInput = input("Write a sentence: ")
totalSpace = 0
for b in userInput:
if b.isspace():
totalSpace += 1
if(userInput.istitle()):
print("The String is already in title case")
elif(totalSpace > 0):
convertToTitle(userInput)
else:
print("The String is of one word only")
OUTPUT:
Write a sentence: welocome python programming
Syed Sir 17
PYTHON PROGRAMMING
The input string in title case is: Welocome Python Programming
6. Write a python program that takes a sentence as an input parameter where
each word in the sentence is separated by a space. The function should replace
each blank with a hyphen and then return the modified sentence.
PROGRAM
def replaceChar(string):
return string.replace(' ','-')
userInput = input("Enter a sentence: ")
result = replaceChar(userInput)
print("The new sentence is:",result)
OUTPUT
Enter a sentence: Computer Science
The new sentence is: Computer-Science
Syed Sir 18
PYTHON PROGRAMMING
7. Write a python program to find the number of times an element occurs in
the list.
PROGRAM
list1 = [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
print("The list is:",list1)
inp = int(input("Which element occurrence would you like to count? "))
count = list1.count(inp)
print("The count of element",inp,"in the list is:",count)
OUTPUT1
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? 20
The count of element 20 in the list is: 2
OUTPUT2
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? 30
The count of element 30 in the list is: 3
Syed Sir 19
PYTHON PROGRAMMING
8. Write a python function that returns the largest element of the list passed as
parameter.
PROGRAM
def largestNum(list1):
large = max(list1)
return large
list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
max_num = largestNum(list1)
print("The elements of the list",list1)
print("\nThe largest number of the list:",max_num)
OUTPUT:
The elements of the list [1, 2, 3, 4, 5, 6, 7, 8, 9]
The largest number of the list:
Syed Sir 20
PYTHON PROGRAMMING
9. Write a python program to read a list of elements. Modify this list so that it
does not contain any duplicate elements, i.e., all elements occurring multiple
times in the list should appear only once.
PROGRAM
def removeDup(list1):
length = len(list1)
newList = [ ]
for a in range(length):
if list1[a] not in newList:
newList.append(list1[a])
return newList
list1 = [ ]
inp = int(input("How many elements do you want to add in the list? "))
for i in range(inp):
a = int(input("Enter the elements: "))
list1.append(a)
print("The list entered is:",list1)
Syed Sir 21
PYTHON PROGRAMMING
print("The list without any duplicate element is:",removeDup(list1)
OUTPUT:
How many elements do you want to add in the list? 5
Enter the elements: 1
Enter the elements: 2
Enter the elements: 2
Enter the elements: 3
Enter the elements: 4
The list entered is: [1, 2, 2, 3, 4]
The list without any duplicate element is: [1, 2, 3, 4]
10. Write a python program to read email IDs of n number of students and store
them in a tuple. Create two new tuples, one to store only the usernames from the
email IDs and second to store domain names from the email IDs. Print all three
tuples at the end of the program. [Hint: You may use the function split()]
PROGRAM
emails = tuple()
username = tuple()
domainname = tuple()
n = int(input("How many email ids you want to enter?: "))
for i in range(0,n):
emid = input("> ")
emails = emails +(emid,)
spl = emid.split("@")
username = username + (spl[0],)
domainname = domainname + (spl[1],)
print("\nThe email ids in the tuple are:")
print(emails)
print("\nThe username in the email ids are:")
print(username)
print("\nThe domain name in the email ids are:")
print(domainname)
OUTPUT:
Syed Sir 22
PYTHON PROGRAMMING
How many email ids you want to enter?: 2
>
[email protected] >
[email protected] The email ids in the tuple are:
('
[email protected]', '
[email protected]')
The username in the email ids are:
('bapu', 'computer')
The domain name in the email ids are:
('gmail.com', 'yahoo.com')
11. Write a python program to input names of n students and store them in a
tuple. Also, input a name from the user and find if this student is present in the
tuple or not.
PROGRAM
name = tuple()
n = int(input("How many names do you want to enter?: "))
for i in range(0, n):
num = input("> ")
name = name + (num,)
print("\nThe names entered in the tuple are:")
print(name)
search=input("\nEnter the name of the student you want to search? ")
for a in name:
if(a==search):
print("The name",search, " is present in the tuple")
exit(0)
print("The name",search,"is not found in the tuple")
OUTPUT:
Syed Sir 23
PYTHON PROGRAMMING
How many names do you want to enter?: 3
> Raju
> David
> Rani
The names entered in the tuple are:
("Raju'", 'David', 'Rani')
Enter the name of the student you want to search? Rani
The name Rani is present in the tuple
12a). Write a Python program to create a dictionary from a string. Note: Track
the count of the letters from the string. Sample string :‘2nd PU Course'
Expected output : {'P': 1, 'U': 1, ' ': 1, 'C': 1, 'o': 1, 'u': 1, 'r': 1, 's': 1, 'e': 1}
PROGRAM
myStr="PU Course"
print("The input string is:",myStr)
myDict=dict()
for character in myStr:
if character in myDict:
myDict[character]+=1
else:
myDict[character]=1
print("The dictionary created from characters of the string is:")
print(myDict)
OUTPUT:
The input string is: PU Course
The dictionary created from characters of the string is:
{'P': 1, 'U': 1, ' ': 1, 'C': 1, 'o': 1, 'u': 1, 'r': 1, 's': 1, 'e': 1}
Syed Sir 24
PYTHON PROGRAMMING
12b). Create a dictionary with the roll number, name and marks of n
students in a class and display the names of students who have marks
above 75.
PROGRAM
no_of_std = int(input("Enter number of students: "))
result = {}
for i in range(no_of_std):
print("Enter Details of student No.", i+1)
roll_no = int(input("Roll No: "))
std_name = input("Student Name: ")
marks = int(input("Marks: "))
result[roll_no] = [std_name, marks]
print(result)
for student in result:
if result[student][1] > 75:
print("Student's name who get more than 75 marks
is/are",(result[student][0]))
OUTPUT:
Enter number of students: 2
Enter Details of student No. 1
Roll No: 123
Student Name: Asha
Marks: 60
Syed Sir 25
PYTHON PROGRAMMING
Enter Details of student No. 2
Roll No: 124
Student Name: Rocky
Marks: 80
{123: ['Asha', 60], 124: ['Rocky', 80]}
Student's name who get more than 75 marks is/are Rocky
Syed Sir 26