Convert Celsius to Fahrenheit in Python
Check 25°C against 77°F while building a Python temperature converter. See why new programmers need both the 9/5 scale factor and the extra 32 in the formula.
1# Program to convert Celsius to Fahrenheit23celsius = float(input("Enter temperature in Celsius: "))45fahrenheit = (celsius * 9/5) + 3267print(celsius, "°C is equal to", fahrenheit, "°F")
Sample Output
25.0 °C is equal to 77.0 °F
25.0 multiplied by 9/5 is 45.0, and adding 32 gives 77.0.
In this one
- Apply a scale and offset
- Use Python 3 true division
- Check formulas with known reference values
How it works
- 1
Read celsius as a float.
- 2
Evaluate (celsius * 9/5) + 32 into fahrenheit.
- 3
Print the two values with their unit labels.
Edge cases
Freezing point
Enter 0.
The product is zero, leaving the required 32.0 Fahrenheit.
Negative temperature
Enter -40.
Both scales meet at -40, so the converted result is -40.0.
What's going on
F = C times 9/5 plus 32. The multiplication changes the scale of a degree; the added 32 accounts for the two scales starting at different zero points. You need both parts.
Python 3 uses true division for /, so 9/5 is 1.8. The expression multiplies celsius by 9, divides by 5, then adds 32. The parentheses make that grouping easy to read.
25 degrees Celsius becomes 77 degrees Fahrenheit. Zero Celsius becomes 32 Fahrenheit, which is a better test than only trying one positive input. Do not replace / with //: floor division loses the fractional part of the scaled value. The ° symbols are just text in the output labels; they do not affect the arithmetic.
After this program
If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does / do in Python 3?
Pick an answer to check it.
Question 2
Why is 32 added?
Pick an answer to check it.
Questions people ask
What does / do in Python 3?
It performs true division, so 9/5 evaluates to 1.8.
Why is 32 added?
It accounts for the offset between the Celsius and Fahrenheit zero points.
What is a quick check for a missing offset?
Convert 0°C; the answer must be 32°F, not 0°F.