How to Convert Celsius to Fahrenheit in Python

How to Convert Celsius to Fahrenheit in Python

3 mins read1K Views Comment
clickHere
Updated on Nov 22, 2022 19:31 IST

One of the most common Python programs for beginners is to convert temperature from Celsius to Fahrenheit and vice versa. This tutorial will help you learn how to perform both of these conversions using Python.

2022_07_thumb-07.jpg

How to Convert Celsius to Fahrenheit in Python?

To understand the following programs, all you need to know are the mathematical formulas for conversion and basic knowledge of the following Python concepts is a must-have:

  • Python Input/Output
  • Python Data Types
  • NumPy arrays

We will be covering the following sections today:

So, without further ado, let’s get started!

What is Celsius?

Named after the Swedish astronomer Anders Celsius, degree Celsius (aka Centigrade), is the SI unit of measurement for temperature. It is the most widely accepted unit used by most countries worldwide.

What is Fahrenheit?

Named after the Polish-born German physicist Daniel Gabriel Fahrenheit, we have another temperature scale that uses degrees Fahrenheit as a unit for temperature. This is the standard adapted unit used by the US and its territories.

Conversion Formulas

Celsius to Fahrenheit:

Fahrenheit = (Celsius * 9/5) + 32

Fahrenheit to Celsius:

Celsius = (Fahrenheit – 32) * 5/9

Convert Celsius to Fahrenheit

Using the conversion formula:

#Celsius to Fahrenheit
 
#Input temperature in Celsius
celsius = float(input("Enter temperature in Celsius: "))
 
#Apply the conversion formula
fahrenheit = (celsius * 9/5) + 32
 
print('%.2f degrees Celsius is equivalent to: %0.2f degrees Fahrenheit' %(celsius, fahrenheit))

Output:

output9

Using NumPy arrays:

  1. np.array() method
#Celsius to Fahrenheit
 
#Import numpy module
import numpy as np
 
#Take Celsius inputs
inp = [0, 11.8, 49.19, 74, 99.99]
 
#Store the inputs in the array
celsius = np.array(inp)
 
print(f"Celsius {celsius}")
 
#Apply the conversion formula
fahrenheit = (celsius * 9/5) + 32
 
#Print the results
print(f"Fahrenheit {fahrenheit}")

Output:

output8
  1. np.asarray() method
#Celsius to Fahrenheit
 
#Import numpy module
import numpy as np
 
#Take Celsius inputs
inp = [0, 11.8, 49.19, 74, 99.99]
 
#Store the inputs in the array
celsius = np.asarray(inp)
 
print(f"Celsius {celsius}")
 
#Apply the conversion formula
fahrenheit = (celsius * 9/5) + 32
 
#Print the results
print(f"Fahrenheit {fahrenheit}")
 

Output:

output7
  1. np.arange() method
#Celsius to Fahrenheit
 
#Import numpy module
import numpy as np
 
#Take Celsius inputs
inp = [0, 11.8, 49.19, 74, 99.99]
 
#This method will not directly convert into numpy array
celsius = np.asarray(5)
 
celsius = [i for i in inp]
 
print(f"Celsius {celsius}")
 
#Apply the conversion formula
fahrenheit = [((i * 9/5) + 32) for i in celsius]
 
#Print the results
print(f"Fahrenheit {fahrenheit}")

Output:

output6

Now, let’s see how to perform conversions from Fahrenheit to Celsius. The methods are going to be exactly the same – only the conversion formulas will differ:

Convert Fahrenheit to Celsius

Using the conversion formula:

#Fahrenheit to Celsius
 
#Input temperature in Fahrenheit
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
 
#Apply the conversion formula
celsius = (fahrenheit - 32 ) * 5/9
 
print('%.2f degrees Fahrenheit is equivalent to: %0.2f degrees Celsius' %(fahrenheit, celsius))

Output:

output4

Using NumPy arrays:

  1. np.array() method
#Fahrenheit to Celsius
 
#Import numpy module
import numpy as np
 
#Take Fahrenheit inputs
inp = [32, 53.2, 120, 165.2, 99.99]
 
#Store the inputs in the array
fah = np.array(inp)
 
print(f"Fahrenheit {fah}")
 
#Apply the conversion formula
celsius = (fah - 32 ) * 5/9
 
#Print the results
print(f"Celsius {celsius}")

Output:

output3
  1. np.asarray() method
#Fahrenheit to Celsius
 
#Import numpy module
import numpy as np
 
#Take Fahrenheit inputs
inp = [32, 53.2, 120, 165.2, 99.99]
 
#Store the inputs in the array
fah = np.asarray(inp)
 
print(f"Fahrenheit {fah}")
 
#Apply the conversion formula
celsius = (fah - 32 ) * 5/9
 
#Print the results
print(f"Celsius {celsius}")
 

Output:

output2
  1. np.arange() method
#Fahrenheit to Celsius
 
#Import numpy module
import numpy as np
 
#Take Fahrenheit inputs
inp = [32, 53.2, 120, 165.2, 99.99]
 
#This method will not directly convert into numpy array
fah = np.asarray(5)
 
fah = [i for i in inp]
 
print(f"Fahrenheit {fah}")
 
#Apply the conversion formula
celsius = [((i - 32 ) * 5/9) for i in fah]
 
#Print the results
print(f"Celsius {celsius}")

Output:

output1

Endnotes

Hope this article will be helpful in teaching you how to convert the temperature from Celsius to Fahrenheit and vice-versa in Python. If you wish to learn more about Python and practice Python programming, you can explore related articles here.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio