To write a Python program that converts between Celcius and Fahrenheit, you need to implement these formulas in your code:

# Celcius to Fahrenheit: F = 1.8 x C + 32 # Fahrenheit to Celcius: C = (F - 32) / 1.8

## Quick Solution: Celcius to Fahrenheit Program

Here’s an example program that **asks for temperature in Celcius and converts the input to Fahrenheit**:

# Ask for user input c = float(input("Give me a temperature in celcius: ")) # Convert user input to Fahrenheit f = 1.8 * c + 32 # Show the result in console print(f"The temperature is {f} in fahrenheit")

## Quick Solution: Fahrenheit to Celcius Program

And here’s an example program that **asks for temperature in Fahrenheit and gives back Celcius**:

# Ask for user input f = float(input("Give me a temperature in fahrenheit: ")) # Convert user input to Celcius c = (f - 32) / 1.8 # Show the result in console print(f"The temperature is {c} in fahrenheit")

## Converting between Celcius and Fahrenheit

Fahrenheit is a popular temperature measuring scale in which water freezes at 32 degrees and boils at 212 degrees. Fahrenheit is denoted by using the °F symbol.

Another popular temperature scale is the Celcius scale. In Celcius, water freezes at 0 degrees and boils at 100. Celcius is denoted by °C.

**Converting Fahrenheit to Celcius is a common task because different countries use different temperature scales.**

For example, if you’re a European and visit the US, you might wonder if it’s safe to go outside when the temperatures reach 100. No worries, even though it’s hot weather you are not going to boil—100 °F means ~38 °C.

In this section, you learn how to perform the conversion between Celcius and Fahrenheit.

## Celcius to Fahrenheit Formula

To convert Celcius to Fahrenheit, use the following simple formula:

F = 1.8 x C + 32

Where:

- F is the resulting temperature in Fahrenheit.
- C is the input temperature in Celcius.

## Fahrenheit to Celcius Formula

To convert Fahrenheit to Celcius, use the following simple formula:

C = (F - 32) / 1.8

Where:

- C is the resulting temperature in Celcius
- F is the input temperature in Fahrenheit

If you know basic math, you only need to remember either one of these equations and solve for the other temperature variable to convert in the other direction.

For example, you can use the Fahrenheit-to-Celcius equation to convert Celcius to Fahrenheit by multiplying both sides by 1.8 and subtracting 32.

Now that you know the conversion equations, you can turn them into Python code easily.

## Python Program to Convert Celcius to Fahrenheit

Converting Celcius to Fahrenheit is classic homework in a Python programming course. There are two types of exercises related to Celcius-to-Fahrenheit conversion:

- Convert user input from Celcius to Fahrenheit.
- Create a function that converts Celcius to Fahrenheit.

Also, you might need to convert Fahrenheit to Celcius.

The following example solutions cover all these cases, that is:

- Convert
**Celcius to Fahrenheit****from user input**. - Convert
**Celcius to Fahrenheit****with a function**. - Convert
**Fahrenheit to Celcius****from user input**. - Convert
**Fahrenheit to Celcius****with a function**.

### Example 1. Convert from User Input

To convert user input in Celcius to Fahrenheit:

**Ask**for user input.**Cast**the user input into a floating point value with the float() function.**Convert**the input Celcius to Fahrenheit with the formula you learned before.**Show**the result.

Here’s how it looks in the code:

c = float(input("Give me a temperature in celcius: ")) f = 1.8 * c + 32 print(f"The temperature is {f} in fahrenheit")

Example run:

Give me a temperature in celcius: 53 The temperature is 127.4 in fahrenheit

### Example 2. Write a Conversion Function

If you’re not dealing with user inputs, you can write a reusable function to calculate the conversion from Celcius to Fahrenheit. You can then call the function anywhere in your code as many times as you like.

Here’s an example solution:

def to_fahrenheit(c): return 1.8 * c + 32 # Example calls: print(to_fahrenheit(35)) print(to_fahrenheit(-12))

Output:

95.0 10.399999999999999

## Python Program to Convert Fahrenheit to Celcius

Let’s also convert Fahrenheit to Celcius in a similar manner you just did above in the opposite direction.

### Example 1. Convert from User Input

To convert user input in Fahrenheit to Celcius:

**Ask**for user input in Fahrenheit.**Turn**the user input into a float with the built-in**float()**function.**Convert**the Fahrenheit to Celcius with the formula you learned earlier.**Display**the result.

Here’s an example code:

f = float(input("Give me a temperature in fahrenheit: ")) c = (f - 32) / 1.8 print(f"The temperature is {c} in fahrenheit")

Example run:

Give me a temperature in fahrenheit: 100 The temperature is 37.77777777777778 in fahrenheit

### Example 2. Write a Conversion Function

If you’re not dealing with user inputs, you can write a function that converts Fahrenheit to Celcius:

def to_celcius(f): return (f - 32) / 1.8 # Example calls: print(to_celcius(150)) print(to_celcius(48))

Output:

65.55555555555556 8.88888888888889

## Summary

Today you learned how to use Python to convert Celcius to Fahrenheit and vice versa.

To summarize, converting from Celcius to Fahrenheit in Python happens with the following formula:

f = 1.8 * c + 32

And to convert from Fahrenheit to Celcius, you can solve for c in the above equation. This gives you:

c = (c - 32) / 1.8

Depending on what you’re trying to accomplish, you can either use a custom function to do the conversions or convert user input from console.

Thanks for reading. Happy coding!