Practice Program For Annual Practical Exam
Practice Program For Annual Practical Exam
Code
weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in meters: "))
bmi = weight / (height ** 2)
print("Your BMI is:", round(bmi,2))
if bmi < 18.5:
print("You are underweight.")
elif 18.5 <= bmi < 24.9:
print("You have a normal weight.")
elif 25 <= bmi < 29.9:
print("You are overweight.")
else:
print("You have obesity.")
Q.3] Write a Python program that takes two integers as input and displays the
difference between them based on their parity (even or odd). The program
should display different messages depending on the following conditions:
• If both numbers are even, display:
"Difference between both numbers is [difference]"
(where [difference] is the absolute difference).
• If both numbers are odd, display:
"Difference between both numbers is [difference]".
• If one number is even and the other is odd, display:
"Difference is not even".
Code
num1 = int(input("Enter the first number: "))
num2 = int(input("Enter the second number: "))
if num1 % 2 == 0 and num2 % 2 == 0:
print("Difference between both numbers is", abs(num1 - num2))
elif num1 % 2 != 0 and num2 % 2 != 0:
print("Difference between both numbers is", abs(num1 - num2))
else:
print("Difference is not even")
Q.4] Write a program that asks the user to input a single character and checks:
• If it is an alphabet, print "Alphabet".
• If it is a digit, print "Digit".
• If it is a special character, print "Special Character".
Code
char = input("Enter a single character: ")
if char.isalpha():
print("Alphabet")
elif char.isdigit():
print("Digit")
else:
print("Special Character")
Based on Looping statement
Q.1] Write Python program using for loop to print Fibonacci series for n terms.
Code
print("*****Fibonacci Series*****")
n=int(input("Enter no.of terms required:"))
a=1
b=1
print(a,b,end=' ')
for i in range(1,n-1):
c=a+b
print(c,end=' ')
a=b
b=c
Q.2] Write Python program using loop to check if a 3-digit number taken as
input from the user is Armstrong number or not.
Code
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** 3
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
Q.3] Write a Python program using for loop to print the following triangle
pattern
8
86
864
8642
Code
row=int(input("Enter the no.of rows:"))
for i in range(1,row+1):
val=8
for j in range(1,i+1):
print(val,end='')
val=val-2
print()
Q.4] Write a Python program using for loop to print the following triangle
pattern
*****
****
***
*
Code
row=int(input("Enter the no.of rows:"))
for i in range(1,row+1):
for j in range(row,i-1,-1):
print("*",end=' ')
print()
Based on String
Q.1] Write a Python program that performs the following operations on the
given string using built-in functions and methods.
"Data Science is the future of technology!"
1. Replace all occurrences of the letter 'e' with '%'.
2. Display the string in uppercase.
3. Check if the string starts with the word "Data".
4. Count how many times the letter 't' appears in the string.
5. Check if the string contains only alphabets.
Code
# Given string
str1 = "Data Science is the future of technology!"
# 4. Count how many times the letter 't' appears in the string
count_t = str1.count('t')
Code
# Given string
str = "Artificial Intelligence is the future of technology!"
sliced_str = str[5:15]
print("Sliced portion of the string (from index 5 to 15):", sliced_str)
Based on List
Q.1] Consider the list given below and perform the following operation using
built-in functions/methods
L = [150,50,99,73,14,10,22,46,3,10,60,50,10]
i. Find the number of times 10 is written in the List L
ii. Find the Maximum Number from the List L
iii. Add all the numbers in the List and Print the total
iv. Arrange the List in Assending Order.
v. Delete the first occurrence of 50 in the List.
Code
L = [150, 50, 99, 73, 14, 10, 22, 46, 3, 10, 60, 50, 10]
# iii. Add all the numbers in the List and Print the total
total_sum = sum(L)
print("Total sum of the numbers in the list:", total_sum)
Code
# Initial list of technologies
tech = ["Quantum Computing", "Artificial Intelligence", "Blockchain", "Internet
of Things (IoT)"]
# 1. Append 'Virtual Reality' to the list
tech.append("Virtual Reality")
print("After appending 'Virtual Reality':", tech)
# 2. Insert 'Augmented Reality' at index 2
tech.insert(2, "Augmented Reality")
print("After inserting 'Augmented Reality' at index 2:", tech)
# 3. Extract a sublist (slice) containing the first two technologies
sublist = tech[:2]
print("First two technologies:", sublist)
# 4. Reverse the list
tech.reverse()
print("List after reversing:", tech)
# 5. Remove the last element from the list
tech.pop() # Removes the last element
print("List after removing the last element:", tech)
Q.3] Consider the list given below and perform the following operation using
built-in functions/methods
cyber_terms = ["Phishing", "Malware", "Firewall", "Encryption"]
1. Add a Ransomware to the end of the list.
2. Remove the last item from the list.
3. Access the second element from the list
4. Modify an element Firewall to 'Network Security
5. Extract the first three terms from the list.
Code
# Initial list
cyber_terms = ["Phishing", "Malware", "Firewall", "Encryption"]
Code
# Initial dictionary
countries = {'IN': 'India', 'US': 'United States', 'UK': 'United Kingdom', 'CA':
'Canada'}
# iv) Delete the item from the dictionary corresponding to the key 'UK'
removed_item = countries.pop('UK')
print("After deleting the item with key 'UK':", countries)
print("Removed item:", removed_item)
Q.2] Consider the following dictionary:
students = {'S101': 'Alice', 'S102': 'Bob', 'S103': 'Charlie', 'S104': 'David'}
Perform the following operations:
i) Add the new student ('S105': 'Eva') to the dictionary students.
ii) Access and print the name of the student with ID 'S103'.
iii) Determine the number of student records in the dictionary.
iv) Remove the student record for 'S102'.
Code
# Initial dictionary
students = {'S101': 'Alice', 'S102': 'Bob', 'S103': 'Charlie', 'S104': 'David'}
# ii) Access and print the name of the student with ID 'S103'
student_s103 = students['S103']
print("Name of the student with ID 'S103':", student_s103)