def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
print("Temperature Converter")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
choice = input("Choose an option (1 or 2): ")
if choice == '1':
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equal to {fahrenheit:.2f}°F")
elif choice == '2':
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit}°F is equal to {celsius:.2f}°C")
else:
print("Invalid choice. Please choose 1 or 2.")
Python 2: Student Marklist
class StudentData:
def __init__(self):
self.total = 0.00
self.average = 0.00
self.percentage = 0.00
self.marks = []
self.max_marks = 0.00
def main(self):
print("Enter the mark of five subjects:")
for i in range(5):
self.marks.append(int(input()))
def calc_max_marks(self):
self.max_marks = len(self.marks) * 100
return self.max_marks
def calc_total(self):
self.total = sum(self.marks)
return self.total
def calc_average(self):
self.average = self.calc_total() / len(self.marks)
return self.average
def calc_percentage(self):
self.percentage = (self.calc_total() / self.calc_max_marks()) * 100
return self.percentage
def calc_grade(self):
if self.average >= 90:
return 'A'
elif self.average >= 80:
return 'B'
elif self.average >= 70:
return 'C'
elif self.average >= 60:
return 'D'
else:
return 'E'
def calc_result(self):
passed = all(mark > 40 for mark in self.marks)
return "Passed" if passed else "Failed"
if __name__ == "__main__":
student_data = StudentData()
student_data.main()
print("\n The Total Marks:\t", student_data.calc_total(), "/", student_data.calc_max_marks())
print("\n The Average is:\t", student_data.calc_average())
print("\n The Percentage is:\t", student_data.calc_percentage(), "%")
print("\n The Grade is:\t", student_data.calc_grade())
print("\n The result is:\t", student_data.calc_result())
Python 3: Area Calculation
import math
def cal_area_rec(length, width):
return length * width
def cal_area_sqa(side):
return side ** 2
def cal_area_cir(radius):
return math.pi * radius ** 2
def cal_area_tri(base, height):
return 0.5 * base * height
def main():
print("Enter the shape to find the area:")
print("\n1. Rectangle\n2. Square\n3. Circle\n4. Triangle")
choice = int(input("Enter your choice: "))
if choice == 1:
length = float(input("Enter the length of the rectangle: "))
width = float(input("Enter the width of the rectangle: "))
area = cal_area_rec(length, width)
print("Area of rectangle =", area)
elif choice == 2:
side = float(input("Enter the side of the square: "))
area = cal_area_sqa(side)
print("Area of Square =", area)
elif choice == 3:
radius = float(input("Enter the radius of the circle: "))
area = cal_area_cir(radius)
print("Area of Circle =", area)
elif choice == 4:
base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))
area = cal_area_tri(base, height)
print("Area of Triangle =", area)
else:
print("Invalid Choice")
if __name__ == "__main__":
main()
Program 4: FactoriaL
def recursive_factorial(n):
if n == 0 or n == 1:
return 1
return n * recursive_factorial(n - 1)
number = int(input("Enter a number to find its factorial: "))
if number < 0:
print("Factorial of a negative number is not defined")
else:
print(f"The factorial of {number} is {recursive_factorial(number)}")
Program 5: Odd or even
def count_odd_even(numbers):
even_count = sum(1 for num in numbers if num % 2 == 0)
odd_count = len(numbers) - even_count
return even_count, odd_count
N = int(input("Enter the number of elements in the array: "))
numbers = [int(input(f"Enter number {i+1}: ")) for i in range(N)]
even_count, odd_count = count_odd_even(numbers)
print(f"Number of even numbers: {even_count}")
print(f"Number of odd numbers: {odd_count}")
Program 6: Pattern
rows = 5
for i in range(1, rows + 1):
print('* ' * i)
for i in range(rows - 1, 0, -1):
print('* ' * i)
import time
name = input("What is your name? ")
print("Hello", name, "Time to play Hangman!")
time.sleep(1)
print("Start guessing...")
time.sleep(1)
word = "Secret"
guesses = ""
turns = 10
while turns > 0:
failed = 0
guess = input("Guess a character: ")
guesses += guess
for char in word:
if char in guesses:
print(char, end=" ")
else:
print("_", end=" ")
failed += 1
print()
if failed == 0:
print("You Won!")
break
turns -= 1
print(f"Wrong! You have {turns} more guesses left.")
if turns == 0:
print("You Lose!")
# Program 10: Count Tuples
Tuple_data = ('a', 'a', 'c', 'b', 'd')
List_data = ['a', 'b']
Total_count = sum(Tuple_data.count(item) for item in List_data)
print("Occurrences of all items of the list in the tuple is", Total_count)