Celsius to Fahrenheit Converter in Python
F = C × 9 / 5 + 32. Write a small conversion function and print the result with one decimal place.
- Turning a formula into a function
- Floating-point results
- Formatting with :.1f
- 1
25 * 9= 225 - 2
/ 5= 45.0 - 3
+ 32= 77.0 °F
Apply the formula, format the result
The problem
Weather apps, recipes and science homework constantly switch between Celsius and Fahrenheit. A converter is a short program, but it practices a formula, the order of operations and readable output.
Think of it like this
Converting a temperature is like reading a thermometer that has both scales printed side by side. The liquid stands at one height, and you read it on the other scale. Fahrenheit marks are closer together (9 marks for every 5 Celsius marks), and the Fahrenheit scale reads 32 where Celsius reads 0.
celsius = 25.0🌡️ The reading on the Celsius sidecelsius * 9 / 5📐 Fahrenheit degrees are smaller: 9 for every 5+ 32➕ Fahrenheit reads 32 where Celsius reads 0{fahrenheit:.1f}📝 Write it with one decimal: 77.0To convert Celsius to Fahrenheit, multiply by 9/5 and add 32: F = C × 9/5 + 32.
Key points
celsius * 9 / 5 + 32follows the formula; multiplication and division run before addition./always returns a float, so the result keeps its decimals.{celsius:.1f}and{fahrenheit:.1f}print both values with one decimal place.- 25 °C becomes 77.0 °F.
When should you use it?
- Unit converters for temperature, distance, weight or currency.
- Practicing formulas and operator precedence.
- Formatting numbers for people to read.
When not to use it
- Converting many values: wrap the formula in a function so it is written once.
- Money: use the
decimalmodule, because floats can round in surprising ways.
Example code
celsius = 25.0
# Apply the Celsius-to-Fahrenheit formula.
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius:.1f} C = {fahrenheit:.1f} F")
Result of running the code
25.0 C = 77.0 F
Code explanation
The formula multiplies celsius by 9, divides by 5 and then adds 32; Python performs * and / before +, so no parentheses are needed. Because / always returns a float, the result keeps its decimal part. The format specifier .1f prints both temperatures with exactly one decimal place.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Adding before multiplying
Without parentheses in the right place, the formula changes.
fahrenheit = (celsius + 32) * 9 / 5
fahrenheit = celsius * 9 / 5 + 32
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges