Python programming lab practical
questions with Solutions (SEM -1)
1. Write a Python program to check whether a number
is even or odd.
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
2. Write a Python program to check whether a number
is positive, negative, or zero.
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
3. Write a Python program to find the largest of three
numbers entered by the user.
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)
4. Write a Python program to check whether a given
year is a leap year or not.
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 100 != 0 and year %
4 == 0):
print("Leap Year")
else:
print("Not a Leap Year")
5. Write a Python program to calculate the sum of
digits of a given number.
num = int(input("Enter a number: "))
total = 0
while num > 0:
digit = num % 10
total += digit
num //= 10
print("Sum of digits:", total)
6. Write a Python program to check whether a given
number is a palindrome.
num = int(input("Enter a number: "))
temp = num
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num //= 10
if temp == rev:
print("Palindrome")
else:
print("Not Palindrome")
7. Write a Python program to generate the Fibonacci
sequence up to n terms.
n = int(input("Enter number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
8. Write a Python program to print the multiplication
table of a number using a loop.
num = int(input("Enter a number: "))
for i in range(1, 11):
print(num, "x", i, "=", num * i)
9. Write a Python program to display numbers from 1
to 10 but skip the number 5 using the continue
statement.
for i in range(1, 11):
if i == 5:
continue
print(i)
10. Write a Python program to print numbers from 1
to 20, but stop printing if the number becomes
greater than 12 using the break statement.
for i in range(1, 21):
if i > 12:
break
print(i)
11. Write a Python program that takes a string from
the user and counts the number of vowels.
text = input("Enter a string: ").lower()
vowels = "aeiou"
count = 0
for ch in text:
if ch in vowels:
count += 1
print("Number of vowels:", count)
12. Write a Python function to calculate the factorial
of a number using recursion.
def factorial(n):
if n == 0 or n == 1:
return 1
return n * factorial(n - 1)
num = int(input("Enter a number: "))
print("Factorial:", factorial(num))
13. Write a Python function that checks whether a
number is prime or not.
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
num = int(input("Enter a number: "))
if is_prime(num):
print("Prime")
else:
print("Not Prime")
14. Write a Python function that takes a list of
numbers as input and returns the sum of all
numbers.
def sum_list(numbers):
return sum(numbers)
nums = [int(x) for x in input("Enter numbers separated
by space: ").split()]
print("Sum:", sum_list(nums))
15. Write a Python program to find the largest
element in a list using the built-in max() function.
nums = [int(x) for x in input("Enter numbers separated
by space: ").split()]
print("Largest element:", max(nums))
16. Write a Python program to check whether a string
entered by the user is a palindrome using a user-
defined function.
def is_palindrome(s):
return s == s[::-1]
text = input("Enter a string: ")
if is_palindrome(text):
print("Palindrome")
else:
print("Not Palindrome")
Q17. Design, Develop and Implement a menu driven
Program in Python to perform the following tasks:
a. Read three numbers and print largest of it
b. Read a number and print sum of its digits
c. Read a number and print reverse of it (without using
built-in function)
Support the program with appropriate functions for each
of the above tasks.
# Function to find largest of three numbers
def find_largest(a, b, c):
if a >= b and a >= c:
return a
elif b >= a and b >= c:
return b
else:
return c
# Function to find sum of digits of a number
def sum_of_digits(num):
total = 0
while num > 0:
digit = num % 10
total = total + digit
num = num // 10
return total
# Function to reverse a number
def reverse_number(num):
rev = 0
while num > 0:
digit = num % 10
rev = rev * 10 + digit
num = num // 10
return rev
# Main Program with Menu
while True:
print("\n--- MENU ---")
print("1. Find Largest of Three Numbers")
print("2. Sum of Digits of a Number")
print("3. Reverse a Number")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
x = int(input("Enter first number: "))
y = int(input("Enter second number: "))
z = int(input("Enter third number: "))
print("Largest number is:", find_largest(x, y, z))
elif choice == 2:
n = int(input("Enter a number: "))
print("Sum of digits:", sum_of_digits(n))
elif choice == 3:
n = int(input("Enter a number: "))
print("Reverse of number:",
reverse_number(n))
elif choice == 4:
print("Exiting program...")
break
else:
print("Invalid choice! Please try again.")
Q18.A university portal allows students to check their
CGPA. The system takes total marks and total credits as
input.
• If the user enters text instead of numbers, the program
should handle it.
• If credits entered are 0, handle division by zero.
Finally, display the CGPA rounded to 2 decimals.
try:
total_marks = float(input("Enter total marks: "))
total_credits = float(input("Enter total credits: "))
if total_credits == 0:
print("Error: Credits cannot be zero.")
else:
cgpa = total_marks / total_credits
print("Your CGPA is:", round(cgpa, 2))
except ValueError:
print("Error: Please enter numbers only.")
Q19. Design a custom Python module named math_utils
that contains the following functions:
• is_prime(n): Check if a number is prime.
• factorial(n): Compute the factorial of a number using
recursion.
• convert_to_binary(n): Convert a number into its binary
representation.
Then, in your main program:
• Take a number as input from the user.
• Use functions from your module to check if the number
is prime, find its factorial, and display its binary
representation.
Also, handle exceptions if the user enters invalid (non-
integer or negative) values.
# Function to check if a number is prime
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Function to calculate factorial using recursion
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Function to convert a number into binary
def convert_to_binary(n):
if n == 0:
return "0"
binary = ""
while n > 0:
binary = str(n % 2) + binary
n = n // 2
return binary
# Main Program
try:
num = int(input("Enter a positive integer: "))
if num < 0:
print("Error: Number must be non-negative.")
else:
print("Is Prime:", is_prime(num))
print("Factorial:", factorial(num))
print("Binary Representation:",
convert_to_binary(num))
except ValueError:
print("Error: Please enter a valid integer.")
Q20.A restaurant billing system displays a menu with
items and prices. Develop a program that should:
I. Show food items with prices.
II. Take the user’s order (multiple items).
III. Calculate the total bill.
IV. Apply a 10% discount if the bill is above ₹500.
V. Exit option to close billing.
• Use functions for add_to_order, calculate_total,
apply_discount.
• Use match-case for menu selection.
Handle invalid inputs using exceptions.
menu = {
1: ("Pizza", 200),
2: ("Burger", 120),
3: ("Pasta", 150),
4: ("Sandwich", 100),
5: ("Cold Drink", 50)
order_list = []
def add_to_order(choice):
if choice in menu:
order_list.append(menu[choice])
print(menu[choice][0], "added.")
else:
print("Wrong choice!")
def calculate_total():
total = 0
for item in order_list:
total = total + item[1]
return total
def apply_discount(amount):
if amount > 500:
return amount - (amount * 0.1)
else:
return amount
while True:
print("\n--- MENU ---")
for key in menu:
print(key, menu[key][0], "- ₹", menu[key][1])
print("6. Show Bill and Exit")
try:
choice = int(input("Enter your choice: "))
if choice in [1, 2, 3, 4, 5]:
add_to_order(choice)
elif choice == 6:
total = calculate_total()
final = apply_discount(total)
print("\nTotal Bill: ₹", total)
print("Final Amount: ₹", round(final, 2))
break
else:
print("Please enter a valid option.")
except ValueError:
print("Enter numbers only!")
elif choice == 6:
total = calculate_total()
final = apply_discount(total)
print("\nTotal Bill: ₹", total)
print("Final Amount: ₹", round(final, 2))
break
else:
print("Please enter a valid option.")
except ValueError:
print("Enter numbers only!")