Book a free demo

Beginner ยท ages 9+ ยท functions ยท return ยท formatting

Temperature converter in Python.

Turn Celsius into Fahrenheit and print a conversion table.

The program

Press Run to see what it prints. Change anything you like, then run it again.

def to_fahrenheit(c):
    return c * 9 / 5 + 32

for c in range(0, 101, 20):
    print(f"{c} C = {to_fahrenheit(c):.1f} F")
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

How it works

  1. A function is a named recipe. def defines it and return sends back the answer.
  2. range(0, 101, 20) counts in steps of 20.
  3. :.1f inside an f-string means 'show one decimal place'.
See the Python path โ†’

Try this

  • Write a function that turns Fahrenheit back into Celsius.
  • Print every 10 degrees.
  • Find the temperature where both scales give the same number.

Stuck on an error? Read how to debug Python.

Similar ideas

More programs

All programs โ†’

FAQ

Temperature converter: questions

What does the temperature converter program do?

Turn Celsius into Fahrenheit and print a conversion table. It prints: 0 C = 32.0 F / 20 C = 68.0 F / 40 C = 104.0 F โ€ฆ

Which Python ideas does it use?

functions, return, formatting. Beginner level, suitable from about age 9.

How do I run it?

Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.

What should I try next?

Write a function that turns Fahrenheit back into Celsius. Print every 10 degrees. Find the temperature where both scales give the same number.

Start a conversation

Want a teacher to guide the learning?

Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try