Python

# How to Convert Fahrenheit to Celsius using Python

Both Fahrenheit and Celsius are used for temperature measurement. German physicist Daniel Gabriel Fahrenheit is the inventor of the Fahrenheit measurement scale, and the unit of this measurement is defined by the degree. The water freezes at 32 degrees Fahrenheit and water boils at 212 degrees Fahrenheit. Swedish astronomer Andres Celsius is the inventor of the Celsius measurement scale, and the unit of this measurement is also defined by the degree. The water freezes at 0 degrees Celsius, and water boils at 100 degrees Celsius. Different ways to convert the Fahrenheit scale to the Celsius scale using python script are shown in this tutorial.

## Formula to convert Fahrenheit to Celsius

The following formula is used to convert the temperature from the Fahrenheit scale to the Celsius scale. Here, C indicates the value in Celsius, and F indicates the value in Fahrenheit. This formula can be used in different ways to calculate the value of Celsius from the Fahrenheit value.

C = (5/9) * (F - 32)

## Convert Fahrenheit to Celsius using Function

The following script shows the way to convert the temperature from Fahrenheit to Celsius by using the Function. The value of the Fahrenheit value will be taken from the user.Â  ConvertFtoC() function will take the Fahrenheit value by the argument, and the Function will return the Celsius value after converting the Fahrenheit to Celsius. Both Fahrenheit and Celsius values will be printed later.

# Define function to convert fahrenheit to celsius

def ConvertFtoC(F):

# Convert the Fahrenheit into Celsius

C = (5 / 9) * (F - 32)

# Return the conversion value

return C

# Take the Fahrenheit value from the user

F = float(input("Enter the temperature in Fahrenheit: "))

# Print the Fahrenheit value

print("Temperature in Fahrenheit = {:.2f}".format(F))

# Print the Celsius value

print("Temperature in Celsius = {:.2f}".format(ConvertFtoC(F)))

## Output

The following output will appear after executing the code. The output shows that 7.22 degrees Celsius is the value of 45 degrees Fahrenheit.

## Convert Fahrenheit to Celsius using Class

The following script shows the way to convert the temperature from Fahrenheit to Celsius by using Class. The Conversion class is defined in the script that contains ConvertFtoC() method to convert the Fahrenheit value to Celsius value. The Fahrenheit value will be taken from the user here and call the ConvertFtoC() method of the Class by creating the object of the Conversion class.

# Define the class to convert fahrenheit to celsius

class Conversion:

def ConvertFtoC(self, F):

# Convert the Fahrenheit into Celsius

C = (5 / 9) * (F - 32)

# Return the conversion value

return C

# Take the Fahrenheit value from the user

F = float(input("Enter the temperature in Fahrenheit: "))

# Create object

object = Conversion()

# Get the celsius value

C = object.ConvertFtoC(F)

# Print the Fahrenheit value

print("Temperature in Fahrenheit = {:.2f}".format(F))

# Print the Celsius value

print("Temperature in Celsius(Using Class) = {:.2f}".format(C))

## Output

The following output will appear after executing the code. The output shows that 10 degrees Celsius is the value of 45 degrees Fahrenheit.

## Convert Fahrenheit to Celsius using Form

The following script shows the way of converting Fahrenheit to Celsius by using GUI (Graphical User Interface). Qapplication, QMainWindow, QLabel, QtextEdit, and QpushButton modules of PyQt5 have been imported in the script to create a dialog box with label, text box, and button. A window has defined at the beginning of the constructor method of ConvertFtoC class. Next, a text box has defined with a label and a Pushbutton to take the Fahrenheit value from the user. Another label has been defined to show the Celsius value after converting the Fahrenheit value. onClicked() method associated with the push button has been defined in the Class to calculate and print the Celsius value with the formatting in the label. When the user clicks on the Push button after entering the Fahrenheit value in the text box, the onClicked() method will be called, and the corresponding Celsius value will be displayed.

# Import necessary modules

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QTextEdit, QPushButton

class ConvertFtoC(QMainWindow):

def __init__(self):

# Call the parent constructor

super().__init__()

# Set the title of the window

self.setWindowTitle("Fahrenheit to Celsius Conversion")

# Set the width and height of the window

self.resize(350, 200)

# Move the position of the window

self.move(800, 400)

# Create label for the first textbox

self.lbl = QLabel('Enter temperature in Fahrenheit', self)

self.lbl.setGeometry(50, 20, 250, 50)

# Create textbox to take fahrenheit value

self.textbox = QTextEdit(self)

self.textbox.setGeometry(50, 60, 70, 30)

# Create push button to get the celsius value

self.submit = QPushButton('Convert to Celsius', self)

self.submit.setGeometry(50, 100, 190, 30)

# Create label for show the result

self.lblResult = QLabel('', self)

self.lblResult.setGeometry(50, 130, 250, 50)

# Call function when the button is clicked

self.submit.clicked.connect(self.onClicked)

# Display the window

self.show()

def onClicked(self):

# Read the fahrenheit value

F = int(self.textbox.toPlainText())

# Calculate the celsius value

C = (5 / 9) * (F - 32)

# Format the output

output = "<p style='color:blue'>The temperature in celsius is " + str(C) + '</p>'

self.lblResult.setText(output)

# Create object PyQt application

app = QApplication([])

# Create window object

window = ConvertFtoC()

# Start the event loop for executing the application

app.exec(

## Output

The following similar output will appear after executing the code. Here, 50 has taken as the Fahrenheit value. When the user has pressed the Convert to Celsius button, then 10 has printed as the Celsius value.

## Conclusion

Three different ways of converting the Fahrenheit value to the Celsius value have been shown in this tutorial by using simple examples. The first two examples show the conversion using the Class and Function that generates the output in the console. The last example shows the conversion by using GUI.

### About the author

#### Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.