In this tutorial, you will learn how to write a program in Python to convert temperature from degrees Celsius to Fahrenheit. The program will take input from the user and convert the entered value from a string to an integer. Then, using a formula, the program will convert the temperature from Celsius to Fahrenheit and display the result.
Python Program to Convert Temperature Degrees Celsius to Fahrenheit
Step 1: Understanding the Formula for Conversion
The formula to convert temperature from Celsius to Fahrenheit is as follows:
Fahrenheit = (celsius * 1.8) + 32
Step 2: Writing the Code
- Start by creating a new Python file in your favourite code editor and save it with a .py extension.
- The first thing we need to do is take input from the user. Use the input() function to prompt the user to enter the temperature in Celsius.
- Next, we need to apply the formula to convert the temperature from Celsius to Fahrenheit.
- Finally, we will use the print() function to display the result. The %0.1f format specifier is used to display the result with one decimal place.
Step 3: Running the Program
To run the program, simply save the file and open a terminal or command prompt in the same directory as the file. Then, type the following command and press Enter:
# Python Program to Convert Temperature Degrees Celsius to Fahrenheit # we are taking a number from user as input # entered value will be converted to int from string celsius = int(input("Enter the degree Celsius value:")) # calculate fahrenheit fahrenheit = (celsius * 1.8) + 32 print('%0.1f degree Celsius is equal to %0.1f degree Fahrenheit' %(celsius,fahrenheit))
When the program is run, it will prompt the user to enter a temperature in Celsius. Here’s an example of the output:
Enter the degree Celsius value:40 40.0 degree Celsius is equal to 104.0 degree Fahrenheit
Advantages of Writing a Temperature Conversion Program in Python:
- Easy to Learn: Python is a high-level programming language that is easy to learn and understand. This makes it a great choice for beginners who want to learn how to code.
- Versatile: Python is a versatile language that can be used for a wide range of applications, including web development, scientific computing, data analysis, and more.
- Large Community: Python has a large and active community of developers, which makes it easy to find help and resources when you need them.
- Built-in Libraries: Python has a large number of built-in libraries that can be used for various tasks, including mathematics, data manipulation, and more. This makes it easy to perform complex operations with just a few lines of code.
Disadvantages of Writing a Temperature Conversion Program in Python:
- Performance: Although Python is an excellent choice for prototyping and testing, there may be better choices for resource-intensive applications due to its interpreted nature and the overhead of the Python interpreter.
- Syntax: Python’s syntax can be confusing for some users, especially those who are used to other programming languages.
- Library Availability: While Python has a large number of built-in libraries, not all libraries are available for Python. In some cases, you may need to find an alternative library or write your code to perform a specific task.
- Security: Since Python is an interpreted language, it may be less secure than compiled languages, as the source code is visible to anyone who can access the program.
Congratulations, you have successfully written a temperature conversion program in Python! You can use this program as a starting point for other similar programs, or as a reference to help you understand the concept of temperature conversion.
For More Python Programming Exercises and Solutions check out our Python Exercises and Solutions