Temperature Converter – Python Program

 

 

Temperature Converter – Python Program

This Python program converts temperatures between Celsius and Fahrenheit. It includes two functions:

  • celsius_to_fahrenheit(celsius): Converts a temperature from Celsius to Fahrenheit.
  • fahrenheit_to_celsius(fahrenheit): Converts a temperature from Fahrenheit to Celsius.

Python Code


# temperature_converter.py

def celsius_to_fahrenheit(celsius):
"""
Convert Celsius to Fahrenheit.

:param celsius: Temperature in Celsius
:return: Temperature in Fahrenheit
"""
return (celsius * 9/5) + 32

def fahrenheit_to_celsius(fahrenheit):
"""
Convert Fahrenheit to Celsius.

:param fahrenheit: Temperature in Fahrenheit
:return: Temperature in Celsius
"""
return (fahrenheit - 32) * 5/9

if __name__ == "__main__":
# Example usage
celsius = 25
fahrenheit = 77

print(f"{celsius}°C is equal to {celsius_to_fahrenheit(celsius)}°F")
print(f"{fahrenheit}°F is equal to {fahrenheit_to_celsius(fahrenheit)}°C")

Explanation

This program consists of the following components:

Functions

  • celsius_to_fahrenheit(celsius): This function takes a temperature in Celsius as input and converts it to Fahrenheit using the formula (celsius * 9/5) + 32.
  • fahrenheit_to_celsius(fahrenheit): This function takes a temperature in Fahrenheit as input and converts it to Celsius using the formula (fahrenheit - 32) * 5/9.

Main Section

The if __name__ == "__main__" block demonstrates how to use these conversion functions. It initializes two example temperatures, one in Celsius and one in Fahrenheit, and then prints the converted values to the console.

 

Leave a Reply

Your email address will not be published. Required fields are marked *