PYTHON PROGRAMS
1) Write a Python program that calculates the simple interest based on principal, rate, and time
entered by the user.
Code:
principal = float(input("Enter principal amount: "))
rate = float(input("Enter rate of interest: "))
time = float(input("Enter time period in years: "))
interest = (principal * rate * time) / 100
print(f"Simple interest is: {interest}")
2) Write a Python program that converts temperature from Celsius to Fahrenheit and vice versa.
Code:
choice = input("Enter 'C' to convert from Celsius to Fahrenheit or 'F' to convert from Fahrenheit to Celsius:
").upper()
if choice == 'C':
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}°C is equal to {fahrenheit}°F")
elif choice == 'F':
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5/9
print(f"{fahrenheit}°F is equal to {celsius}°C")
else:
print("Invalid choice")
3) Write a Python program that takes two numbers as input from the user and prints their sum.
Code:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
sum = num1 + num2
print("Sum of the two numbers is:", sum)
4) Write a Python program that checks if a number entered by the user is even or odd.
Code:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(f"{num} is an even number.")
else:
print(f"{num} is an odd number.")
5) Write a Python program to find the factorial of a number entered by the user.
Code:
num = int(input("Enter a number: "))
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f"Factorial of {num} is {factorial}.")
6) Write a Python program that checks whether a number entered by the user is prime or not.
Code:
num = int(input("Enter a number: "))
if num > 1:
for i in range(2, num):
if num % i == 0:
print(f"{num} is not a prime number.")
break
else:
print(f"{num} is a prime number.")
else:
print(f"{num} is not a prime number.")
7) Write a Python program that checks whether a string entered by the user is a palindrome.
Code:
string = input("Enter a string: ")
if string == string[::-1]:
print(f"{string} is a palindrome.")
else:
print(f"{string} is not a palindrome.")
8) Write a Python program to display the Fibonacci sequence up to the nth term.
Code:
n = int(input("Enter the number of terms: "))
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
9) Write a Python program that takes three numbers as input and finds the largest one.
Code:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
num3 = float(input("Enter third number: "))
largest = max(num1, num2, num3)
print(f"The largest number is {largest}.")
10) Write a Python program that counts the number of vowels in a string entered by the user.
Code:
string = input("Enter a string: ")
vowels = 'aeiouAEIOU'
count = 0
for char in string:
if char in vowels:
count += 1
print(f"Number of vowels in the string is {count}.")
11) Write a Python program to check if a number entered by the user is an Armstrong number.
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(f"{num} is an Armstrong number.")
else:
print(f"{num} is not an Armstrong number.")
12) Write a Python program that counts how many times a specific element appears in a list.
Code:
numbers = [1, 2, 3, 4, 1, 2, 1, 5, 6, 1]
element = 1
count = [Link](element)
print(f"The element {element} appears {count} times in the list.")
13) Write a Python program that finds the largest and smallest element in a list.
Code:
numbers = [12, 4, 56, 7, 89, 23]
largest = max(numbers)
smallest = min(numbers)
print("Largest element:", largest)
print("Smallest element:", smallest)
14) Write a Python program that creates a dictionary with key-value pairs and then accesses the
values using keys.
Code:
# Creating a dictionary
person = {
"name": "Alice",
"age": 25,
"city": "New York"
}
# Accessing values using keys
print("Name:", person["name"])
print("Age:", person["age"])
print("City:", person["city"])
15) Write a Python program that adds a new key-value pair to an existing dictionary, or updates an
existing key-value pair.
Code:
# Creating a dictionary
person = {
"name": "Bob",
"age": 30
}
# Adding a new key-value pair
person["city"] = "Los Angeles"
print("Updated Dictionary:", person)
# Updating an existing key-value pair
person["age"] = 31
print("After updating age:", person)