Converting Temperatures in Python
Stellar temperatures are given in units called Kelvins. The Kelvin scale is unique because it refers to absolute zero, the temperature at which, according to classical physics, matter particles cease to vibrate. This scale reflects the relationship between temperature and the kinetic energy of molecules, which is crucial in thermodynamic calculations.
The Kelvin scale is also presented as a color spectrum: from red, through orange, yellow, white, to blue. At first glance, this may seem counterintuitive, but temperatures closer to red are cooler, while those closer to blue are hotter. In everyday life, we encounter this scale when buying light bulbs or adjusting white balance settings in photography.
Students of the Gdynia Space Academy should know methods for converting temperatures from units less useful in space research, such as Celsius or Fahrenheit. In this article, we'll show you how to make such conversions easily using Python.
To perform the conversion, we need to know the formulas for converting degrees Celsius to Kelvin and degrees Fahrenheit to Kelvin.
Converting degrees Celsius to Kelvin
Converting degrees Celsius to Kelvin is relatively easy. Just add 273.15 to the temperature in degrees Celsius to get the temperature in Kelvin. These scales differ only in the placement of the zero point.
The formula for converting temperature in degrees Celsius to Kelvin looks like this:
T(K) = T(°C) + 273.15
Converting degrees Fahrenheit to Kelvin
Degrees Fahrenheit are calibrated according to the freezing temperatures of brine, water, human body temperature, and boiling water. This makes it a more intuitive measure for people, although it's mainly used in the USA and a few other countries.
The formula for converting temperature in degrees Fahrenheit to Kelvin looks like this:
T(K) = (T(F) − 32) × 5 ⁄ 9 + 273.15
How to make these conversions in Python?
Before we start writing code, we need to determine what information we'll need from the program user:
Which temperature they want to convert: Celsius or Fahrenheit to Kelvin. What is the value of this temperature?
These are quite simple requirements, so any beginner Python programmer should be able to handle them.
Functions for converting temperatures
Let's define 2 functions necessary for our calculations. These will be the celsius_to_kelvin and fahrenheit_to_kelvin functions:
def celsius_to_kelvin(celsius):
return celsius + 273.15
def fahrenheit_to_kelvin(fahrenheit):
return (fahrenheit - 32) * 5/9 + 273.15
Displaying the function selection menu
We display the selection menu using the print() function:
print("Choose the type of conversion:")
print("1. Temperature in degrees Celsius to temperature in degrees Kelvin.")
print("2. Temperature in degrees Fahrenheit to temperature in degrees Kelvin.")
Getting the value
This code is unfortunately not very interactive and won't cause any kind of value retrieval or conversion. Therefore, using the input() function, we can get the menu choice and display the next command.
choice = int(input("Enter 1 or 2 and press enter: "))
We assign the choice entered by the user to the variable choice as a numerical value int.
Performing calculations
Having saved the choice in the choice variable, we can call the appropriate function and get the value for calculations, or inform the user that they entered an incorrect value.
if choice == 1:
celsius = float(input("Enter the temperature in degrees Celsius: "))
kelvin = celsius_to_kelvin(celsius)
print(f"The temperature in degrees Kelvin is: {kelvin:.2f} K")
elif choice == 2:
fahrenheit = float(input("Enter the temperature in degrees Fahrenheit: "))
kelvin = fahrenheit_to_kelvin(fahrenheit)
print(f"The temperature in degrees Kelvin is: {kelvin:.2f} K")
else:
print("Invalid choice. Enter 1 or 2.")
The code contains a complete selection menu and functions for converting temperatures. In the code tab, you can download the full version of the script. As an additional exercise, you can write functions that convert temperatures from Kelvin to degrees Fahrenheit and Celsius, and modify the menu to add these options. Good luck!
Article photo by Greg Rakozy on Unsplash
def celsius_to_kelvin(celsius):
return celsius + 273.15
def fahrenheit_to_kelvin(fahrenheit):
return (fahrenheit - 32) * 5/9 + 273.15
print("Choose the type of conversion:")
print("1. Temperature in degrees Celsius to temperature in degrees Kelvin.")
print("2. Temperature in degrees Fahrenheit to temperature in degrees Kelvin.")
choice = int(input("Enter 1 or 2 and press enter: "))
if choice == 1:
celsius = float(input("Enter the temperature in degrees Celsius: "))
kelvin = celsius_to_kelvin(celsius)
print(f"The temperature in degrees Kelvin is: {kelvin:.2f} K")
elif choice == 2:
fahrenheit = float(input("Enter the temperature in degrees Fahrenheit: "))
kelvin = fahrenheit_to_kelvin(fahrenheit)
print(f"The temperature in degrees Kelvin is: {kelvin:.2f} K")
else:
print("Invalid choice. Enter 1 or 2.")