Python programs · Numbers
Python program to convert Celsius to Fahrenheit
Converting temperatures is a short program that practises decimals and formatting.
Approach
- Read the temperature in Celsius as a decimal number.
- Multiply by 9, divide by 5 and add 32.
- Print the result with one decimal place.
The program
c = float(input()) f = c * 9 / 5 + 32 print(f"{c} C = {f:.1f} F")
Sample run
Input
37
Output
37.0 C = 98.6 F
How the code works
The formula is F = C × 9 ÷ 5 + 32. Body temperature, 37 °C, becomes 98.6 °F. {f:.1f} inside the f-string rounds to one decimal place. float() is used instead of int() so that readings such as 36.6 are accepted.
Try changing it
- Convert the other way, from Fahrenheit to Celsius.
- Print a table of conversions from 0 to 100 in steps of 10.
Common mistakes
- Using
9 // 5, which is 1 and ruins the answer. - Using
int(input()), which rejects decimal readings.