Editor

Python Program to Convert Celsius to Fahrenheit

Beginner · Basics

Celsius and Fahrenheit are two scales for measuring temperature: water freezes at 0°C or 32°F and boils at 100°C or 212°F. To convert, multiply the Celsius value by 9/5 and add 32. This program converts normal body temperature, 37°C, to Fahrenheit.

Program

celsius_to_fahrenheit.py
celsius = 37 # or: celsius = float(input("Enter temperature in Celsius: "))
# Multiply by 9/5, then add 32
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius}°C = {fahrenheit:.1f}°F")
Output
37°C = 98.6°F

How it works

  1. celsius * 9 / 5 scales the value, since a change of 1 degree Celsius equals 1.8 degrees Fahrenheit.
  2. Adding 32 shifts the scale, because 0°C is 32°F.
  3. :.1f prints the result with one decimal place, giving 98.6.

Other ways to write it

A conversion table

A conversion table
print("Celsius Fahrenheit")
for celsius in range(-10, 41, 10):
fahrenheit = celsius * 9 / 5 + 32
print(f"{celsius:7} {fahrenheit:10.1f}")
Output
Celsius  Fahrenheit
    -10        14.0
      0        32.0
     10        50.0
     20        68.0
     30        86.0
     40       104.0

range(-10, 41, 10) counts from -10 to 40 in steps of 10, and the widths in the f-string line up the columns.

Fahrenheit to Celsius

Fahrenheit to Celsius
fahrenheit = 98.6
celsius = (fahrenheit - 32) * 5 / 9
print(f"{fahrenheit}°F = {celsius:.1f}°C")
Output
98.6°F = 37.0°C

To go the other way, subtract 32 first, then multiply by 5/9.

Try it yourself

Find the temperature where Celsius and Fahrenheit show the same number by trying values from -50 to 0 in a loop.

Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.

All Python programs · CBSE class 12 practical file · Python exercises