Temperature Conversion Program in Python
What is Celsius?
Celsius (°C) is a unit of temperature measurement in the metric system, named after Swedish astronomer Anders Celsius. It is used to measure temperature on the Celsius scale, which sets the freezing point of water at 0°C and the boiling point of water at 100°C, at standard atmospheric pressure.
What is Fahrenheit?
Fahrenheit (°F) is a unit of temperature measurement in the Imperial system, named after German physicist Daniel Gabriel Fahrenheit. It is used to measure temperature on the Fahrenheit scale, which sets the freezing point of water at 32°F and the boiling point of water at 212°F, at standard atmospheric pressure. The Fahrenheit scale is commonly used in the United States and some other countries for weather forecasting and other purposes.
What is Kelvin?
Kelvin (K) is a unit of temperature measurement in the International System of Units (SI), named after British physicist and mathematician William Thomson, also known as Lord Kelvin. It is used to measure temperature on the Kelvin scale, which sets the zero point at absolute zero (-273.15°C or -459.67°F) and uses the same unit size as Celsius degrees. Kelvin is the primary unit of temperature measurement in scientific and engineering applications, particularly those involving thermodynamics and materials science.
Conversion from Celsius to Fahrenheit
The formula to convert Celsius (°C) to Fahrenheit (°F) is:
°F = (°C × 9/5) + 32
Therefore, to convert a temperature from Celsius to Fahrenheit in Python, you can define a function like this:
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return Fahrenheit
This function takes a temperature in Celsius as input, multiplies it by 9/5, adds 32 to the result, and returns the equivalent temperature in Fahrenheit.
You can call this function with a Celsius temperature value like this:
Example:
celsius = 25
fahrenheit = celsius_to_fahrenheit(celsius)
print(fahrenheit)
Output:
77.0
This will convert the Celsius temperature of 25°C to its equivalent Fahrenheit temperature of 77°F.
Conversion from Fahrenheit to Celsius
The formula to convert Fahrenheit (°F) to Celsius (°C) is:
°C = (°F - 32) × 5/9
Therefore, to convert a temperature from Fahrenheit to Celsius in Python, you can define a function like this:
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return Celsius
This function takes a temperature in Fahrenheit as input, subtracts 32 from it, multiplies the result by 5/9, and returns the equivalent temperature in Celsius.
You can call this function with a Fahrenheit temperature value like this:
fahrenheit = 77
celsius = fahrenheit_to_celsius(fahrenheit)
print(celsius)
Output:
25.0
This will convert the Fahrenheit temperature of 77°F to its equivalent Celsius temperature of 25°C.
Celsius and Kelvin Relation
Celsius (°C) and Kelvin (K) are two temperature scales that are closely related to each other. In fact, one degree Celsius is equivalent to one Kelvin, and the two scales have the same size unit.
The relationship between Celsius and Kelvin can be expressed mathematically as:
Kelvin = Celsius + 273.15
This means that to convert a temperature from Celsius to Kelvin, you simply need to add 273.15 to the Celsius temperature value.
Here is an example to illustrate the relationship between Celsius and Kelvin:
Example:
celsius = 20.0
kelvin = celsius + 273.15
print(kelvin)
Output:
293.15
In this example, we start with a Celsius temperature value of 20.0°C. We then add 273.15 to it to convert it to Kelvin. The resulting Kelvin temperature is 293.15 K.
Conversely, if you have a temperature in Kelvin and want to convert it to Celsius, you can use the formula:
Celsius = Kelvin - 273.15
Here's an example of converting a Kelvin temperature to Celsius:
Example:
kelvin = 300.0
celsius = kelvin - 273.15
print(celsius) # output: 26.85
In this example, we start with a Kelvin temperature value of 300.0 K. We then subtract 273.15 from it to convert it to Celsius. The resulting Celsius temperature is 26.85°C.
Fahrenheit and Kelvin Relation
Fahrenheit (°F) and Kelvin (K) are two temperature scales that can also be related to each other, but the formula for the conversion is a bit more complex than the Celsius-Kelvin conversion.
The relationship between Fahrenheit and Kelvin can be expressed mathematically as:
Kelvin = (Fahrenheit + 459.67) * 5/9
This means that to convert a temperature from Fahrenheit to Kelvin, you need to add 459.67 to the Fahrenheit temperature, multiply the result by 5/9, and get the equivalent Kelvin temperature.
Here's an example to illustrate the relationship between Fahrenheit and Kelvin:
Example:
fahrenheit = 68.0
kelvin = (fahrenheit + 459.67) * 5/9
print(kelvin)
Output:
293.15
In this example, we start with a Fahrenheit temperature value of 68.0°F. We then add 459.67 to it, multiply the result by 5/9, and get the equivalent Kelvin temperature. The resulting Kelvin temperature is 293.15 K.
Conversely, if you have a temperature in Kelvin and want to convert it to Fahrenheit, you can use the formula:
Fahrenheit = Kelvin * 9/5 - 459.67
Here's an example of converting a Kelvin temperature to Fahrenheit:
Example:
kelvin = 300.0
fahrenheit = kelvin * 9/5 - 459.67
print(fahrenheit)
Output:
80.33
In this example, we start with a Kelvin temperature value of 300.0 K. We then multiply it by 9/5, subtract 459.67 from the result, and get the equivalent Fahrenheit temperature. The resulting Fahrenheit temperature is 80.33°F.
Here is an example Python program that converts temperature between Celsius, Fahrenheit and Kelvin:
Temperature Conversion Program
def celsius_to_fahrenheit(celsius):
fahrenheit = (celsius * 9/5) + 32
return fahrenheit
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius
def celsius_to_kelvin(celsius):
kelvin = celsius + 273.15
return kelvin
def kelvin_to_celsius(kelvin):
celsius = kelvin - 273.15
return celsius
def fahrenheit_to_kelvin(fahrenheit):
kelvin = (fahrenheit - 32) * 5/9 + 273.15
return kelvin
def kelvin_to_fahrenheit(kelvin):
fahrenheit = (kelvin - 273.15) * 9/5 + 32
return fahrenheit
# main program
print("Temperature Conversion Program\n")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
print("3. Celsius to Kelvin")
print("4. Kelvin to Celsius")
print("5. Fahrenheit to Kelvin")
print("6. Kelvin to Fahrenheit")
choice = int(input("Enter your choice (1-6): "))
if choice == 1:
celsius = float(input("Enter temperature in Celsius: "))
fahrenheit = celsius_to_fahrenheit(celsius)
print("Temperature in Fahrenheit:", fahrenheit)
elif choice == 2:
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
celsius = fahrenheit_to_celsius(fahrenheit)
print("Temperature in Celsius:", celsius)
elif choice == 3:
celsius = float(input("Enter temperature in Celsius: "))
kelvin = celsius_to_kelvin(celsius)
print("Temperature in Kelvin:", kelvin)
elif choice == 4:
kelvin = float(input("Enter temperature in Kelvin: "))
celsius = kelvin_to_celsius(kelvin)
print("Temperature in Celsius:", celsius)
elif choice == 5:
fahrenheit = float(input("Enter temperature in Fahrenheit: "))
kelvin = fahrenheit_to_kelvin(fahrenheit)
print("Temperature in Kelvin:", kelvin)
elif choice == 6:
kelvin = float(input("Enter temperature in Kelvin: "))
fahrenheit = kelvin_to_fahrenheit(kelvin)
print("Temperature in Fahrenheit:", fahrenheit)
else:
print("Invalid choice. Please enter a number between 1 and 6.")
Output:
Temperature Conversion Program
1. Celsius to Fahrenheit
2. Fahrenheit to Celsius
3. Celsius to Kelvin
4. Kelvin to Celsius
5. Fahrenheit to Kelvin
6. Kelvin to Fahrenheit
Enter your choice (1-6): 2
Enter temperature in Fahrenheit: 344
Temperature in Celsius: 173.33333333333334