top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Converting Temperature in Fahrenheit to Celsius and Vice Versa.

Writer's picture: Nargiz HuseynovaNargiz Huseynova


In this blog, we will talk about Converting Temperature from Fahrenheit to Celsius and vice versa.

The Fahrenheit temperature scale is named for German physicist Daniel Gabriel Fahrenheit and is the measurement of temperature commonly used by the United States (and its associated territories) and by several nations in the Caribbean. On the Fahrenheit scale, water freezes at 32°F and boils at 212°F (at sea level).

The Celsius temperature scale—originally called centigrade and later renamed for Swedish astronomer Anders Celsius—is used almost everywhere else in the world. On the Celsius scale, water freezes at 0°C and boils at 100°C (at sea level).


In order to convert temperature in Fahrenheit to Celsius following formula is used:


C = 5/9 (° F - 32)


And for vice versa,


F = 9/5 ( ° C) + 32


Now we will consider a simple converter app, written in Python language.


temperature = input("Enter the temperature you would like to convert (e.g., 56F, 39C, etc.): ") #Getting input from the user

In the above piece of code, we get the input from a user and assign it to a temperature variable.

degree = int(temperature[0:-1])#Getting digits from the input

Here we declare a new variable, degree, which contains the characters that are digits from the temperature variable.


scale = temperature[-1].upper()#Getting the scale from the input

Scale variable contains the upper case of the last character, F or C, in this example.

result_scale='' #Initializing an empty string

Now we initialize an empty string, result_scale, in order to store resulting scale.


#Converting Celsius to Fahrenheit
if scale == 'C':
    result = int(round((9 * degree) / 5 + 32))
    result_scale = 'Fahrenheit'

The above code checks if the scale that should be converted is Celsius or not, if yes, it uses the relevant formula and rounds the finding, makes it integer data type, and assigns the final result to a variable called result.

In the last line of the code result_scale is the variable that contains the desired scale name, in this case, "Fahrenheit".



#Converting Fahrenheit to Celsius
elif scale == 'F':
    result = int(round((degree - 32) * 5 / 9))
    result_scale = 'Celsius'

Here are the same operations but for converting Fahrenheit to Celsius.



#Warning the user in case of not entering proper input 
else:
    print("Your input is not proper")
    quit()

This code prints a warning that the user has entered a wrong input.


#Printing out the final result
print("The temperature in", result_scale, "is", result, "degrees.")

And here is the final print statement that prints the output.


Let us see an example:

When we run the code the following message pops up and requires us to enter input and if we type "34c" like the following:


Enter the temperature you would like to convert (e.g., 56F, 39C, etc.): 34c

We get the output like this:


The temperature in Fahrenheit is 93 degrees.

Let us say we enter an invalid input such as:

Enter the temperature you would like to convert (e.g., 56F, 39C, etc.): 43s

In this case, the output will be:

Your input is not proper

You can find the GitHub repo for this simple project at this link




2 comments

Recent Posts

See All

2 comentarios


Data Insight
Data Insight
19 sept 2021

Very good post. Could you also provide an example of how to use your code. What is 25 degree Celsius in Fahrenheit for instance?

Me gusta
Nargiz Huseynova
Nargiz Huseynova
19 sept 2021
Contestando a

Thanks for the feedback. I edited the post and provided an example. If there is something else please let me know.

Me gusta

COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page