Sep. 22, 2012

Python Code: Celsius and Fahrenheit Converter


This script converts temperature between Fahrenheit to Celsius. 

To create a python converter for celsius and fahrenheit, you first have to find
out which formula to use.
Fahrenheit to Celsius formula:

(°F - 32) x 5/9 = °C or in plain english, First subtract 32, then multiply by 5,
then divide by 9.
Celsius to Fahrenheit formula:

(°C × 9/5) + 32 = °F or in plain English, Multiple by 9, then divide by 5, then
add 32.
Convert Fahrenheit to Celsius
#!/usr/bin/env python
Fahrenheit = int(raw_input("Enter a temperature in Fahrenheit: "))

Celsius = (Fahrenheit - 32) * 5.0/9.0

print "Temperature:", Fahrenheit, "Fahrenheit = ", Celsius, " C"
Convert Celsius to Fahrenheit
#!/usr/bin/env python
Celsius = int(raw_input("Enter a temperature in Celsius: "))

Fahrenheit = 9.0/5.0 * Celsius + 32

print "Temperature:", Celsius, "Celsius = ", Fahrenheit, " F"
Read and understand the script. Happy scripting!

Share this article

Recommended Python Training – DataCamp

For Python training, our top recommendation is DataCamp.

Datacamp provides online interactive courses that combine interactive coding challenges with videos from top instructors in the field.

Datacamp has beginner to advanced Python training that programmers of all levels benefit from.


Download Our Free Guide To Learning Python

* indicates required

Read more about:
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, only recommend products or services that we try personally and believe will add value to our readers.