0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Fahrenheit to Celsius

To convert Fahrenheit to Celsius in Python, use the formula C = (F - 32) * 5 / 9 where F is the Fahrenheit temperature and C is the Celsius temperature.
📋

Examples

Input32
Output0.0
Input100
Output37.77777777777778
Input-40
Output-40.0
🧠

How to Think About It

To convert Fahrenheit to Celsius, subtract 32 from the Fahrenheit value to remove the offset, then multiply the result by 5/9 to scale it to the Celsius range. This formula adjusts the temperature from one scale to the other.
📐

Algorithm

1
Get the temperature value in Fahrenheit from the user or input.
2
Subtract 32 from the Fahrenheit temperature.
3
Multiply the result by 5.
4
Divide the product by 9 to get the Celsius temperature.
5
Return or print the Celsius temperature.
💻

Code

python
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = (fahrenheit - 32) * 5 / 9
print(f"Temperature in Celsius: {celsius}")
Output
Enter temperature in Fahrenheit: 100 Temperature in Celsius: 37.77777777777778
🔍

Dry Run

Let's trace the input 100 Fahrenheit through the code

1

Input Fahrenheit

fahrenheit = 100.0

2

Subtract 32

100.0 - 32 = 68.0

3

Multiply by 5

68.0 * 5 = 340.0

4

Divide by 9

340.0 / 9 = 37.77777777777778

5

Print Celsius

Temperature in Celsius: 37.77777777777778

StepOperationResult
1Input Fahrenheit100.0
2100.0 - 3268.0
368.0 * 5340.0
4340.0 / 937.77777777777778
5Print result37.77777777777778
💡

Why This Works

Step 1: Remove Fahrenheit offset

Subtracting 32 from the Fahrenheit value adjusts the starting point to zero Celsius.

Step 2: Scale the temperature

Multiplying by 5 and dividing by 9 converts the temperature scale from Fahrenheit to Celsius.

Step 3: Output the result

The final value is the temperature in Celsius, which can be printed or used further.

🔄

Alternative Approaches

Using a function
python
def fahrenheit_to_celsius(f):
    return (f - 32) * 5 / 9

print(fahrenheit_to_celsius(100))
This approach makes the conversion reusable and cleaner for multiple inputs.
Using lambda expression
python
convert = lambda f: (f - 32) * 5 / 9
print(convert(100))
A concise way to define the conversion inline, useful for quick calculations.

Complexity: O(1) time, O(1) space

Time Complexity

The calculation uses a fixed number of arithmetic operations, so it runs in constant time.

Space Complexity

Only a few variables are used, so the space needed is constant.

Which Approach is Fastest?

All approaches run in constant time; using a function or lambda improves code reuse but does not affect speed.

ApproachTimeSpaceBest For
Direct calculationO(1)O(1)Simple one-time conversion
FunctionO(1)O(1)Reusable code for multiple conversions
LambdaO(1)O(1)Quick inline conversions
💡
Always convert the input to float to handle decimal temperatures correctly.
⚠️
Forgetting to subtract 32 before multiplying by 5/9 leads to incorrect Celsius values.