0% found this document useful (0 votes)
667 views39 pages

I Puc Lab Manual 2024-25

The document is a lab manual for the Department of Computer Science, containing various Python programming exercises for students. It includes programs for tasks such as swapping numbers, performing arithmetic operations, calculating the area and perimeter of a rectangle, and determining eligibility for a driving license. Each program is accompanied by a flowchart, example outputs, and explanations of the algorithms used.

Uploaded by

Shamith Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
667 views39 pages

I Puc Lab Manual 2024-25

The document is a lab manual for the Department of Computer Science, containing various Python programming exercises for students. It includes programs for tasks such as swapping numbers, performing arithmetic operations, calculating the area and perimeter of a rectangle, and determining eligibility for a driving license. Each program is accompanied by a flowchart, example outputs, and explanations of the algorithms used.

Uploaded by

Shamith Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

SECTION -A
PROGRAM 1
1. Write a program to swap two numbers using a third variable.
Program 1 : Flowchart:
x = int(input("Enter the value of x"))
y = int(input("Enter the value of y"))
print("Values of variables before swapping")
print("Value of x:", x)
print("Value of y:", y)
temp = x
x=y
y = temp
print("Values of variables after swapping")
print("Value of x:", x)
print("Value of y:", y)

Output 1:
Enter the value of x 5
Enter the value of y 10
Values of variables before swapping
Value of x: 5
Value of y: 10
Values of variables after swapping
Value of x: 10
Value of y: 5

Output 2:
Enter the value of x -1
Enter the value of y-5
Values of variables before swapping
Value of x: -1
Value of y: -5
Values of variables after swapping
Value of x: -5
Value of y: -1

*****

PROGRAM 2

MES PU COLLEGES 1
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Write a program to enter two integers and perform all arithmetic operations on them.
Program: Flowchart:

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 1:

Enter first number: 5


Enter second number: 10
Printing the result for all arithmetic operations:-
Addition: 15
Subtraction: -5
Multiplication: 50
Division: 0.5
Modulus: 5

Output 2:

Enter first number: -5


Enter second number: -10
Printing the result for all arithmetic operations:-
Addition: -15
Subtraction: 5
Multiplication: 50
Division: 0.5
Modulus: -5

*****

PROGRAM 3

MES PU COLLEGES 2
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Write a Python program to accept length and width of a rectangle and compute its perimeter
and area.
Program: Flowchart:

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 1:
Enter length of the rectangle: 10
Enter breadth of the rectangle: 20
Area of rectangle = 200.0
Perimeter of rectangle = 400.0

Output 2:
Enter length of the rectangle: 5
Enter breadth of the rectangle: 15
Area of rectangle = 75.0
Perimeter of rectangle = 150.0

*****

PROGRAM 4

MES PU COLLEGES 3
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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.
Amount payable = Principal + SI.
P, R and T are given as input to the program.

Program: Flowchart:
principal = float(input('Enter amount: '))
time = float(input('Enter time: '))
rate = float(input('Enter rate: '))
simple_interest = (principal*time*rate)/100
print('Simple interest is: ',simple_interest)

Output 1:
Enter amount: 1000
Enter time: 2
Enter rate: 5
Simple interest is: 100.0

Output 2:
Enter amount: 2000
Enter time: 3
Enter rate: 4
Simple interest is: 240.0

*****

PROGRAM 5
Write a Python program to find largest among three numbers.

MES PU COLLEGES 4
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Flowchart:

Program:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)

Output 1: Output 2:
Enter first number: 5 Enter first number: 3
Enter second number: 10 Enter second number: 2
Enter third number: 15 Enter third number: 4
The largest number is 15.0 The largest number is 4.0

*****

PROGRAM 6

MES PU COLLEGES 5
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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).
Flowchart:

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? Anusha
What is your age? 20
You are eligible to apply for the driving license.

Output 2:
What is your name? Sahana
What is your age? 15
You are not eligible to apply for the driving license.

*****

PROGRAM 7
MES PU COLLEGES 6
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Write a program that prints minimum and maximum of five numbers entered by the user.
Flowchart:

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 1:
Enter the number: 3
MES PU COLLEGES 7
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Enter the number: 5


Enter the number: 6
Enter the number: 2
Enter the number: 9
The smallest number is 2
The largest number is 9

Output 2:
Enter the number: 2
Enter the number: 3
Enter the number: 4
Enter the number: 5
Enter the number: 6
The smallest number is 2
The largest number is 6

*****

PROGRAM 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

MES PU COLLEGES 8
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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.
Flowchart:

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: Output 2:
Enter the percentage of the student: 90 Enter the percentage of the student: 60
Grade B Grade D
*****
PROGRAM 9
Write a python program to print the table of a given number. The number has to be entered
by the user

MES PU COLLEGES 9
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Flowchart:

Algorithm:
Step 1: START
Step 2: INPUT num
Step 3: while count <= 10
prd = num * count
print(num, 'x', count, '=', prd)
count += 1
Step 4:OUTPUT prd
Step 5: STOP

Program:
num = int(input("Enter the number: "))
count = 1
while count <= 10:
prd = num * count
print(num, 'x', count, '=', prd)
count += 1

Output 1: Output 2:
Enter the number: 5 Enter the number: 3
5x1=5 3x1=3
5 x 2 = 10 3x2=6
5 x 3 = 15 3x3=9
5 x 4 = 20 3 x 4 = 12
5 x 5 = 25 3 x 5 = 15
5 x 6 = 30 3 x 6 = 18
5 x 7 = 35 3 x 7 = 21
5 x 8 = 40 3 x 8 = 24
MES PU COLLEGES 10
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

5 x 9 = 45 3 x 9 = 27
5 x 10 = 50 3 x 10 = 30

*****

PROGRAM 10
Write a program to find the sum of digits of an integer number, input by the user
flowchart:

Algorithm:
Step 1: START
Step 2: INPUT n
Step 3: sum=0
Step 4: while n > 0:
digit = n % 10
sum = sum + digit
n = n//10
Step 4:OUTPUT sum
Step 5: STOP

Program:
MES PU COLLEGES 11
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

n = int(input("Enter the number: "))


sum = 0
while n > 0:
digit = n % 10
sum = sum + digit
n = n//10
print("The sum of digits of the number is”, sum)

Output 1:
Enter the number: 123
The sum of digits of the number is 6

Output 2:
Enter the number: 457
The sum of digits of the number is 16
*****

PROGRAM 11
Write a program to check whether an input number is a palindrome or not.
Flowchart:

Algorithm:
Step 1: START
Step 2: INPUT num
Step 3: temp = n
MES PU COLLEGES 12
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

reverse = 0
Step 4: while(n>0):
digit = n % 10
reverse = reverse*10 + digit
n = n // 10
Step 5: if(temp == reverse):
print("Palindrome")
else:
print("Not a Palindrome”)
Step 6:OUTPUT reverse
Step 7: STOP

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

Output 1: Output 2:
Enter a number:5 Enter a number:12
Palindrome Not a Palindrome

*****

PROGRAM 12
Write a python program to print the following patterns:
12345
1234
123
12
1
Flowchart:
Algorithm:
Step 1: START
Step 2: INPUT rows
Step 3: for i =rows down to 0
for j in range(1,i+1):
MES PU COLLEGES 13
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

print(j)
Step 5: STOP

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

Output 1:
Enter the number of rows: 3
123
12
1

Output 2:
Enter the number of rows: 5
12345
1234
123
12
1

*****

SECTION -B
PROGRAM B1
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.
Flowchart:

MES PU COLLEGES 14
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Algorithm:
Step 1: START
Step 2: INPUT name,gender
Step 3: prefix(name,gender)
Step 4: OUTPUT j
Step 5: STOP

prefix(name,gender):
Step 1: 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")

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)

Output 1:
Enter your name: DEEPA
Enter your gender: M for Male, and F for Female: F
MES PU COLLEGES 15
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Hello, Ms. DEEPA

Output 2:
Enter your name: RAM
Enter your gender: M for Male, and F for Female: M
Hello, Mr. RAM

*****
PROGRAM B2
Write a program that has a user defined function to accept the coefficients of a quadratic
equation and calculate 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 as positive, zero and negative and print appropriate result.
Flowchart:

Algorithm:
Step 1: START
Step 2: INPUT a,b,c
Step 3: det = discriminant(a,b,c)
Step 4: if (det > 0):
print("The quadratic equation has two real roots.")
elif (det == 0):
print("The quadratic equation has one real root.")
MES PU COLLEGES 16
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

else:
print("The quadratic equation doesn't have any real root.“)
Step 5: OUTPUT j
Step 6: STOP

discriminant(a, b, c):
Step 1 : d = b**2 - 4 * a * c
Step 2 : return d

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.“)

Output 1:
For a quadratic equation in the form of ax^2 + bx + c = 0
Enter the value of a: 2
Enter the value of b: 3
Enter the value of c: 4
The quadratic equation doesn't have any real root

Output 2:
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
Enter the value of c: 1
The quadratic equation has one real root.

Output 3:

MES PU COLLEGES 17
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

For a quadratic equation in the form of ax^2 + bx + c = 0


Enter the value of a: 2
Enter the value of b: 8
Enter the value of c: 3
The quadratic equation has two real roots.

*****

PROGRAM B3
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.
Flowchart:

Algorithm:
Step 1: START
Step 2: INPUT x,y
Step 3: x, y = swapN(x, y)
Step 4: OUTPUT x,y

MES PU COLLEGES 18
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Step 5: STOP

swapN(a, b):
Step 1 : if(a < b):
return b,a
else:
return a,b

Program:
def swapN(a, b):
if(a < b):
return b,a
else:
return a,b

x= int(input("Enter Number 1: "))


y= int(input("Enter Number 2: "))
print("Entered values are :")
print("Number 1:",x," Number 2: ",y)
print("Returned value from swap function:")
x, y = swapN(x, y)
print("Number 1:",x," Number 2: ",y)

Output 1:
Enter Number 1: 2
Enter Number 2: 5
Entered values are :
Number 1: 2 Number 2: 5
Returned value from swap function:
Number 1: 5 Number 2: 2

Output 2:
Enter Number 1: 5
Enter Number 2: 2
Entered values are :
Number 1: 5 Number 2: 2
Returned value from swap function:
Number 1: 5 Number 2: 2
*****

PROGRAM B4
MES PU COLLEGES 19
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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).
Flowchart:

False

True False

False
True

True

Algorithm:
Step 1: START
Step 2: INPUT userInput
Step 3: totalChar = len(userInput)
Step 4: totalAlpha = 0
totalDigit = 0
MES PU COLLEGES 20
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

totalSpecial = 0
totalSpace = 0
Step 4: for a in userInput:
if a.isalpha():
totalAlpha += 1
elif a.isdigit():
totalDigit += 1
elif a.isspace():
totalSpace += 1
else:
totalSpecial += 1
Step 4: OUTPUT totalAlpha, totalDigit,totalSpecial, totalSpace + 1
Step 5: STOP

Program:
userInput = input("Write a sentence: ")
totalChar = len(userInput)
print("Total Characters: ",totalChar)
totalAlpha = totalDigit = totalSpecial = totalSpace = 0
for a in userInput:
if a.isalpha():
totalAlpha += 1
elif a.isdigit():
totalDigit += 1
elif a.isspace():
totalSpace += 1
else:
totalSpecial += 1

print("Total Alphabets: ",totalAlpha)


print("Total Digits: ",totalDigit)
print("Total Special Characters: ",totalSpecial)
print("Total Words in the Input :",(totalSpace + 1))

Output 1:
Write a sentence: WELCOME TO PYTHON PROGRAMMING
Total Characters: 29
Total Alphabets: 26
Total Digits: 0
Total Special Characters: 3
Total Words in the Input : 4

Output 2:
Write a sentence: HELLO 123
Total Characters: 9

MES PU COLLEGES 21
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Total Alphabets: 5
Total Digits: 3
Total Special Characters: 1
Total Words in the Input: 2

*****

PROGRAM B5
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)
Flowchart:

Algorithm:
Step 1: START
Step 2: INPUT userInput
Step 3: convertToTitle(userInput)
Step 5: STOP

convertToTitle(string):
Step 1 : titleString = string.title();
Step 2 : OUTPUT titleString

Program:
def convertToTitle(string):
titleString = string.title();
print("The input string in title case is:",titleString)
userInput = input("Write a sentence: ")
convertToTitle(userInput)

MES PU COLLEGES 22
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Output 1:
Write a sentence: hello world
The input string in title case is: Hello World

Output 2:
Write a sentence: HELLO WORLD
The input string in title case is: Hello World

*****

PROGRAM B6
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.

Algorithm:
Step 1: START
Step 2: INPUT userInput
Step 3: result = replaceChar(userInput)
Step 4: OUTPUT result
Step 5: STOP

replaceChar(string):
Step 1 : return string.replace(' ','-')

MES PU COLLEGES 23
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Flowchart:

Program:
def replaceChar(string):
return string.replace(' ','-')
userInput = input("Enter a sentence: ")
result = replaceChar(userInput)
print("The new sentence is:",result)

Output 1:
Enter a sentence: HELLO COMPUTER SCIENCE
The new sentence is: HELLO-COMPUTER-SCIENCE

Output 2:
Enter a sentence: PYTHON PROGRAMMING
The new sentence is: PYTHON-PROGRAMMING

*****

MES PU COLLEGES 24
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

PROGRAM B7
Write a python program to find the number of times an element occurs in the list.
Algorithm:
Step 1: START
Step 2: INPUT list1,inp
Step 3: count = list1.count(inp)
Step 4: OUTPUT count
Step 5: STOP

Flowchart:

Program:
list1 = [10, 20, 30, 40, 50, 60, 20, 50, 10, 30, 50, 30, 24, 45]
print("The list is:",list1)
inp = int(input("Enter the element to count in the list: "))
count = list1.count(inp)
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? 20
The count of element 20 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? 80
The count of element 80 in the list is: 0

MES PU COLLEGES 25
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

*****

PROGRAM B8
Write a python function that returns the largest element of the list passed as parameter.
Algorithm:
Step 1: START
Step 2: INPUT list1
Step 3: max_num = largestNum(list1)
Step 4: OUTPUT max_num
Step 5: STOP

largestNum(list1):
Step 1 : length = len(list1)
Step 2 : num = 0
Step 3 : for i in range(length):
if(i == 0 or list1[i] > num):
num = list1[i]
Step 4 : return num

Flowchart:

MES PU COLLEGES 26
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Program:
def largestNum(list1):
length = len(list1)
num = 0
for i in range(length):
if(i == 0 or list1[i] > num):
num = list1[i]
return num
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: 9

*****
PROGRAM B9
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.

Algorithm:
Step 1: START
Step 2: INPUT inp
Step 3: for i in range(inp):
INPUT a
list1.append(a)
Step 4: OUTPUT list1,removeDup(list1)
Step 5: STOP

removeDup(list1):
Step 1 : length = len(list1)
Step 2 : newList = []
Step 3 : for a in range(length):
if list1[a] not in newList:
newList.append(list1[a])
Step 4 : return newList

MES PU COLLEGES 27
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Flowchart:

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)
print("The list without any duplicate element is:",removeDup(list1))

Output 1:
How many elements do you want to add in the list? 3
Enter the elements: 10
MES PU COLLEGES 28
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Enter the elements: 15


Enter the elements: 20
The list entered is: [10, 15, 20]
The list without any duplicate element is: [10, 15, 20]

Output 2:
How many elements do you want to add in the list? 4
Enter the elements: 10
Enter the elements: 20
Enter the elements: 30
Enter the elements: 30
The list entered is: [10, 20, 30, 30]
The list without any duplicate element is: [10, 20, 30]

*****

PROGRAM B10
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

MES PU COLLEGES 29
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

store domain names from the email IDs. Print all three tuples at the end of the program.
[Hint: You may use the function split ()]

Flowchart:

Program:
emails = tuple()
username = tuple()
domainname = tuple()
MES PU COLLEGES 30
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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 1:
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:


('anu', 'akash')

The domain name in the email ids are:


('gmail.com', 'yahoo.com')

Output 2:
How many email ids you want to enter? 1
> [email protected]

The email ids in the tuple are:


('[email protected]',)

The username in the email ids are:


('computerscience',)

The domain name in the email ids are:


('gmail.com',)
*****
PROGRAM B11
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.
Flowchart:

MES PU COLLEGES 31
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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,)
MES PU COLLEGES 32
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

print("\nThe names entered in the tuple are:")


print(name)
search=input("\nEnter the name of the student you want to search? ")
if search in name:
print("The name",search,"is present in the tuple")
else:
print("The name",search,"is not found in the tuple")

Output 1:
How many names do you want to enter? 3
> Ram
> Shyam
> Shiv

The names entered in the tuple are:


('Ram', 'Shyam', 'Shiv')

Enter the name of the student you want to search? Ram


The name Ram is present in the tuple

Output 2:
How many names do you want to enter?: 3
> Ram
> Shyam
> Shiv

The names entered in the tuple are:


('Ram', 'Shyam', 'Shiv')

Enter the name of the student you want to search? Suresh


The name Suresh is not found in the tuple

*****

PROGRAM B12a
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': 1, 'n': 1, 'd': 1, ' ': 2, 'P': 1, 'U': 1, 'C': 1, 'o': 1, 'u': 1, 'r': 1, 's': 1, 'e': 1}
Flowchart:

MES PU COLLEGES 33
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

Program:
myStr="2nd PU Course"
print("The input string is:",myStr)
myDict=dict()
for c in myStr:
if c in myDict:
myDict[c]+=1
else:
myDict[c]=1
print("The dictionary created from characters of the string is:")
print(myDict)

Output 1:
The input string is: 2nd PU Course
The dictionary created from characters of the string is:
{'2': 1, 'n': 1, 'd': 1, ' ': 2, 'P': 1, 'U': 1, 'C': 1, 'o': 1, 'u': 1, 'r': 1, 's': 1, 'e': 1}

*****
PROGRAM B12b
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.
Flowchart:

MES PU COLLEGES 34
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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]
MES PU COLLEGES 35
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

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 1:
Enter number of students: 2
Enter Details of student No. 1
Roll No: 01
Student Name: Anusha
Marks: 55
Enter Details of student No. 2
Roll No: 02
Student Name: Moksh
Marks: 85
{1: ['Anusha', 55], 2: ['Moksh', 85]}
Student's name who get more than 75 marks is/are Moksh

Output 2:
Enter number of students: 3
Enter Details of student No. 1
Roll No: 01
Student Name: Shiv
Marks: 87
Enter Details of student No. 2
Roll No: 02
Student Name: Ram
Marks: 56
Enter Details of student No. 3
Roll No: 03
Student Name: Robert
Marks: 74
{1: ['Shiv', 87], 2: ['Ram', 56], 3: ['Robert', 74]}
Student's name who get more than 75 marks is/are Shiv

*****
VIVA VOCE
Important Questions and Answers
1. What is a class?
A template from which objects are created.
2. What is an object?
An object is a real-world entity that has attributes(data) and behaviours(functions).
3. What is the full form of OOP?
Object Oriented Programming.
4. Mention any two OOP languages.
C++, Java and Python.

MES PU COLLEGES 36
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

5. Is Python a case sensitive language?


Yes
6. What is a variable?
A programming element whose value may be changed during execution of the program.
7. Name few data types available in Python.
number (int, float, complex), sequence (List, String, Tuple)
8. What is the use of None data type in Python?
A special data type with a single value. It is used when a value for a variable is not available.
9. Give any one rule for naming an identifier?
Keywords are not allowed.
A blank space or special characters are not allowed except underscore.
10. How do you create different types of variables?
Type of variable can be created by assigning relevant values to them.
For example, a = 10, b = 1.5 where a is integer variable and b is a float variable.
11. Differentiate mutable and immutable data types.
Changes are allowed in mutable data types whereas changes are not allowed in immutable data types.
12. Give two examples for mutable data type.
Lists, Dictionaries and Sets.
13. Give two examples for immutable data type.
integer, string and tuple.
14. What are the different data types available for numbers in python?
integer, float, Boolean and complex.
15. Mention various data types come under Sequence type.
List, Strings and Tuples.
16. Give two identity operators? Write their purpose.
is - Returns True, if a given value is belonged to particular type. Otherwise returns False.
is not -Returns True, if a given value is not belonged to particular type. Otherwise returns False.
17. Give two membership operators? Write their purpose.
in- Returns True, if a given value is belonged to particular sequence. Otherwise returns False.
not in -Returns True, if a given value is not belonged to particular sequence. Otherwise returns False.
18. Which is the shell prompt of IDLE interactive mode?
>>> is a shell prompt.
19. Give the differences between interactive mode and script mode.
Interactive mode is used for execution of individual commands.
Script mode is used for editing large number of sequence of instructions as a file for particular purpose.
20. What is the default extension of Python file?
.py
21. Write any two keywords of Python.
def, elif, while, True, False, None etc.

22. Give any one input function of Python. Write its syntax.
input ()-used for accepting user input.
Syntax: variable =input ([prompt])
23. Give any one output function of Python. Write its syntax.
print()-used for displaying the results on screen.
Syntax: print(value [, ..., sep=’ ’, end = '\n'])
24. What is a comment?
A non-executable statement in program is called a comment.
25. How to define single line comment?
A single line comment can be added using # symbol.
Example: # My first program in Python.

MES PU COLLEGES 37
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

26. How to add multi line comment in Python program?


Multi-line comment can be added using pair of single, or double quotes.
27. What is the purpose of using ** operator in Python?
It is used for finding x³ value. (ie. exponential value).
28. What is the purpose of using // operator in Python?
// - gives integer quotient.
29. Give the differences between / and //.
/- gives float quotient.
// - gives integer quotient.
30. Give the differences between * and **.
*-is used for multiplication purpose.
**- is used for finding x value. (i.e., exponential value).
31. What is debugging?
A process of removing the errors or fixing the bugs in a software program is called debugging.
32. What is syntax error?
An error occurred due to wrong grammar(rule) of a language.
33. What is semantic error?
An error occurred due to wrong logic or meaning of programming statements.
34. What is runtime error? Give an example.
A type of error occurred at the time of execution. For example, dividing by 0(zero) situation.
35. What is indentation? Write its importance in Python programming.
A process of leaving uniform number of spaces to mark a block is called indentation. It is used to define block
or group of statements as one unit.
36. Give the different selection statements available in Python.
if, if else, elif, if-else-if.
37. Mention the types of looping statement available in Python.
while loop and for loop.
38. Write the differences between break and continue statement.
break-it terminates the loop.
continue-it switches the control to beginning of a loop.
39. What is a function?
A sub-program is called a function. that means a large program is decomposed into small versions called a
function.
40. Which symbol is used at the end of function header?
A colon symbol (:) is used at the end of function header.
41. What are argument and parameter?
A variable which is present in the function call is called an argument.
A variable which is present in the function header is called a parameter.

42. What is default parameter?


A pre-decided value is called default parameter. It sets automatically when value is not available.
43. What is the use of return statement?
It shifts the control to a calling function. It may or may not return value(s).
44. Differentiate local and global variables.
A variable which is defined inside the block or function is called local variable.
A variable which is defined outside the block or function is called global variable.
45. Give any two built-in functions available in Python.
len()- to find length, that means counts number of characters.
divmod()-to find quotient and remainder.
abs(x)- gives absolute value of x.
46. What is a module? Write its importance.

MES PU COLLEGES 38
DEPARTMENT OF COMPUTER SCIENCE I PUC LAB MANUAL

A module is a .py file containing executable code and function definitions.


47. Name few built-in modules.
math module, random module, statistics module.
48. Give any two functions of math module.
math.sqrt(x) - find square root of x.
math.gcd(x, y) - find GCD of x and y.
49. Give any two functions of random module.
random.random() - returns random real numbers in the range 0.0 to 1.0.
random.randint(x, y) - return random integer between x and y.
50. Give any two functions of statistics module.
statistics.mean(x) - finds arithmetic mean.
statistics.mode(x) - returns most repeated value.

*****

MES PU COLLEGES 39

You might also like