Back

Python Program to Convert Celsius to Fahrenheit With Examples

6 Mar 2025
5 min read

People worldwide use two different temperature measurement scales, celsius and fahrenheit.  To avoid confusion and errors, computer systems must convert one scale to another, depending on the scale they use. Therefore, converting temperature from Celsius to Fahrenheit is a basic concept students learn in programming. Since many countries still use Fahrenheit, the conversion is important for weather applications, data analysis, and scientific calculations. It is more so when temperature data comes from different regions. Knowing how to apply the Celsius to Fahrenheit formula in Python is necessary for anyone working with temperature-related data in scientific fields.

This article will examine how to write a Python program to convert Celsius to Fahrenheit and discover different methods. We will explore practical examples that show how to perform this conversion using a basic formula, functions, and classes. These techniques will help you understand how Python handles calculations and builds reusable code.

What are Fahrenheit and Celsius Scales?

Fahrenheit and Celsius are two temperature scales that are applied across the globe. Both scales apply different reference points to the freezing and boiling points of water.

1. Celsius Scale (°C): The most commonly applied scale globally, it is used more than others due to usage in scientific as well as regular temperature measurements by numerous countries. The Celsius scale is defined relative to the freezing point of water at 0°C and to the boiling point at 100°C under a standard atmospheric pressure.

2. Fahrenheit Scale (°F): It is used primarily in the United States and some other nations. In this system, water freezes at 32°F and boils at 212°F at standard conditions.

Because there are variations in the scale, the temperature readings on one scale need to be frequently converted to another. It is important to do so for worldwide use, such as in weather forecasting and scientific studies.

Celsius to Fahrenheit Formula in Python 

To convert a temperature from Celsius to Fahrenheit, we use the following formula:

\( F = \left( C \times \frac{9}{5} \right) + 32 \)

Here, 

F is the temperature in Fahrenheit

C is the temperature in Celsius

Celsius to Fahrenheit: \( T_F = \left( \frac{9}{5} T_C \right) + 32 \)

Fahrenheit to Celsius: \( T_C = \frac{5}{9} \left( T_F - 32 \right) \)

The Celsius to Fahrenheit formula uses the differences in how the two temperature scales are created. On the Celsius scale, 0°C is set as the freezing point of water and 100°C as the boiling point. Therefore, you are covering a range of 100 degrees.

On the Fahrenheit scale, the same is at 32°F for freezing and boiling at 212°F and spans 180 degrees. Therefore, each degree Celsius equals 9/5 (or 1.8) degrees Fahrenheit for converting. To convert Celsius to Fahrenheit, we first multiply by 9/5 to adjust for the difference in scale and then add 32 to match the Fahrenheit starting point.

This Celsius to Fahrenheit formula in Python is the main formula of the Python program. We can easily convert temperature values from one scale to another by applying this formula in a Python script.

Python Program To Convert Celsius to Fahrenheit

You can write a Fahrenheit to Celsius program in Python in many different ways. Our approach depends on how structured and reusable we want the code to be. This section will explore three approaches: using a basic formula, functions, and classes.

Celsius to Fahrenheit Using the Basic Formula

The simplest way to convert Celsius to Fahrenheit in Python is by directly applying the formula inside a script. This approach is very friendly for beginners as it demonstrates how mathematical operations work in Python. Below is the algorithm:

Algorithm

1. Start the program.

2. Ask the user to enter the temperature in Celsius.

3. Convert the input to a numerical value.

4. Apply the Celsius to Fahrenheit formula: F = (C x 9/5) + 32

5. Display the converted temperature in Fahrenheit.

6.  End the program.

Code

# Celsius to Fahrenheit Conversion Using Basic Formula

# Take user input
celsius = float(input("Enter temperature in Celsius: "))

# Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9/5) + 32

# Display the result
print(f"{celsius}°C is equal to {fahrenheit}°F")

How the Code Works

1. The program asks the user to enter a temperature in Celsius.

2. The input is converted to a floating-point number to handle decimal values.

3. The Celsius to Fahrenheit formula in Python is applied.

4. The program prints the result in a readable format.

Output

Enter temperature in Celsius: 0
0.0°C is equal to 32.0°F

=== Code Execution Successful ===

Converting Celsius to Fahrenheit Using a Function 

A function-based approach makes the code more reusable and structured. Instead of writing the conversion logic inside the main program, we define a function that takes Celsius as input and returns the Fahrenheit value. This method is useful for programs that require multiple conversions.

Algorithm

1. Start the program.

2. Define a function named celsius_to_fahrenheit that takes a Celsius value as input.

3. Inside the function, apply the Celsius to Fahrenheit formula: F = (C x 9/5) + 32

4. Return the converted Fahrenheit value.

5. In the main program, ask the user to enter a temperature in Celsius.

6. Call the function and pass the user input as an argument.

7. Display the result.

8. End the program.

Code

# Celsius to Fahrenheit Conversion Using a Function

# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
    return (celsius * 9/5) + 32

# Take user input
celsius = float(input("Enter temperature in Celsius: "))

# Call the function and store the result
fahrenheit = celsius_to_fahrenheit(celsius)

# Display the result
print(f"{celsius}°C is equal to {fahrenheit}°F")

How the Code Works

1. A function celsius_to_fahrenheit is defined, which takes Celsius as input and returns Fahrenheit.

2. The user is prompted to enter a temperature in Celsius.

3. The input is passed to the function, which performs the conversion.

4. The function returns the result, which is then printed.

Output

Enter temperature in Celsius: 100
100.0°C is equal to 212.0°F

=== Code Execution Successful ===

Converting Celsius to Fahrenheit Using a Class 

Using a class for the Fahrenheit to Celsius program in Python is beneficial when working with larger programs that need structured data handling. This object-oriented approach allows us to create reusable instances and store temperature values as attributes, which makes the program more scalable.

Algorithm

1. Start the program.

2. Define a class named TemperatureConverter.

3. Inside the class, create a constructor (__init__) to initialize the Celsius value.

4. Define a method to_fahrenheit() to apply the Celsius to Fahrenheit formula: F = (C x 9/5) + 32

5. In the main program, ask the user to enter a temperature in Celsius.

6. Create an instance of the class and pass the user input as an argument.

7. Call the to_fahrenheit() method to get the converted temperature.

8. Display the result.

9. End the program.

Code

# Celsius to Fahrenheit Conversion Using a Class

class TemperatureConverter:
    def __init__(self, celsius):
        self.celsius = celsius  # Store the Celsius value

    def to_fahrenheit(self):
        return (self.celsius * 9/5) + 32  # Convert to Fahrenheit

# Take user input
celsius = float(input("Enter temperature in Celsius: "))

# Create an instance of TemperatureConverter
converter = TemperatureConverter(celsius)

# Call the method to convert Celsius to Fahrenheit
fahrenheit = converter.to_fahrenheit()

# Display the result
print(f"{celsius}°C is equal to {fahrenheit}°F")

How the Code Works

A class TemperatureConverter is created to store and process temperature values.

The constructor (__init__) initialises the Celsius temperature when an object is created.

The method to_fahrenheit() applies the Celsius to Fahrenheit formula and returns the result.

The user enters a temperature, which is stored in an instance of the class.

Calling the to_fahrenheit() method converts and prints the temperature in Fahrenheit.

Output

Enter temperature in Celsius: 150
150.0°C is equal to 302.0°F

=== Code Execution Successful ===

Conclusion

Learning how to write Fahrenheit to Celsius Python code is a very important basic exercise for students who are learning Python coding. It mainly helps in understanding mathematical operations, user input handling, and different coding approaches like basic scripts, functions, and object-oriented programming. To take your coding skills to the next level, join the CCBP Academy 4.0 Program and get hands-on training in Python, data structures, and real-world projects.

Frequently Asked Questions

1. Why multiply 9/5 in the Celsius to Fahrenheit formula?

The factor 9/5 is meant to adjust for the difference in scale size between Celsius and Fahrenheit. It is necessary to convert from one scale to the other in the correct manner.

2. Can we convert Fahrenheit to Celsius using the same formula?

No, you need to use a different formula. The reverse formula is C = (F - 32) × 5/9. It adjusts Fahrenheit values back to Celsius.

3. Why should we use functions or classes for conversion?

Functions and classes make the code reusable and structured. It is especially useful in larger programs handling multiple conversions.

4. Does Python have a built-in method to convert Celsius to Fahrenheit?

No, Python doesn’t have a direct function for this.  But you can easily create one using the formula.

5. Where is temperature conversion used in real life?

It’s used in weather apps, scientific calculations, and global temperature reporting, where different regions use different temperature scales.

Read More Articles

Chat with us
Chat with us
Talk to career expert