Beginner Python Exercises

Celsius to Fahrenheit Converter in Python

F = C × 9 / 5 + 32. Write a small conversion function and print the result with one decimal place.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • Turning a formula into a function
  • Floating-point results
  • Formatting with :.1f
25 °C → °F
  1. 125 * 9= 225
  2. 2/ 5= 45.0
  3. 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.

From a two-scale thermometer to code
celsius = 25.0🌡️ The reading on the Celsius side
celsius * 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.0
Multiply by 9/5, then add 32.
In one sentence

To convert Celsius to Fahrenheit, multiply by 9/5 and add 32: F = C × 9/5 + 32.

Key points

  1. celsius * 9 / 5 + 32 follows the formula; multiplication and division run before addition.
  2. / always returns a float, so the result keeps its decimals.
  3. {celsius:.1f} and {fahrenheit:.1f} print both values with one decimal place.
  4. 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 decimal module, because floats can round in surprising ways.

Example code

Run code →
main.py
celsius = 25.0
# Apply the Celsius-to-Fahrenheit formula.
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius:.1f} C = {fahrenheit:.1f} F")
Get a Run button for this example

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.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Adding before multiplying

Without parentheses in the right place, the formula changes.

Problem
fahrenheit = (celsius + 32) * 9 / 5
Fix
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