Python Tutorial (33) - Example: Convert Celsius to Fahrenheit

Time: Column:Python views:217

Convert Celsius to Fahrenheit in Python

The following example demonstrates how to convert Celsius temperature to Fahrenheit.

Example:

# -*- coding: UTF-8 -*-

# User input for Celsius temperature
celsius = float(input('Enter temperature in Celsius: '))

# Convert Celsius to Fahrenheit
fahrenheit = (celsius * 1.8) + 32
print('%0.1f Celsius is equal to %0.1f Fahrenheit' % (celsius, fahrenheit))

When the above code is executed, it prompts the user to enter a temperature in Celsius and then converts it to Fahrenheit using the formula.

Output:

Enter temperature in Celsius: 38
38.0 Celsius is equal to 100.4 Fahrenheit

In this example, the formula used to convert Celsius to Fahrenheit is:

Fahrenheit=(Celsius×1.8)+32 ext{Fahrenheit} = ( ext{Celsius} imes 1.8) + 32

The reverse calculation for converting Fahrenheit to Celsius is:

Celsius=Fahrenheit321.8 ext{Celsius} = rac{ ext{Fahrenheit} - 32}{1.8}