Celsius to Fahrenheit in Python
A formula program that also teaches something important about Python 3: the / operator always gives a decimal answer.
Quick answer
The formula is F = C × 9 / 5 + 32, and the reverse is C = (F − 32) × 5 / 9. In Python: f = c * 9 / 5 + 32. So 0 °C is 32.0 °F, 37 °C (body temperature) is 98.6 °F and 100 °C is 212.0 °F.
The question
Read a temperature in Celsius (an integer) and print it in Fahrenheit using F = C × 9 / 5 + 32.
The code
Input
70
Output
Fahrenheit = 158.0
How it works
- 1Read the temperature in Celsius.
- 2Apply F = C × 9 / 5 + 32. Multiplication and division happen before addition.
- 3Because / produces a float, a whole answer such as 212 prints as 212.0.
The formula on common temperatures
| c | c * 9 / 5 + 32 | Fahrenheit |
|---|---|---|
| 0 | 0 * 9 / 5 + 32 | 32.0 |
| 37 | 37 * 9 / 5 + 32 | 98.6 |
| 100 | 100 * 9 / 5 + 32 | 212.0 |
| -40 | -40 * 9 / 5 + 32 | -40.0 |
−40 is the one temperature that is the same in both scales.
Common mistakes
- Writing C * (9 / 5 + 32) — the brackets change the order and the answer is wrong.
- Using // (floor division), which throws away the decimal part.
Fahrenheit to Celsius (the reverse)
Turn the formula around: subtract 32 first, then multiply by 5 / 9. The brackets make the subtraction happen first.
Input
212
Output
Celsius = 100.0
Print a Celsius to Fahrenheit table using a loop
range(0, 101, 20) gives 0, 20, 40 … 100, and the same formula is applied to each value.
Input
(no input)
Output
0 C = 32.0 F 20 C = 68.0 F 40 C = 104.0 F 60 C = 140.0 F 80 C = 176.0 F 100 C = 212.0 F
Questions students ask
- What is the formula to convert Celsius to Fahrenheit in Python?
- f = c * 9 / 5 + 32. For 100 °C it gives 212.0 °F, and for 0 °C it gives 32.0 °F.
- How do I convert Fahrenheit to Celsius?
- c = (f - 32) * 5 / 9. Subtract 32 first — that is what the brackets are for — then multiply by 5 / 9. For 212 °F it gives 100.0 °C.
Now solve a similar question yourself
A new Python question every time, checked instantly.
Preparing for NIELIT O Level?
This program is the kind asked in the M3 (Python) theory paper and the PR-3 practical exam.
Want to build websites and apps with AI tools instead of writing every line yourself? See our AI Digital Pro course in Kaithal.
