Published: 6 March 2025 | Reading Time: 5 minutes
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. 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, especially when temperature data comes from different regions.
This article examines how to write a Python program to convert Celsius to Fahrenheit using 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.
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.
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 standard atmospheric pressure.
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, 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.
To convert a temperature from Celsius to Fahrenheit, we use the following formula:
F = (C × 9/5) + 32
Where:
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, 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.
Celsius to Fahrenheit: TF = (9/5 × TC) + 32
Fahrenheit to Celsius: TC = 5/9 × (TF - 32)
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.
You can write a Fahrenheit to Celsius program in Python in many different ways. The 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.
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.
# 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")
Enter temperature in Celsius: 0
0.0°C is equal to 32.0°F
=== Code Execution Successful ===
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.
celsius_to_fahrenheit that takes a Celsius value as input.# 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")
celsius_to_fahrenheit is defined, which takes Celsius as input and returns Fahrenheit.Enter temperature in Celsius: 100
100.0°C is equal to 212.0°F
=== Code Execution Successful ===
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.
TemperatureConverter.(__init__) to initialize the Celsius value.to_fahrenheit() to apply the Celsius to Fahrenheit formula: F = (C × 9/5) + 32to_fahrenheit() method to get the converted temperature.# 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")
TemperatureConverter is created to store and process temperature values.(__init__) initializes the Celsius temperature when an object is created.to_fahrenheit() applies the Celsius to Fahrenheit formula and returns the result.to_fahrenheit() method converts and prints the temperature in Fahrenheit.Enter temperature in Celsius: 150
150.0°C is equal to 302.0°F
=== Code Execution Successful ===
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.
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.
No, you need to use a different formula. The reverse formula is C = (F - 32) × 5/9. It adjusts Fahrenheit values back to Celsius.
Functions and classes make the code reusable and structured. It is especially useful in larger programs handling multiple conversions.
No, Python doesn't have a direct function for this. But you can easily create one using the formula.
It's used in weather apps, scientific calculations, and global temperature reporting, where different regions use different temperature scales.
Understand Generators in Python for Efficient Programming - Explore the power of generators in Python. Learn how to write memory-efficient code using yield and generator expressions. (07 Jan 2026, 5 min read)
Bitwise Operators in Python Explained with Examples - Learn bitwise operators in Python with simple examples, explanations, and use cases to understand how binary operations work step by step. (07 Jan 2026, 5 min read)
Palindrome String in Python: Easy Guide to Check Palindromes - Learn how to check if a string is a palindrome in Python. Explore simple logic, step-by-step examples, and Python code for palindrome string programs. (05 Jan 2026, 5 min read)
Leap Year Program in Python - Learn how to write a leap year program in Python using simple conditions, clear logic, and easy examples for beginners. (04 Jan 2026, 5 min read)
A Complete Guide to Exception Handling in Python for Developers - Explore the basics and best practices of exception handling in Python. Understand try-except blocks, custom exceptions, and real-world uses. (02 Jan 2026, 6 min read)
While Loop in Python: Syntax, Examples and Best Practices - Learn how the while loop in Python works with syntax, examples, flowcharts, and real-world use cases. Master conditions, iterations, break, and continue. (02 Jan 2026, 6 min read)
Source: NxtWave CCBP Blog
Original URL: https://www.ccbp.in/blog/articles/python-program-to-convert-celsius-to-fahrenheit