Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Conditional Statements

Convert temperature from Celsius to Fahrenheit and categorize (hot/cold) - Python Program

Example 1 :

celsius = float(input("Enter temperature in Celsius: ")) fahrenheit = (celsius * 9/5) + 32 print("Fahrenheit:", fahrenheit) if fahrenheit > 95: print("It's hot") elif fahrenheit < 50: print("It's cold") else: print("Moderate")

Output

 
OUTPUT 1 :
Enter temperature in Celsius: 43
Fahrenheit: 109.4
It's hot

OUTPUT 2 :
Enter temperature in Celsius: 12
Fahrenheit: 53.6
Moderate

OUTPUT 3 :
Enter temperature in Celsius: 2
Fahrenheit: 35.6
It's cold

Example 2 : Advanced Program

def celsius_to_fahrenheit(celsius): """Converts temperature from Celsius to Fahrenheit.""" fahrenheit = (celsius * 9/5) + 32 return fahrenheit def categorize_temperature(fahrenheit_temp): """Categorizes temperature as 'Hot' or 'Cold'.""" if fahrenheit_temp >= 70: # Example threshold for 'Hot' return "Hot" else: return "Cold" # Get input from the user try: celsius_input = float(input("Enter temperature in Celsius: ")) # Convert to Fahrenheit fahrenheit_output = celsius_to_fahrenheit(celsius_input) # Categorize the temperature category = categorize_temperature(fahrenheit_output) # Display the results print(f"\n{celsius_input}°C is equal to {fahrenheit_output:.2f}°F") print(f"The temperature is categorized as: {category}") except ValueError: print("Invalid input. Please enter a numerical value for temperature.")

Output

 
OUTPUT 1 :
Enter temperature in Celsius: 43

43.0°C is equal to 109.40°F
The temperature is categorized as: Hot

OUTPUT 2 :
Enter temperature in Celsius: 12

12.0°C is equal to 53.60°F
The temperature is categorized as: Cold