Post

Convert Celsius to Fahrenheit in Python

In this project, we will create a python project on how we can convert Celsius to Fahrenheit in python. Also, we will learn about what is Celsius and Fahrenheit are and the mathematical foundations regarding the conversion of Celsius to Fahrenheit.

Convert Celsius to Fahrenheit in Python

Project Prerequisites:

In this python project, you just need to know basic python. That includes.

What will you learn?

  • End to End development of Python programs based on mathematical formulas.
  • Basic Input / Output implementation.
  • How to translate a real-world mathematical formula into clean Python code.
  • How to write reusable functions for unit conversion.
  • How to handle user input safely using type casting.

Let’s learn what Celsius and Fahrenheit are and what they are used to.

Introduction

Temperature is one of the most commonly measured physical quantities in daily life. Whether you are checking the weather forecast, cooking a meal, or running a scientific experiment, you will almost certainly encounter temperature values expressed in one of two popular scales: Celsius or Fahrenheit. Countries that follow the metric system (most of the world) use Celsius, while the United States and a few other regions still rely on Fahrenheit for everyday measurements.

As a Python developer, knowing how to convert between these two scales is a practical skill. It teaches you how to read mathematical formulas and translate them directly into working code, how to accept user input, perform arithmetic, and display a formatted result. By the end of this tutorial you will have two fully working Python programs that handle the conversion, and you will understand every line of code.

What is Celsius?

Celsius is a measurement system used in the metric system. It is often associated with temperatures, but it is more than that. Celsius can also measure other things like volume and pressure, and many also know it as centigrade.

The Celsius scale was created in 1742 by Swedish astronomer Anders Celsius, trying to find an alternative to the outdated Fahrenheit scale for measuring temperature. He set his scale at 0 degrees for freezing water and 100 degrees for boiling water. The metric system was standardized in 1960 by an international agreement, making the Celsius scale universal across many countries today.

Celsius is symbolized as °C as a unit of measurement.

What is Fahrenheit?

Fahrenheit is a temperature scale that was created by German physicist Daniel Gabriel Fahrenheit in 1724. This is the imperial measurement system for measuring temperature. That measures the boiling point of water at 212 degrees and the freezing point of water at 32 degrees. The Celsius scale is used in most other countries in the world.

Fahrenheit is symbolized as °F as a unit of measurement.

Now let’s see how to convert Celsius into Fahrenheit based on scientific and mathematical proofs.

Approach to convert celsius to Fahrenheit:

To convert Celsius to Fahrenheit, we have to multiply celsius by 1.8 or 9/5 and then add 32.

Formula:

T(°F) = T(°C) × 9/5 + 32 or T(°F) = T(°C) × 1.8 + 32

Example:

(4 °C * 1.8) + 32 = 39.2 °F or (4°C × 9/5) + 32 = 39.2°F

The logic behind this formula is straightforward. The ratio 9/5 (or 1.8) accounts for the difference in the size of one degree between the two scales — a degree on the Fahrenheit scale is smaller than a degree on the Celsius scale. The constant 32 is added because the two scales have different zero points: water freezes at 0°C but at 32°F.

Celsius to Fahrenheit Chart

Below is the chart overview of Celsius to Fahrenheit.

Celsius °CFahrenheit °F
-30 °C-22 °F
-20 °C-4.0 °F
-10 °C14.0 °F
0 °C32.0 °F
1 °C33.8 °F
2 °C35.6 °F
3 °C37.4 °F
4 °C39.2 °F
5 °C41.0 °F

You can verify any row of this table by plugging the Celsius value into the formula. For example, for 5°C: (5 × 1.8) + 32 = 9 + 32 = 41.0°F. This cross-checking habit is excellent practice when you are writing programs based on formulas.

Now let’s get our hands dirty by writing a python program based on the above formula and example, which will give you an understanding of how we can convert any mathematical formula into the python program.

Convert Celsius to Fahrenheit in Python

In this python project, first, we will take input from the user, and then we will implement a formula to convert celsius to Fahrenheit and print the result after that.

Example 1: How to convert Celsius to Fahrenheit in python?

1
2
3
4
5
6
7
8
9
10
# Python project to convert celsius to Fahrenheit in python.

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

# Implementing the formula
fahrenheit = (celsius * 1.8) + 32

# Printing the result
print("The %0.f degrees Celsius is equal to %0.1f degrees Fahrenheit" % (celsius, fahrenheit))

The output will be as follow:

1
2
Enter the temperature in Celsius: 4
The 4 degrees Celsius is equal to 39.2 degrees Fahrenheit.

Step-by-Step Explanation of Example 1

Step 1 — Accept user input:
input() always returns a string in Python. Since we need to perform arithmetic on the value, we wrap it with float(). Using float instead of int allows the user to enter decimal values like 36.6 (normal body temperature).

Step 2 — Apply the formula:
fahrenheit = (celsius * 1.8) + 32 mirrors the mathematical formula exactly. Python follows the standard order of operations (PEMDAS/BODMAS), so the multiplication happens first, and then 32 is added. The parentheses here are actually redundant due to operator precedence, but including them makes the code more readable and matches the written formula.

Step 3 — Format and print the result:
The %0.f format specifier prints the Celsius value as an integer (no decimal places), while %0.1f prints the Fahrenheit value rounded to one decimal place. This gives a clean, human-readable output.

Example 2: Create a reusable conversion function in Python

Wrapping your logic inside a function makes it reusable. You can call CelsiusToFahrenheit() anywhere in your program without rewriting the formula every time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Define a reusable function for the conversion
def CelsiusToFahrenheit(value):
    """Convert a Celsius temperature to Fahrenheit and return the result."""
    result = (value * 1.8) + 32
    return result

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

# Call the function
fahrenheit = CelsiusToFahrenheit(celsius)

# Display the result
print("The %0.f degrees Celsius is equal to %0.1f degrees Fahrenheit" % (celsius, fahrenheit))

Output:

1
2
Enter the temperature in Celsius: 25
The 25 degrees Celsius is equal to 77.0 degrees Fahrenheit

Step-by-Step Explanation of Example 2

Step 1 — Define the function:
def CelsiusToFahrenheit(value): declares a function that accepts one parameter called value. Inside the function, the conversion formula is applied and the result is returned using the return statement.

Step 2 — Add a docstring:
The triple-quoted string immediately below the def line is a docstring. It describes what the function does. This is a Python best practice that helps other developers (and your future self) understand the purpose of the function without reading its implementation.

Step 3 — Call the function:
fahrenheit = CelsiusToFahrenheit(celsius) passes the user-provided Celsius value into the function and stores the returned Fahrenheit value in the variable fahrenheit.

Step 4 — Print the result:
The formatted print() statement displays both values clearly.

How It Works — The Big Picture

Both programs follow the same three-step pipeline:

  1. Input — gather a temperature value from the user.
  2. Process — apply the formula F = C × 1.8 + 32.
  3. Output — display the converted temperature.

This input-process-output (IPO) model is the foundation of almost every program you will ever write, regardless of language or complexity. Understanding it now will make you a better programmer in the long run.

Customisation Ideas

Once you are comfortable with the basic program, here are some ways to extend it:

  1. Reverse conversion — add a FahrenheitToCelsius(value) function using the formula C = (F - 32) / 1.8 and let the user choose the direction of conversion.
  2. Kelvin support — add a third scale. Kelvin is used in scientific contexts, and the conversion from Celsius is K = C + 273.15.
  3. Input validation — use a try/except block to handle cases where the user types something that cannot be converted to a float (for example, letters or symbols).
  4. Loop for multiple conversions — wrap the program in a while True loop so that users can keep entering values until they type quit.
  5. GUI version — use the tkinter library to create a simple graphical window with an input box and a Convert button.

Common Errors and Fixes

Error 1: ValueError: could not convert string to float
This happens when the user types a non-numeric value such as abc or leaves the input blank.
Fix: wrap the input in a try/except block.

1
2
3
4
try:
    celsius = float(input("Enter the temperature in Celsius: "))
except ValueError:
    print("Please enter a valid number.")

Error 2: IndentationError inside a function
If the return statement is not indented to the same level as the rest of the function body, Python raises an IndentationError.
Fix: ensure consistent indentation (4 spaces is the standard in Python).

Error 3: Forgetting to call float() on the input
If you write celsius = input(...) without float(), the variable holds a string. When you try to multiply it by 1.8, Python raises a TypeError.
Fix: always cast numeric inputs with int() or float() depending on the expected type.

Error 4: Printing before assigning the result
If you call print(fahrenheit) before the line fahrenheit = (celsius * 1.8) + 32, Python raises a NameError because fahrenheit has not been defined yet.
Fix: make sure every variable is assigned before it is used.

FAQ

Q1: Why do we use float() instead of int() when reading input?
A: float() allows decimal values, which are common for real-world temperatures (for example, 36.6°C for body temperature). If you used int(), any decimal part would be silently dropped. Using float() keeps the precision intact.

Q2: Can I use the fraction 9/5 instead of 1.8 in Python?
A: Yes. In Python 3, 9/5 evaluates to 1.8 because the / operator performs true (float) division by default. So (celsius * 9/5) + 32 and (celsius * 1.8) + 32 produce the same result. In Python 2 (now end-of-life), 9/5 would evaluate to 1 because of integer division — but that is not a concern for modern Python 3 projects.

Q3: How do I convert a list of Celsius values all at once?
A: You can use a list comprehension together with the conversion function:

1
2
3
4
celsius_list = [0, 20, 37, 100]
fahrenheit_list = [CelsiusToFahrenheit(c) for c in celsius_list]
print(fahrenheit_list)
# Output: [32.0, 68.0, 98.6, 212.0]

This is a concise, Pythonic way to apply a function to every element of a list in a single line.

We hope you guys have found this worthwhile project and learned how to convert Celsius to Fahrenheit using Python. Also, we encourage and challenge you to make a python program for converting Fahrenheit to Celsius, and you can follow this tutorial to convert Fahrenheit to Celsius.

Khushal Jethava
Khushal Jethava

Machine Learning Engineer at Codiste, specializing in Generative AI, NLP, and Computer Vision. Building production AI systems with Python.

This post is licensed under CC BY 4.0 by the author.