Easy Temperature Conversions with Python: A Beginner’s Guide (Celsius, Fahrenheit, Kelvin)

Basic Temperature Conversion Program

This program takes a temperature input from the user, identifies the scale (Celsius, Fahrenheit, Kelvin), and converts it to the other two scales. Let’s break down the code step-by-step.

For higher accuracy in the Kelvin scale use 273.15 instead of 273


x = str(input("Enter in which scale you measured temperature (Celsius, Fahrenheit, Kelvin)? Enter C or F or K: "))
# 160 is 5*32  .... equations are simplified
if x=="C" or x=="c":
    c=eval(input("Enter your temperature in Celsius scale: "))
    to_fahrenheit=(9*c+160)/5
    to_kelvin=c+273
    print("Celsius :", c,  "\nFahrenheit :", to_fahrenheit,  "\nKelvin: ", to_kelvin)
    
elif x=="F" or x=="f":
    f=eval(input("Enter your temperature in Fahrenheit scale: "))
    to_celsius=(5*f-160)/9
    to_kelvin=((5*f-160)/9)+273
    print("Celsius :", to_celsius,  "\nFahrenheit :", f,  "\nKelvin: ", to_kelvin)
    

elif x== "K" or x=="k":
    k=eval(input("Enter your temperature in Kalvin scale: "))
    to_celsius=k-273
    to_fahrenheit=(9*(k-273)+160)/5
    print("Celsius :", to_celsius,  "\nFahrenheit :", to_fahrenheit,  "\nKelvin: ", k)



Explanation

>Firstly the this code takes input from user in which scale user measured the temperature

>Then user asked for another input in which scale they want to convert

  • The user is prompted to enter the temperature scale: ‘C’ for Celsius, ‘F’ for Fahrenheit, or ‘K’ for Kelvin.
  • Conversion Calculations:
  • Depending on the input scale, the program calculates the temperature in the other two scales using the following formulas:

Celsius to Fahrenheit:

\[ F = \frac{9}{5}C + 32 \]

Celsius to Kelvin:

\[ K = C + 273 \]

Fahrenheit to Celsius:

\[ C = \frac{5}{9}(F – 32) \]

Fahrenheit to Kelvin:

\[ K = \frac{5}{9}(F – 32) + 273 \]

Kelvin to Celsius:

\[ C = K – 273 \]

Kelvin to Fahrenheit:

\[ F = \frac{9}{5}(K – 273) + 32 \]

Complete code with some added feature


x = str(input("Enter in which scale you measured temperature (Celsius, Fahrenheit, Kelvin)? Enter C or F or K: "))
# 160 is 5*32  .... equations are simplified
if x == "C" or x == "c":
    c = eval(input("Enter your temperature in Celsius scale: "))
    to_fahrenheit = (9 * c + 160) / 5
    to_kelvin = c + 273
    while to_kelvin < 0:
        print("Temperature cannot be less than absolute zero")
        c = eval(input("Enter your temperature in Celsius scale: "))
        to_fahrenheit = (9 * c + 160) / 5
        to_kelvin = c + 273
    print("Celsius:", c, "\nFahrenheit:", to_fahrenheit, "\nKelvin:", to_kelvin)
    
elif x == "F" or x == "f":
    f = eval(input("Enter your temperature in Fahrenheit scale: "))
    to_celsius = (5 * f - 160) / 9
    to_kelvin = ((5 * f - 160) / 9) + 273
    while to_kelvin < 0:
        print("Temperature cannot be less than absolute zero")
        f = eval(input("Enter your temperature in Fahrenheit scale: "))
        to_celsius = (5 * f - 160) / 9
        to_kelvin = ((5 * f - 160) / 9) + 273
    print("Celsius:", to_celsius, "\nFahrenheit:", f, "\nKelvin:", to_kelvin)
    
elif x == "K" or x == "k":
    k = eval(input("Enter your temperature in Kelvin scale: "))
    while k < 0:
        print("Temperature cannot be less than absolute zero")
        k = eval(input("Enter your temperature in Kelvin scale: "))
    to_celsius = k - 273
    to_fahrenheit = (9 * (k - 273) + 160) / 5
    print("Celsius:", to_celsius, "\nFahrenheit:", to_fahrenheit, "\nKelvin:", k)




Related Posts

Calculate time dilation with python
June 16, 2024

In this example imagine two object one is Earth And another object is traveling in the space \[ t_0 = t \sqrt{1 – \frac{v^2}{c^2}} \] t0: Represents the dilated time experienced by the object which is traveling or seems traveling from earth t: Time passed on earth v: Velocity of the traveling object. c: Speed […]

How to convert text to sentence with python and nltk
June 16, 2024

We can easily convert text to sentence in python with nltk by tokenize sentence.. First install nltk Download nltk data . NLTK requires data files for tokenization. run the code in a python program to download the data. import nltk nltk.download(‘punkt’) Copy Code After download the necessary data from nltk import tokenize text=”In the symphony […]

How to get all possible combinations with python
June 16, 2024

Python is rich with wonderful standard library so you do not have to write everything from the very beginning. With permutations from iterators we can get wonderful and useful result with less code >In this example code we get the user input as string > Get the user input length > Then run a for […]