Python Programs | IT Developer
IT Developer

Python Programs



Share with a Friend

Python Programs - Functions

Function to convert Celsius to Fahrenheit - Python Program

Example 1 :

def celsius_to_fahrenheit(c): """Convert Celsius to Fahrenheit.""" return (c * 9/5) + 32 print(celsius_to_fahrenheit(0))

Output

 
OUTPUT  :
32.0 

A Python function can be written to convert a temperature from Celsius to Fahrenheit. The conversion formula is

 

𝐹=(𝐶 × 95) + 32, where F is the temperature in Fahrenheit and 𝐶 is the temperature in Celsius.

Example 2 : Advanced Program

def celsius_to_fahrenheit(celsius): """ Converts a temperature from Celsius to Fahrenheit. Args: celsius: The temperature in Celsius (float or int). Returns: The equivalent temperature in Fahrenheit (float). """ fahrenheit = (celsius * 9/5) + 32 return fahrenheit # Example usage: celsius_temp = 25 fahrenheit_temp = celsius_to_fahrenheit(celsius_temp) print(f"{celsius_temp} degrees Celsius is equal to {fahrenheit_temp} degrees Fahrenheit.") celsius_temp_2 = 0 fahrenheit_temp_2 = celsius_to_fahrenheit(celsius_temp_2) print(f"{celsius_temp_2} degrees Celsius is equal to {fahrenheit_temp_2} degrees Fahrenheit.")

Output

 
OUTPUT  :
25 degrees Celsius is equal to 77.0 degrees Fahrenheit.
0 degrees Celsius is equal to 32.0 degrees Fahrenheit.

Explanation:

celsius_to_fahrenheit(celsius) function:

  • This function takes one argument, celsius, which represents the temperature in Celsius.
  • Inside the function, the formula (celsius * 9/5) + 32is applied to convert the Celsius temperature to Fahrenheit.
  • The calculated Fahrenheit temperature is then returned by the function.