0% found this document useful (0 votes)
12 views13 pages

Practice Program For Annual Practical Exam

The document outlines a practice program for an annual practical exam, consisting of various Python programming tasks. These tasks include converting decimal numbers to different formats, calculating BMI, checking number parity, string manipulations, list operations, and dictionary manipulations. Each task is accompanied by sample code demonstrating the required functionality.

Uploaded by

waghmodemegha
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)
12 views13 pages

Practice Program For Annual Practical Exam

The document outlines a practice program for an annual practical exam, consisting of various Python programming tasks. These tasks include converting decimal numbers to different formats, calculating BMI, checking number parity, string manipulations, list operations, and dictionary manipulations. Each task is accompanied by sample code demonstrating the required functionality.

Uploaded by

waghmodemegha
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/ 13

Practice Program for Annual Practical Exam

Based on Conditional statement


Q.1] Write a Python program that allows the user to convert a
decimal number into binary, octal, or hexadecimal format based on
their choice. The program should display a menu and prompt the
user for input.
Code
print("\nDecimal Conversion Program")
print("1. Convert decimal to binary")
print("2. Convert decimal to octal")
print("3. Convert decimal to hexadecimal")
print("4. Exit")
choice = input("Enter your choice (1/2/3/4): ")
if choice == '1':
decimal_num = int(input("Enter a positive decimal number: "))
binary_num = bin(decimal_num)[2:]
print("Binary:", binary_num)
elif choice == '2':
decimal_num = int(input("Enter a positive decimal number: "))
octal_num = oct(decimal_num)[2:]
print("Octal:", octal_num)
elif choice == '3':
decimal_num = int(input("Enter a positive decimal number: "))
hex_num = hex(decimal_num)[2:]
print("Hexadecimal:", hex_num)
elif choice == '4':
print("Exiting the program...")
else:
print("Invalid choice")
Q.2] Write a Python program that calculates the BMI based on user input for
weight (in kilograms) and height (in meters). Display the BMI value and classify
it according to the following categories:
Underweight: BMI < 18.5
Normal weight: 18.5 ≤ BMI < 24.9
Overweight: 25 ≤ BMI < 29.9
Obesity: BMI ≥ 30

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!"

# 1. Replace all occurrences of the letter 'e' with '%'


replaced_str = str1.replace('e', '%')

# 2. Display the string in uppercase


uppercase_str = str1.upper()

# 3. Check if the string starts with the word "Data"


starts_with_data = str1.startswith("Data")

# 4. Count how many times the letter 't' appears in the string
count_t = str1.count('t')

# 5. Check if the string contains only alphabets


only_alphabets = str1.isalpha()

# Output the results


print("String with 'e' replaced by '%':", replaced_str)
print("String in uppercase:", uppercase_str)
print("Does the string start with 'Data'?", starts_with_data)
print("Count of letter 't':", count_t)
print("Does the string contain only alphabets?", only_alphabets)
Q.2] Write a Python program that performs the following operations on the
given string using built-in functions and methods.
"OpenAI GPT"
1. Extract Every Other Character from the String
2. Display the position of the substring "GPT".
3. Convert the string to title case.
4. Remove all spaces from the string.
5. Check if the string contains the word "AI".
Code
# Given string
str1 = "OpenAI GPT"

# 1. Extract Every Other Character from the String


every_other_char = str1[::2]

# 2. Display the position of the substring "GPT"


position_gpt = str1.find("GPT")

# 3. Convert the string to title case


title_case_str = str1.title()

# 4. Remove all spaces from the string


no_space_str = str1.replace(" ", "")

# 5. Check if the string contains the word "AI"


contains_ai = "AI" in str1

# Output the results


print("Every Other Character from the String:", every_other_char)
print("Position of 'GPT':", position_gpt)
print("Title Case String:", title_case_str)
print("String without spaces:", no_space_str)
print("Contains 'AI':", contains_ai)
Q.3] Write a Python program that performs the following operations on the
given string:
"Artificial Intelligence is the future of technology!"
1. Partition the String Based on the Word "is"
2. Split the String into Words
3. Join the Words with a Dash ("-"):
4. Check if the String Contains the Word "future":
5. Extract characters from index 5 to 15

Code
# Given string
str = "Artificial Intelligence is the future of technology!"

# 1. Partition the String Based on the Word "is"


partitioned_str = str.partition("is")
print("Partitioned based on 'is':", partitioned_str)

# 2. Split the String into Words


words = str.split()
print("Split into words:", words)

# 3. Join the Words with a Dash ("-")


joined_str = "-".join(words)
print("Words joined with a dash:", joined_str)

# 4. Check if the String Contains the Word "future"


contains_future = "future" in str
print("Contains the word 'future':", contains_future)

# 5. Slice the String (Extract a Portion of the String)

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]

# i. Find the number of times 10 is written in the List L


count_10 = L.count(10)
print("Number of times 10 is written:", count_10)

# ii. Find the Maximum Number from the List L


max_value = max(L)
print("Maximum number in the list:", max_value)

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

# iv. Arrange the List in Ascending Order


L.sort()
print("List sorted in ascending order:", L)

# v. Delete the first occurrence of 50 in the List


L.remove(50)
print("List after removing first occurrence of 50:", L)
Q.2] Consider the list given below and perform the following operation using
built-in functions/methods
tech = ["Quantum Computing", "Artificial Intelligence", "Blockchain", "Internet
of Things (IoT)"]
1. Append a technology Virtual Reality to the list
2. Insert 'Augmented Reality' at index 2
3. Extract a sublist (slice) containing the first two technologies
4. Reverse the list using
5.Remove the last element from the list

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"]

# 1. Add "Ransomware" to the end of the list


cyber_terms.append("Ransomware")
print("After adding 'Ransomware':", cyber_terms)

# 2. Remove the last item from the list


removed_term = cyber_terms.pop()
print("After removing the last term:", cyber_terms)
print("Removed term:", removed_term)

# 3. Access the second element from the list (index 1)


second_term = cyber_terms[1]
print("Accessing the second term:", second_term)

# 4. Modify an element ("Firewall") to "Network Security" (index 2)


cyber_terms[2] = "Network Security"
print("After modifying 'Firewall' to 'Network Security':", cyber_terms)

# 5. Extract the first three terms from the list


sublist = cyber_terms[:3]
print("Extracted sublist containing the first three terms:", sublist)
Based on dictionary
Q.1] Consider the following dictionary:
countries = {'IN': 'India', 'US': 'United States', 'UK': 'United Kingdom', 'CA':
'Canada'}
Perform the following operations:
i) Insert ('AU': 'Australia') into the dictionary countries.
ii) Return the value corresponding to the key 'US'.
iii) Return the length of the dictionary countries.
iv) Delete the item from the dictionary corresponding to the key 'UK'

Code
# Initial dictionary
countries = {'IN': 'India', 'US': 'United States', 'UK': 'United Kingdom', 'CA':
'Canada'}

# i) Insert ('AU': 'Australia') into the dictionary


countries['AU'] = 'Australia'
print("After inserting ('AU': 'Australia'):", countries)

# ii) Return the value corresponding to the key 'US'


value_us = countries['US']
print("Value corresponding to the key 'US':", value_us)

# iii) Return the length of the dictionary


length = len(countries)
print("Length of the dictionary:", length)

# 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'}

# i) Add the new student ('S105': 'Eva') to the dictionary


students['S105'] = 'Eva'
print("After adding ('S105': 'Eva'):", students)

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

# iii) Determine the number of student records in the dictionary


num_students = len(students)
print("Number of student records:", num_students)

# iv) Remove the student record for 'S102'


removed_student = students.pop('S102')
print("After removing the student with ID 'S102':", students)
print("Removed student record:", removed_student)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy