• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Python Tutorial
  • Python Basics
  • Python Code Examples

 

You are here: Home / Code Snippets / Python Code: Celsius and Fahrenheit Converter

Python Code: Celsius and Fahrenheit Converter

Last Updated: May 20, 2020

Overview

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!

Recommended Python Training

For Python training, our top recommendation is DataCamp.

Free Trial

Filed Under: Code Snippets Date Originally Published: September 22, 2012

More Python Topics

API Argparse Argv Basics Beautiful Soup Cheatsheet Code Snippets Collection Comments Concatenation Control Flow CSV Development Dictionary Error Handling Feedparser Files GUI Json Loops Modules OS Python On The Web Random Regex Requests Super System System & OS

Primary Sidebar

Get Our Free Guide To Learning Python

Menu

  • Python Basics
  • Code Examples
  • Loops
  • Functions
  • Strings
  • Python on the Web
  • Lists
  • Dictionaries
  • Python Modules
  • Python Glossary
  • Learn Python

Most Popular Content

  • Reading and Writing Files in Python
  • String Concatenation and Formatting
  • List Comprehensions in Python
  • How to use sys.argv in Python
  • How to use Split in Python
  • How to use comments in Python
  • Python Syntax Basics

Recent Posts

  • Python 2 Vs Python 3 with Examples
  • How To Run Your Python Scripts
  • The 5 Best Python IDE’s and Code Editors for 2019
  • Python Syntax Basics
  • Hello Developers. Meet Python. The King of Growth

Copyright © 2012–2020 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us