Open In App

Python Program to Convert Temperatures using Classes

Last Updated : 20 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Temperature conversion is a common task in various fields, and Python provides a versatile platform for creating programs to handle such conversions. In this article, we will explore how to create a temperature converter using classes in Python for four different conversions: Celsius to Fahrenheit, Fahrenheit to Celsius, Kelvin to Celsius, and Celsius to Kelvin.

Python Program To Convert Temperatures Using Classes

below, are the Python programs to Convert Temperatures Using Classes in Python.

Python Program To Convert Celsius to Fahrenheit

In this example, This code defines a `CelsiusToFahrenheitConverter` class with a `convert` method for converting Celsius to Fahrenheit. An instance of the class is created, and the `convert` method is used to convert 25 Celsius to Fahrenheit, with the result printed as output. The class encapsulates the conversion functionality.

Python




class CelsiusToFahrenheitConverter:
    def convert(self, celsius):
        return (celsius * 9/5) + 32
 
# Example usage
celsius_value = 25
converter = CelsiusToFahrenheitConverter()
fahrenheit_value = converter.convert(celsius_value)
print(f"{celsius_value} Celsius is equal to {fahrenheit_value} Fahrenheit")


Output

25 Celsius is equal to 77.0 Fahrenheit

Python Program To Convert Fahrenheit to Celsius

In this article, code defines a `FahrenheitToCelsiusConverter` class with a `convert` method for converting Fahrenheit to Celsius. An instance of the class is created, and the `convert` method is used to convert 77 Fahrenheit to Celsius, with the result printed as output. The class encapsulates the conversion logic.

Python




class FahrenheitToCelsiusConverter:
    def convert(self, fahrenheit):
        return (fahrenheit - 32) * 5/9
 
# Example usage
fahrenheit_value = 77
converter = FahrenheitToCelsiusConverter()
celsius_value = converter.convert(fahrenheit_value)
print(f"{fahrenheit_value} Fahrenheit is equal to {celsius_value} Celsius")


Output

77 Fahrenheit is equal to 25.0 Celsius

Python Program To Convert Kelvin to Celsius

In this example, code defines a `KelvinToCelsiusConverter` class with a `convert` method for converting Kelvin to Celsius. An instance of the class is created, and the `convert` method is used to convert 300 Kelvin to Celsius, with the result printed as output. The class structure enhances code organization.

Python




class KelvinToCelsiusConverter:
    def convert(self, kelvin):
        return kelvin - 273.15
 
# Example usage
kelvin_value = 300
converter = KelvinToCelsiusConverter()
celsius_value = converter.convert(kelvin_value)
print(f"{kelvin_value} Kelvin is equal to {celsius_value} Celsius")


Output

300 Kelvin is equal to 26.850000000000023 Celsius

Python Program To Convert Celsius to Kelvin

In this example, code defines a `CelsiusToKelvinConverter` class with a `convert` method for converting Celsius to Kelvin. An instance of the class is created, and the `convert` method is used to convert 25 Celsius to Kelvin, with the result printed as output. The class structure encapsulates the conversion functionality.

Python




class CelsiusToKelvinConverter:
    def convert(self, celsius):
        return celsius + 273.15
 
# Example usage
celsius_value = 25
converter = CelsiusToKelvinConverter()
kelvin_value = converter.convert(celsius_value)
print(f"{celsius_value} Celsius is equal to {kelvin_value} Kelvin")


Output

25 Celsius is equal to 298.15 Kelvin

Conclusion

In conclusion, the Python program utilizing classes for temperature conversions provides a modular and organized approach to handling various temperature scales. Each class encapsulates a specific conversion logic, enhancing code readability and reusability. This object-oriented design allows for easy integration into larger projects and facilitates the maintenance of temperature conversion functionality. Overall, using classes in Python for temperature conversions promotes a structured and scalable solution.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads