Python Program to Convert Temperature Between Celsius and Fahrenheit
You can convert temperature in Python using the formula
F = (C * 9/5) + 32 for Celsius to Fahrenheit and C = (F - 32) * 5/9 for Fahrenheit to Celsius; for example, fahrenheit = (celsius * 9/5) + 32 converts Celsius to Fahrenheit.Examples
Inputcelsius = 0
OutputFahrenheit: 32.0
Inputfahrenheit = 100
OutputCelsius: 37.78
Inputcelsius = -40
OutputFahrenheit: -40.0
How to Think About It
To convert temperature, first decide which scale you start with: Celsius or Fahrenheit. Use the correct formula to convert to the other scale by multiplying, dividing, and adding or subtracting constants. This is like changing units, for example, converting kilometers to miles by multiplying by a fixed number.
Algorithm
1
Get the temperature value and the scale it is in (Celsius or Fahrenheit).2
If the scale is Celsius, apply the formula to convert to Fahrenheit.3
If the scale is Fahrenheit, apply the formula to convert to Celsius.4
Display the converted temperature with the correct scale.Code
python
def celsius_to_fahrenheit(celsius): return (celsius * 9/5) + 32 def fahrenheit_to_celsius(fahrenheit): return (fahrenheit - 32) * 5/9 # Example usage c = 25 f = celsius_to_fahrenheit(c) print(f"{c}°C is {f:.2f}°F") f2 = 77 c2 = fahrenheit_to_celsius(f2) print(f"{f2}°F is {c2:.2f}°C")
Output
25°C is 77.00°F
77°F is 25.00°C
Dry Run
Let's trace converting 25°C to Fahrenheit and 77°F to Celsius through the code.
1
Convert 25°C to Fahrenheit
Calculate (25 * 9/5) + 32 = 45 + 32 = 77
2
Convert 77°F to Celsius
Calculate (77 - 32) * 5/9 = 45 * 5/9 = 25
| Input | Operation | Result |
|---|---|---|
| 25°C | (25 * 9/5) + 32 | 77°F |
| 77°F | (77 - 32) * 5/9 | 25°C |
Why This Works
Step 1: Formula for Celsius to Fahrenheit
Multiply the Celsius temperature by 9/5 and then add 32 to shift the scale to Fahrenheit.
Step 2: Formula for Fahrenheit to Celsius
Subtract 32 from the Fahrenheit temperature and multiply by 5/9 to convert back to Celsius.
Alternative Approaches
Using a single function with scale parameter
python
def convert_temperature(value, scale): if scale == 'C': return (value * 9/5) + 32 elif scale == 'F': return (value - 32) * 5/9 else: return None print(convert_temperature(0, 'C')) # 32.0 print(convert_temperature(32, 'F')) # 0.0
This method uses one function for both conversions, making the code shorter but requires checking the scale each time.
Using lambda functions
python
c_to_f = lambda c: (c * 9/5) + 32 f_to_c = lambda f: (f - 32) * 5/9 print(c_to_f(100)) # 212.0 print(f_to_c(212)) # 100.0
Lambda functions provide a concise way to write conversion formulas but may be less readable for beginners.
Complexity: O(1) time, O(1) space
Time Complexity
The conversion uses simple arithmetic operations that take constant time regardless of input.
Space Complexity
Only a few variables are used to store input and output, so space usage is constant.
Which Approach is Fastest?
All approaches run in constant time; using separate functions or a single function with a parameter has no significant speed difference.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Separate functions | O(1) | O(1) | Clear and explicit conversions |
| Single function with scale | O(1) | O(1) | Compact code with scale check |
| Lambda functions | O(1) | O(1) | Concise one-liners, less readable |
Always round the converted temperature to two decimal places for neat output.
Forgetting to subtract 32 before multiplying when converting Fahrenheit to Celsius.