Python Program to Convert Temperatures using Classes
Last Updated :
20 Feb, 2024
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.
Similar Reads
Python program to convert meters in yards and vice versa
Given the distance in either meter or yard, the task here is to generate a Python program that converts distance given in meters to yards and vice versa. Examples: Input: Length in Meter: 245 Length in Yard : 100 Output: 245 Meter in Yard = 267.9344 100 Yards in Meter = 91.4403 Input: Length in Mete
2 min read
Python Program to Calculate Gross Salary of a Person
The task of calculating the Gross Salary of a Person in Python involves taking the basic salary and grade as input, applying the necessary calculations for salary components and displaying the final salary. The gross salary formula: Gross Salary = Basic Salary + HRA + DA + Allowance - PF Where: HRA
3 min read
Python program to convert int to exponential
Given a number of int type, the task is to write a Python program to convert it to exponential. Examples: Input: 19 Output: 1.900000e+01 Input: 2002 Output: 2.002000e+03 Input: 110102 Output: 1.101020e+05Approach: We will first declare and initialise an integer numberThen we will use format method t
1 min read
Convert Celsius To Fahrenheit - Python
We are given the temperature in Celsius and our task is to convert the temperature value in the Fahrenheit scale and display it. To convert the temperature from Celsius to Fahrenheit or vice versa in Python firstly we will take the input in Celsius or Fahrenheit and then convert that temperature to
2 min read
Python program to convert float to exponential
Given a float number, the task is to write a Python program to convert float to exponential. Examples: Input: 19.0 Output: 1.900000e+01 Input: 200.2 Output: 2.002000e+02 Input: 1101.02 Output: 1.101020e+03Approach: We will first declare and initialise a float number.Then we will use format method to
1 min read
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. Python is: A high-level language, used in web development, data science, automat
10 min read
Python program to convert exponential to float
Given a number in exponential format, the task is to write a Python program to convert the number from exponential format to float. The exponential number is a way of representing a number. Examples: Input: 1.900000e+01 Output: 19.0 Input: 2.002000e+03 Output: 2002.0 Input: 1.101020e+05 Output: 1101
1 min read
Python Program to calculate sum and average of three numbers
We are given three numbers, and our task is to calculate the sum and average of these numbers. The average of the numbers is defined as the sum of the given numbers divided by the total number of elements. In this case, it will be the sum of given three numbers divided by 3. Example: Input: 10, 20,
3 min read
Create Class Objects Using Loops in Python
We are given a task to create Class Objects using for loops in Python and return the result, In this article we will see how to create class Objects by using for loops in Python. Example: Input: person_attributes = [("Alice", 25), ("Bob", 30), ("Charlie", 35)]Output: Name: Alice, Age: 25, Name: Bob,
4 min read
How to write memory efficient classes in Python?
Memory efficiency is a critical aspect of software development, especially when working with resource-intensive applications. In Python, crafting memory-efficient classes is essential to ensure optimal performance. In this article, we'll explore some different methods to write memory-efficient class
2 min read