Taking Input from the User to Convert Fahrenheit to Celsius in Python
We can allow users to enter a temperature in Fahrenheit and convert it to Celsius. The formula for this conversion is:
Celsius=(Fahrenheit−32)×95
Python Program
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
celsius = (fahrenheit - 32) * 5/9
return celsius
# Taking input from the user
fahrenheit_temp = float(input("Enter temperature in Fahrenheit: "))
celsius_temp = fahrenheit_to_celsius(fahrenheit_temp)
print(f"{fahrenheit_temp} degrees Fahrenheit is equal to {celsius_temp:.2f} degrees Celsius.")

You can also try this code with Online Python Compiler
Run Code
Example Output:
Enter temperature in Fahrenheit: 98.6
98.6 degrees Fahrenheit is equal to 37.00 degrees Celsius.
Explanation:
- We define a function fahrenheit_to_celsius that applies the conversion formula.
- The program takes user input, converts it to a float, and passes it to the function.
- The output is formatted to show two decimal places.
Python Program to Convert Fahrenheit to Celsius
Let's write a complete program that converts Fahrenheit to Celsius using a function.
# Function to convert Fahrenheit to Celsius
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
# Example
fahrenheit_temp = 50
celsius_temp = fahrenheit_to_celsius(fahrenheit_temp)
print(f"{fahrenheit_temp} degrees Fahrenheit is equal to {celsius_temp:.2f} degrees Celsius.")

You can also try this code with Online Python Compiler
Run Code
Output:
50 degrees Fahrenheit is equal to 10.00 degrees Celsius.
Explanation:
- This standalone program performs the conversion for a fixed value of 50°F.
- The formula correctly converts 50°F to 10°C.
Taking Input from the User to Convert Celsius to Fahrenheit in Python
Now, let's create a program that takes user input in Celsius and converts it to Fahrenheit.
# Function to convert Celsius to Fahrenheit
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
# Taking input from the user
celsius_temp = float(input("Enter temperature in Celsius: "))
fahrenheit_temp = celsius_to_fahrenheit(celsius_temp)
print(f"{celsius_temp} degrees Celsius is equal to {fahrenheit_temp:.2f} degrees Fahrenheit.")

You can also try this code with Online Python Compiler
Run Code
Example Output:
Enter temperature in Celsius: 37
37 degrees Celsius is equal to 98.60 degrees Fahrenheit.
Explanation:
- The program takes user input and passes it to the function.
- The conversion formula is applied, and the result is printed.
Time & Space Complexity
Time Complexity
Time complexity refers to how much time the program takes to run as the input size grows. In our case, the input is a single temperature value in Celsius. The program performs a fixed number of operations:
1. Multiplication (`C × 9/5`)
2. Addition (`+ 32`)
Since the number of operations doesn’t change regardless of the input value, the time complexity is O(1). This means the program runs in constant time.
Space Complexity
Space complexity refers to how much memory the program uses. In our program, we only store a few variables:
1. The input temperature in Celsius (`C`).
2. The calculated temperature in Fahrenheit (`F`).
No additional memory is used as the input size grows. Therefore, the space complexity is also O(1), meaning the program uses constant space.
Why Does This Matter?
Even though this program is simple, understanding time & space complexity is crucial for more complex programs. It helps you write efficient code, especially when dealing with large datasets or performance-critical applications.
Frequently Asked Questions
How do I convert Celsius to Fahrenheit in Python?
Use the formula (celsius * 9/5) + 32 in a function to convert Celsius to Fahrenheit. This calculation is commonly used in scientific and weather applications.
Can I use a lambda function to convert Celsius to Fahrenheit?
Yes, a lambda function can simplify conversion. Example: lambda c: (c * 9/5) + 32. This method is useful for quick calculations in Python programs.
What is the difference between Fahrenheit and Celsius?
Celsius is based on the freezing (0°C) and boiling (100°C) points of water, whereas Fahrenheit uses 32°F as freezing and 212°F as boiling, commonly used in the US.
Conclusion
In this article, we discussed how to write a Python program to convert Celsius to Fahrenheit using the formula F = (C × 9/5) + 32. This simple program helps in temperature conversions and is useful in various scientific and real-world applications. Understanding this concept enhances basic programming skills and mathematical computations in Python.
Recommended Readings: