We can measure temperature using two units: Celsius and Fahrenheit. We can convert temperature from degrees Celsius to degrees Fahrenheit using a formula.
12 degrees Celsius is equal to 53.6 degrees Fahrenheit.
This tutorial will go through how to convert between the temperature units and write a Python program to perform the conversion.
Table of contents
Formula to Convert Celsius to Fahrenheit
The formula to convert Celsius (C) to Fahrenheit (F) is:
F = (C x 9/5) + 32
Formula to Convert Fahreneit to Celsius
The formula to convert Fahrenheit (F) to Celsius (C) is:
C = (F - 32) x 5/9
Python Program to Convert Celsius to Fahrenheit
Let’s write a program to convert a temperature value from Celsius to Fahrenheit.
def temp_converter(value, is_celsius=True):
if is_celsius:
F = (value * (9/5)) + 32
print(f'{value} Celsius is {round(F, 1)} Fahrenheit')
else:
C = (value - 32) * (5/9)
print(f'{value} Fahrenheit is {round(C, 1)} Celsius')
In the above code, we have a function that takes two parameters, value, which is the temperature we want to convert and is_celsius, a Boolean that tells the function that the value is in Celsius if True or Fahrenheit False.
If is_celsius is True, then the function converts to Fahrenheit and prints the converted value to the console using an f-string.
If is_celsius is False, the function converts to Celsius and prints the converted value rounded to one decimal place to the console using an f-string.
If is_celsius is False, the function converts to Celsius and prints the converted value rounded to one decimal place to the console using an f-string.
Python Convert Celsius to Fahrenheit
We can take a value from the user using the input() and float() and pass it to the temp_converter function as follows:
value = float(input('Enter a temperature: '))
temp_converter(value)
Let’s run the code to see the output:
12.0 Celsius is 53.6 Fahrenheit
Python Convert Fahrenheit to Celsius
If we want the function to convert Fahrenheit to Celsius, we can set the is_celsius flag to False. Let’s pass the value 53.6 Fahrenheit to the function:
value = float(input('Enter a temperature: '))
temp_converter(value, False)
53.6 Fahrenheit is 12.0 Celsius
Summary
Congratulations on reading to the end of this tutorial! We have gone through how to convert temperatures in Celsius to Fahrenheit and Fahrenheit to Celsius, and we have gone through how to write a program to do temperature conversion.
Go to the online courses page on Python to learn more about Python for data science and machine learning.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.