Python program to convert Celsius to Fahrenheit
Description: Write a Python program to convert temperature from Celsius to Fahrenheit.
Rules and hints:
- Formula: Fahrenheit = Celsius x 9 / 5 + 32.
- Use an f-string to print the result.
Python program to convert Celsius to Fahrenheit
def to_fahrenheit(celsius):
return celsius * 9 / 5 + 32
for c in (0, 100):
f = to_fahrenheit(c)
print(f"{c} C = {f} F")
Output:
Tutorials Class - Output Window
0 C = 32.0 F
100 C = 212.0 F
How it works
- The function takes a Celsius value and returns the Fahrenheit value.
- The loop tests two famous values: water freezes at 0 C (32 F) and boils at 100 C (212 F).
- The
/operator always returns a float, that is why we see32.0.
Read What are f-strings? and Python Functions.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |