0% found this document useful (0 votes)
188 views35 pages

Lab Manual 1st Year (Updated)

sdsdsd

Uploaded by

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

Lab Manual 1st Year (Updated)

sdsdsd

Uploaded by

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

st

1 PU LAB MANUAL
COMPUTER SCIENCE
Practical Exam Scheme:
PART – A

1. Write a Python program to swap two numbers using a third


variable.

a=input("enter the first number:")


b=input("enter the second number:")
print(" numbers before swapping :" ,a,b)
temp=a
a=b
b=temp
print("numbers after swapping :")
print("a=",a)
print("b=",b)

Output:

enter the first number: 10


enter the second number: 20
numbers before swapping : 10 20
numbers after swapping :
a= 20
b= 10
2. Write a Python program to enter two integers and perform all
arithmetic operations on them.

a=int(input(' enter the first number :'))


b=int(input('enter the second number :'))
c= a+b
d= a-b
e=a*b
f=a/b
g=a%b
h=a**b
i=a//b
print("arithmetic operations performed on two numbers are :")
print("addition=", c)
print("Subtraction=",d)
print("multiplication=",e)
print("division=",f)
print("modulus=",g)
print("exponent=",h)
print("floor division=",i)

Output:

enter the first number : 6


enter the second number : 2
arithmetic operations performed on two
numbers are :
addition= 8
Subtraction= 4
multiplication= 12
division= 3.0
modulus= 0
exponent= 36
floor division= 3
3. Write a Python program to accept length and width of a
rectangle and compute its perimeter and area.

length=float(input(' enter the length :'))


width=float(input('enter the width:'))
area=length*width
perimeter=2*(length+width)
print("area of rectangle=",area)
print("perimeter of rectangle=",perimeter)

Output:
enter the length : 25
enter the breadth: 10
area of rectangle= 250.0
perimeter of rectangle= 70.0
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.
➢ Amount payable = Principal + SI.
➢ P, R and T are given as input to the program.

principal=int(input("enter the principal amount :"))


time=int(input("enter the time in years :"))
rate_of_interest=int(input("enter the rate of interest :"))
simple_interest=principal*time*rate_of_interest/100
amount_payable=principal+simple_interest
print("simple interest is=",simple_interest)
print("amount payable=",amount_payable)

Output:

enter the principal amount : 10000


enter the time in years : 2
enter the rate of interest : 10
simple interest is= 2000.0
amount payable= 12000.0
5. Write a Python program to find largest among three numbers.

num1=int(input("enter the first number :"))


num2=int(input("enter the second
number :"))
num3=int(input("enter the third number :"))
largest=num1
if num2>largest:
largest=num2
if num3>largest:
largest=num3
print("the largest among three is :", largest)

Output:
enter the first number : 3
enter the second number : 7
enter the third number : 5
the largest among three is : 7
6. Write a Python 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).

name=input("enter the name:")


age=int(input("enter the age:"))
print("name=",name)
print("age=",age)
if age>=18 :
print("Eligible")
else :
print("Not eligible ")

Output:
enter the name: Deekshit
enter the age: 23
name= Deekshit
age= 23
Eligible
7. Write a program that prints minimum and maximum of five
numbers entered by the user.

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


Enter the number: -3
Enter the number: 10
Enter the number: 45
Enter the number: 80
The smallest number is -3
The largest number is 80
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.

percentage=float(input("Enter the percentage ="))


if percentage>90:
grade = "A"
elif percentage>=80:
grade ="B"
elif percentage>=70:
grade ="C"
elif percentage>=60:
grade ="D"
else:
grade = "E"

print("grade=:", grade)

Output:
Enter the percentage = 94.4
grade=: A

Enter the percentage = 55.61


grade=: E
9. Write a function to print a table of given number. The number
has to entered by the user.

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: 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
10. Write a program to find sum of digits of an integer
number:

number=int(input("enter an integer number:"))


sum =0
temp=abs(number)
while temp>0:
digit=temp%10
sum +=digit
temp//=10
print("sum of digits=",sum)

Output:

enter an integer number:325


sum of digits= 10
11. Write a program to check whether an input number is a
Palindrome or not.

number=int(input("enter the number:"))


original_number=number
reversed_number=0
while number>0:
digit=number%10
reversed_number=(reversed_number*10)+digit
number//=10
if original_number==reversed_number:
print(original_number,"is a palindrome")
else:
print(original_number,"is not a palindrome")

Output:

enter the number:121


121 is a palindrome
enter the number:2435
2435 is not a palindrome
12. Write a program to print the following pattern

12345
1234
123
12
1

rows=5
for i in range(rows, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print()

Output:
12345
1234
123
12
1
PART - B
Chapter 7: Functions

13. Write a program that uses user-defined function that


accepts name and gender (as M for Male F for Female) and
prefixes Mr./Ms. Based on gender

def add_prefix(name, gender):


if gender=='M':
return "Mr. "+name
elif gender=='F':
return "Ms. "+name
else:
return "Unknown Gender"
name=input("Enter your name: ")
gender=input("Enter your gender: (M for Male, F for Female) ")
result=add_prefix(name, gender)
print("Greetings: ",result)

Output:

Enter your name: Arya


Enter your gender: (M for Male, F for Female) F
Greetings: Ms. Arya

Enter your name: Pranav


Enter your gender: (M for Male, F for Female) M
Greetings: Mr. Pranav

Enter your name: A


Enter your gender: (M for Male, F for Female) K
Greetings: Unknown Gender
14. Write a program with a user-defined function to accept
the coefficients of a quadratic equation, calculate the
discriminant (b2 -4ac), check if it's positive, zero, or negative,
and output the result.

def calculate_discriminant_and_result():
a = float(input("Enter coefficient a: "))
b = float(input("Enter coefficient b: "))
c = float(input("Enter coefficient c: "))
discriminant=b**2 - 4*a*c
if discriminant > 0:
result = "The discriminant is positive. The quadratic equation
has two distinct real roots."
elif discriminant == 0:
result = "The discriminant is zero. The quadratic equation has
one real root (a repeated root)."
else:
result = "The discriminant is negative. The quadratic equation
has no real roots."
return discriminant, result

discriminant_result, message =calculate_discriminant_and_result()

print("Discriminant:", discriminant_result)
print("Result:", message)

Output:

Enter coefficient a: 1
Enter coefficient b: 2
Enter coefficient c: 1
Discriminant: 0.0
Result: The discriminant is zero. The quadratic equation has one real ro
ot (a repeated root)
Enter coefficient a: 2
Enter coefficient b: -3
Enter coefficient c: 1
Discriminant: 1.0
Result: The discriminant is positive. The quadratic equation has two dist
inct real roots.

Enter coefficient a: 2
Enter coefficient b: -2
Enter coefficient c: 1
Discriminant: -4.0
Result: The discriminant is negative. The quadratic equation has no real
roots.
15. Write a program with a user-defined function that accepts
two numbers as parameters, swaps them if the first is less than
the second, and returns the numbers in the new or same order.

def swap_numbers(num1, num2):


if num1 < num2:
temp = num1
num1 = num2
num2 = temp
return num1, num2
number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
result1, result2 = swap_numbers(number1, number2)
print("Original Numbers:", number1, number2)
print("Swapped Numbers:", result1, result2)

Ouput:

Enter the first number: 23


Enter the second number: 28
Original Numbers: 23 28
Swapped Numbers: 28 23
Chapter 8: Strings

16. Write a program to input lines of text until Enter is


pressed, counting total characters (including spaces), alphabets,
digits, special symbols, and words (assume each word is
separated by one space).

total_characters = 0
total_alphabets = 0
total_digits = 0
total_special_symbols = 0
total_words = 0

print("Enter text (Press Enter to finish):")


while True:
line = input()
if line == "":
break
total_characters += len(line)
total_words += len(line.split())
for char in line:
if char.isalpha():
total_alphabets += 1
elif char.isdigit():
total_digits += 1
else:
total_special_symbols += 1

print("\nResults:")
print("Total Characters:", total_characters)
print("Total Alphabets:", total_alphabets)
print("Total Digits:", total_digits)
print("Total Special Symbols:", total_special_symbols)
print("Total Words:", total_words)
Ouput:

Enter text (Press Enter to finish):


I love my 1st year at BASE

Results:
Total Characters: 26
Total Alphabets: 19
Total Digits: 1
Total Special Symbols: 6
Total Words: 7
17. Write a user-defined function to convert a string with
more than one word into title case, where the string is passed
as a parameter. (Title case means the first letter of each word is
capitalized.)

def convert_to_title_case(input_string):
title_case_string = ""
capitalize_next = True
for char in input_string:
if capitalize_next and char.isalpha():
title_case_string += char.upper()
capitalize_next = False
else:
title_case_string += char.lower()
if char.isspace():
capitalize_next = True
return title_case_string
user_input = input("Enter a string with more than one word: ")
title_case_result = convert_to_title_case(user_input)
print("Original String:", user_input)
print("Title Case String:", title_case_result)

Output:

Enter a string with more than one word: I enjoy my computer classes at
BASE PU College
Original String: I enjoy my computer classes at BASE PU College
Title Case String: I Enjoy My Computer Classes At Base Pu College
18. Write a function that takes a sentence as an input
parameter, replaces each space with a hyphen, and returns the
modified sentence.

def replace_blank_with_hyphen(sentence):
modified_sentence = sentence.replace(' ', '-')
return modified_sentence
user_input = input("Enter a sentence with words separated by spaces: ")
modified_result = replace_blank_with_hyphen(user_input)
print("Original Sentence:", user_input)
print("Modified Sentence:", modified_result)

Output:

Enter a sentence with words separated by spaces: BASE PU College


Bannerghatta
Original Sentence: BASE PU College Bannerghatta
Modified Sentence: BASE-PU-College-Bannerghatta
Chapter 9: Lists

19. Write a program to find the number of times an element


occurs in the list.

user_list = eval(input("Enter a list of elements: "))


element_to_count = eval(input("Enter the element to count occurrences:
"))
occurrences_count = user_list.count(element_to_count)
print("The element '", element_to_count, "' occurs", occurrences_count,
"times in the list.")

Output:

Enter a list of elements: 1,2,3,2,4,3


Enter the element to count occurrences: 2
The element ' 2 ' occurs 2 times in the list.

Enter a list of elements: 'a','b','c','a','b'


Enter the element to count occurrences: 'a'
The element ' a ' occurs 2 times in the list.
20. Write a function that returns the largest element of the list
passed as parameter.

def find_largest_element(input_list):
if not input_list:
return None
largest = input_list[0]
for element in input_list:
if element > largest:
largest = element
return largest
user_list = eval(input("Enter a list of elements: "))
largest_element = find_largest_element(user_list)
if largest_element is not None:
print("The largest element in the list is:", largest_element)
else:
print("The list is empty.")

Output:

Enter a list of elements: 1, 2, 3, 4, -1


The largest element in the list is: 4
21. Write a program to read a list of elements and modify it to
remove duplicate elements so that each element appears only
once.
user_list = eval(input("Enter a list of elements: "))
unique_list = []
for element in user_list:
if element not in unique_list:
unique_list.append(element)
print("Original List:", user_list)
print("List without Duplicates:", unique_list)

Output:

Enter a list of elements: 1,2,1,3,4,2,5


Original List: (1, 2, 1, 3, 4, 2, 5)
List without Duplicates: [1, 2, 3, 4, 5]
Chapter 10: Tuples and Dictionaries

22. Write a program to read email IDs of n students and store


them in a tuple, then create two new tuples to store usernames
and domain names from the email IDs, and print all three
tuples. (Hint: Use the split() function.)

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


email_ids = []
usernames = []
domains = []
for i in range(n):
email_id = input(f"Enter email ID for student {i+1}:")
email_ids.append(email_id)
username, domain = email_id.split('@')
usernames.append(username)
domains.append(domain)

email_ids_tuple = tuple(email_ids)
usernames_tuple = tuple(usernames)
domains_tuple = tuple(domains)
print("Email IDs Tuple:", email_ids_tuple)
print("Usernames Tuple:", usernames_tuple)
print("Domains Tuple:", domains_tuple)

Output:

Enter the number of students: 3


Enter email ID for student 1: [email protected]
Enter email ID for student 2: [email protected]
Enter email ID for student 3: [email protected]
Email IDs Tuple: ('[email protected]', '[email protected]',
'[email protected]')
Usernames Tuple: ('abc', 'zyx', 'pqr')
Domains Tuple: ('gmail.com', 'yahoo.com', 'rediffmail.com')
23. Write a program to input names of n students and store
them in a tuple. Also, input a name from the user and check if
this student is present in the tuple.

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("\n The names entered in the Tuple are:")
print(name)
search = input("\n Enter 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? 5


> Ashwini
> Sreedevi
> Lavanyashree
> Pavithra
> Aditi
The names entered in the Tuple are:
('Ashwini', 'Sreedevi', 'Lavanyashree', 'Pavithra', 'Aditi')
Enter the name of the student you want to search? Pavithra
The name Pavithra is present in the Tuple
2) Enter a name to check if the student is present: Pavithra
Pavithra is present in the list of students.
How many names do you want to enter? 3
> Ashwini
> Lavanyashree
> Sreedevi
The names entered in the Tuple are:
('Ashwini', 'Lavanyashree', 'Sreedevi')
Enter the name of the student you want to search? Aditi
The name Aditi is not found in the Tuple
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}

input_string = input("Enter a string: ")


char_counts = {}
for char in input_string:
char_counts[char] = char_counts.get(char, 0) + 1
print("Input String:", input_string)
print("Character Counts:", char_counts)

Output:

Enter a string: 2nd puc course


Input String: 2nd puc course
Character Counts: {'2': 1, 'n': 1,'d': 1, ' ': 2, 'p': 1, 'u': 2, 'c': 2, 'o': 1, 'r': 1,
's': 1, 'e': 1}

You might also like