Python Practice Program (1)
Python Practice Program (1)
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius
# Main program
print("Temperature Converter")
choice = input("Choose conversion type:\n1. Celsius to Fahrenheit\n2. Fahrenheit
to Celsius\nEnter 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")
else:
print("Invalid choice!")
4. Write a Python program to get the Fibonacci series between 0 and 50.
Note : The Fibonacci Sequence is the series of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Ans:
6. Write a Python program to count the number of strings from a given list
of strings. The string length is 2 or more and the first and last characters
are the same.
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2
Ans:
def count_strings(strings):
count = 0
for string in strings:
if len(string) >= 2 and string[0] == string[-1]:
count += 1
return count
# Sample list
sample_list = ['abc', 'xyz', 'aba', '1221']
def remove_duplicates(lst):
return list(set(lst))
# Sample list with duplicates
my_list = [1, 2, 2, 3, 4, 4, 5, 6, 6]
# Removing duplicates
unique_list = remove_duplicates(my_list)
# Sample dictionary
my_dict = {'apple': 50, 'banana': 20, 'cherry': 30, 'date': 10}
# Sample dictionary
my_dict = {'apple': 50, 'banana': 20, 'cherry': 30}
# Key to be removed
key_to_remove = 'banana'
11. Write a Python program to get the maximum and minimum values of a
dictionary.
Ans:
# Sample dictionary
my_dict = {'apple': 50, 'banana': 20, 'cherry': 30, 'date': 40}