Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for Fahrenheit to Celsius conversion
In this problem, we are given a temperature value in Fahrenheit and we have to convert it to Celsius. This conversion is used in various real-world applications such as weather applications, scientific calculations, and data analysis. In this article, we are going to discuss how we can convert Fahrenheit temperature to Celsius temperature in Python.
Formula to Convert Fahrenheit to Celsius
The formula for converting Fahrenheit to Celsius is:
Celsius = (Fahrenheit - 32) / 1.8
Example 1
- Input: Fahrenheit = 98.6
- Output: Celsius = 37°C
Explanation: The given temperature is 98.6°F, we calculate:
Celsius = (98.6 - 32) / 1.8 = 37°C
Example 2
- Input: Fahrenheit = 50
- Output: Celsius = 10°C
Explanation: The given temperature is 50°F, we calculate:
Celsius = (50 - 32) / 1.8 = 10°C
Using Direct Formula
In this approach, we use the direct formula to convert the given Fahrenheit value to Celsius. We subtract 32 from the Fahrenheit value and divide the result by 1.8.
Steps for Implementation
- Take the input temperature in Fahrenheit.
- Use the formula to calculate Celsius: Celsius = (Fahrenheit?32) / 1.8
- Print the result in Celsius.
fahrenheit = 100
celsius = (fahrenheit - 32) / 1.8
print(f"{fahrenheit}°F is equal to {celsius:.2f}°C")
100°F is equal to 37.78°C
Time Complexity: O(1)
Space Complexity: O(1)
Using Custom Function
In this approach, we create a reusable function to perform the conversion. Functions make the code more organized and allow it to be reused for different inputs.
Steps for Implementation
- Create a function that takes Fahrenheit as the input parameter.
- Inside the function, use the formula: Celsius = (Fahrenheit?32) / 1.8
- Return the calculated Celsius value.
- Call the function with different input values and display the output.
def convert_fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) / 1.8
return celsius
# Test cases
test_temperatures = [98.6, 50, 32, 0]
for fahrenheit in test_temperatures:
result = convert_fahrenheit_to_celsius(fahrenheit)
print(f"{fahrenheit}°F is {result:.2f}°C")
98.6°F is 37.00°C 50.0°F is 10.00°C 32.0°F is 0.00°C 0.0°F is -17.78°C
Time Complexity: O(1)
Space Complexity: O(1)
Using Lambda Function
We can also use a lambda function for a more concise approach ?
# Lambda function for conversion
fahrenheit_to_celsius = lambda f: (f - 32) / 1.8
# Test the lambda function
temperatures = [100, 68, 32, -40]
for temp in temperatures:
result = fahrenheit_to_celsius(temp)
print(f"{temp}°F = {result:.2f}°C")
100°F = 37.78°C 68°F = 20.00°C 32°F = 0.00°C -40°F = -40.00°C
Comparison
| Method | Code Lines | Best For |
|---|---|---|
| Direct Formula | 2-3 | Single conversion |
| Custom Function | 4-5 | Multiple conversions |
| Lambda Function | 1 | Concise one-liner |
Real-Life Applications
- Weather Forecasting: Converting temperatures between Celsius and Fahrenheit for different regions
- Scientific Research: Converting temperature data in research papers and experiments
- Cooking Applications: Converting oven temperatures between different measurement systems
- Medical Equipment: Converting body temperature readings for international standards
Conclusion
Use the direct formula for single conversions, custom functions for reusable code, and lambda functions for concise operations. The conversion formula (Fahrenheit - 32) / 1.8 remains constant across all approaches.
