Practical File
Practical File
# Calculate the year when the person will be 100 years old
current_year = 2023 # Update this to the current year
years_to_100 = 100 - age
future_year = current_year + years_to_100
2) Write a program that converts distances measured in kilometers to miles. One kilometer is
approximately 0.62 miles.
# Conversion factor
kilometers_to_miles = 0.621371
3. Write a program to perform a unit conversion of your own choosing. Make sure that the program
prints an introduction that explains what it does.
# Introduction
print("This program converts weights from pounds to kilograms.")
# Conversion factor
pounds_to_kilograms = 0.453592
4) Write an interactive Python calculator program. The program should allow the user to type a
mathematical expression, and then print the value of the expression. Include a loop so that the user can
perform many calculations (say, up to 100).
# Introduction
print("Welcome to the Interactive Python Calculator!")
print("You can type a mathematical expression to evaluate.")
print("Type 'exit' to quit the calculator.")
# Main loop
for _ in range(100):
expression = input("Enter a mathematical expression: ")
if expression.lower() == "exit":
print("Exiting the calculator.")
break
try:
result = eval(expression)
print("Result:", result)
except Exception as e:
print("Error:", e)
5) Write a program that asks the user to enter their name and their age. Print out a message addressed
to them that tells them the year that they will turn 100 years old.
# Calculate the year when the person will be 100 years old
current_year = 2023 # Update this to the current year
years_to_100 = 100 - age
future_year = current_year + years_to_100
6) Write a program with a loop so that it executes 5 times before quitting. Each time through the loop,
the program should get another temperature from the user and print the converted value.
# Loop 5 times
for _ in range(5):
# Get user input for temperature in Celsius
celsius = float(input("Enter temperature in Celsius: "))
print("Program finished.")
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
result = factorial(number)
10) Write a program to count characters in a string if character is repeated it should count only once.
# Get user input for a string
unique_characters = set()
unique_characters.add(char)
count = len(unique_characters)
11) Write a program to swap two variables without using third variables.
12) Write a program to find the sum of the first n natural numbers, where the value of n is provided by
the user.
sum_of_natural_numbers = (n * (n + 1)) // 2
13) Write a program to find the sum of the cubes of the first n natural numbers where the value of n is
provided by the user
# Get user input for the value of n
print(f"The sum of the cubes of the first {n} natural numbers is: {sum_of_cubes}")
``
def is_prime(num):
if num <= 1:
return False
if num <= 3:
return True
if num % 2 == 0 or num % 3 == 0:
return False
i=5
while i * i <= num:
if num % i == 0 or num % (i + 2) == 0:
return False
i += 6
return True
if is_prime(num):
print(num)
if choice == "odd":
if num % 2 != 0:
print(num)
if num % 2 == 0:
print(num)
else:
16) A Fibonacci sequence is a sequence of numbers where each successive number is the sum of the
previous two. The classic Fibonacci sequence begins: 1, 1, 2, 3, 5, 8, 13, write a program that computes
the nth Fibonacci number where n is a value input by the user. For example, if n = 6, then the result is 8.
17) Write a program to calculate the volume and surface area of a sphere from its radius, given as
input.
import math
18) Write a program to check weather a given number is Armstrong number (It can be for any number
of digits)
19) Write a program to check weather a given number is leap year or not
import math
21) Write a menu driven Python program that should perform the following four tasks:
(i) After getting a word (of input) from the user, your program should use a while (or for) loop to
print out each of the letters of the word. Just remember that strings in Python start with element
0.
(ii) Your program should then use another loop to print out each of the letters of the (same) word
in reverse order.
(ii) Make a new variable that is the original word in reverse and print that variable. You can do
this very easily in Python with a "string slice".
(iv) Ask the user for a letter to count. Use another loop to count how many times that letter
appears in the original word. Print out this count.
while True:
print("\nMenu:")
print("1. Print each letter of the word")
print("2. Print each letter of the word in reverse order")
print("3. Print the word in reverse")
print("4. Count the occurrence of a letter")
print("5. Exit")
if choice == 1:
print_letters(word)
elif choice == 2:
print_reverse(word)
elif choice == 3:
print_reverse_word(word)
elif choice == 4:
letter = input("Enter a letter to count: ")
count_letter(word, letter)
elif choice == 5:
print("Exiting the program.")
break
else:
print("Invalid choice. Please enter a valid option.")
22) Write a program that inputs a word and prints "Yes it starts !!" if the given word starts with "0", "1",
"2", ... , "9".
23) Carefully go through the code given below and answer the questions based on it:
print (len(newStr))#Line 1
print(newStr)#Line 2
print (large)#Line 3
print (small)#Line 4
(i) Given a first input of 12345 and a second input of 246, what result is produced by a Line 1?
(ii) Given a first input of 12345 and a second input of 246, what result is produced by Line 2?
(iii) Given a first input of 123 and a second input of 4567, what result is produced by Line 3?
(iv) Given a first input of 123 and a second input of 4567, what result is produced by Line 4?
i)3
ii)357
iii)7
iv)123
a)
8
86
864
8642
b) #######
# #
# #
# #
# #
# #
#######
c)
#######
#
#
#
#
#
#######
d)
#######
#
#
#
#
#
#######
def pattern_a():
n = int(input("Enter the number of rows: "))
for i in range(n, 0, -1):
for j in range(i, n + 1):
print(j, end="")
print()
def pattern_b():
n=7
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == 0 or j == n - 1:
print("#", end="")
else:
print(" ", end="")
print()
def pattern_c():
n=7
for i in range(n):
for j in range(n):
if i == 0 or i == n - 1 or j == i:
print("#", end="")
else:
print(" ", end="")
print()
def pattern_d():
n=7
for i in range(n):
for j in range(n):
if i == 0 or j == n - 1 or i == j:
print("#", end="")
else:
print(" ", end="")
print()
while True:
print("\nMenu:")
print("a) Pattern A")
print("b) Pattern B")
print("c) Pattern C")
print("d) Pattern D")
print("e) Exit")
if choice == 'a':
pattern_a()
elif choice == 'b':
pattern_b()
elif choice == 'c':
pattern_c()
elif choice == 'd':
pattern_d()
elif choice == 'e':
print("Exiting the program.")
break
else:
print("Invalid choice. Please enter a valid option.")