Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
This post is the first of a yet-to-be named tutorial series in which we will write short programs or scripts to solve a single problem. In this tutorial, you will write a program that converts celsius temperatures to Fahrenheit temperatures.
Because this is the first post of a planned series, I think it is worthwhile to explain my approach, as well as my philosophy behind providing these tutorials.
In this post, we will go through the process of coding a short program together, step-by-step. For each code block contained in this post, I will include explanations of everything that is going on inside the code. If you wish, you will be able to open a text editor and code right along with the tutorial.
Once we have finished writing our program, we will save it to a .py file and run the program.
If you encounter errors, I encourage you to please leave comments! I would be happy to help troubleshoot if I am able.
My aim with this series is to get you coding. I want you to see how simple it is to write a program when you limit yourself to solving a single problem. For me, these were the first kinds of programs that I began writing for myself.
If you are a beginner, you may not understand everything in this tutorial. That is totally okay! This series is about getting your feet wet and showing you what is possible.
Most importantly, I want this series to inspire you to begin experimenting with your own Python code.
In my experience, the best way to learn and get comfortable with Python (or any programming language, for that matter) is to experiment with it. Experimentation doesn’t have to be structured. Just identify a single problem that you want to solve and see if you can put some code together that solves that problem.
My hope is that by seeing what these short scripts do and how they do it, you will be able to think creatively about how you can use the same concepts in a variety of other applications.
For instance, in this tutorial we are writing a program that converts a temperature reading from celsius to Fahrenheit. At its most basic level, this program takes a number as user input, performs a calculation on that number, then returns the result of that calculation. Is there any other situation in which this basic set of operations might be useful to you?
I hope that you will consider that question as you work through this tutorial. Your experimental code may not be the most elegant or efficient. When I look back at some of my old experiments, it’s laughable. The code is a mess. There are lines commented out all over the place from me testing out different things to see what worked. I see processes that could have been made much simpler. I see places where I should have used classes but didn’t. That is okay!
The point of those experiments, to me, was not to write perfect, elegant code that would be acceptable in a professional setting. The point was to see if I could make Python to do what I wanted it to do (perform a calculation, save a document, etc.). In that regard, nearly all of my experiments were eventually successful.
This is important because the “Eureka!” moment that you experience when your experimental program first runs without error is motivating. Once you see firsthand that you are able to achieve your desired goal extremely quickly using only some code that you wrote, it will boost your confidence, and if you’re anything like me, it will motivate you to keep learning.
With that out of the way, let’s learn how to use Python to convert celsius to Fahrenheit!
Before you begin writing your code, you need to think about what you want your program to do and how you want it to do it. While it may seem silly for a small, simple program like the one that you are working on today, going through this exercise before you start writing your program will help you determine how you will organize your code.
Now that you have determined exactly what you need your program to do, you can start to write the code.
To get the celsius temperature that you will be converting, you will be prompting the user. You can do this using Python’s input()
function.
Before beginning, you should be aware that Python’s input()
function stores the user input received as a string by default. For this reason, we will have to use the int()
or float()
type wrapper to ensure that the value is stored as a number.
If you do not specify a numeric data type for the user input that you receive, you will generate a type error later when attempting to do the conversion later in your program.
So, to take the temperature in degrees celsius from the user and convert that input to an integer value, we can use the following line of code:
#Note how the int() type wrapper is used to ensure that the input is stored
#in a numeric data type.
celsius = int(input("What is the temperature in degrees celsius? Please enter an integer value: ")
In order to achieve our program’s goal, we must determine how to convert a temperature given in degrees celsius to a temperature in degrees Fahrenheit.
The formula for this conversion is is expressed below. In this formula, “CT” refers to the temperature in degrees celsius and “FT” represents the temperature in degrees Fahrenheit.
FT = (CT x 1.8) + 32
Understanding this formula, we can write the following function to do our conversion:
def temp_converter(temp_c):
f = (temp_c * 1.8) + 32
return f
In the first line of this code block, we use the def
keyword to alert Python that we are defining a function. We then name the function temp_converter()
and have it take in a value called temp_c
, which will be the celsius temperature that the function converts. We end the line with a colon to indicate that the following indented block of code is the contents of the function temp_converter()
.
Because your function only needs to accomplish one thing, i.e., converting the temperature from celsius to Fahrenheit, your function only needs to be one line long. In this single line of code, we create a variable called ‘f
‘ that represents the temperature in degrees Fahrenheit. We then plug the temp_c
variable, which represents the user’s input of the temperature in degrees celsius, into the equation for converting a temperature from celsius to Fahrenheit.
Finally, we return the value representing the Fahrenheit temperature with the line return f
.
Although your function returns the temperature in degrees Fahrenheit, it does not show that value to the user. As a result, we’ll need to first store the function’s output in a variable. We’ll then need to add a few more lines of code to our program to output the necessary information to the user.
In the below example, three lines have been added to our program. The first line creates a variable called f_temp
that runs our conversion formula on the celsius temperature provided by the user. The two lines that follow use formatted strings to inform the user of the temperature that was provided in degrees celsius as well as the converted Fahrenheit temperature.
f_temp = temp_converter(celsius)
print(f'You entered a temperature of {celsius} degrees celsius.')
print(f'This converts to {f_temp} degrees Fahrenheit.')
And that’s it!
Your program is now finished. In the next section, we’ll look at what the code for our program looks like all put together.
'''
save your code to a file with extension .py, for instance temp_converter.py.
You can then open a terminal window, navigate to the folder where your .py
file is stored and run the following command to run your program:
python3 my_file_name.py
'''
celsius = int(input("What is the temperature in degrees celsius? Please enter an integer value: ")
def temp_converter(temp_c):
f = (temp_c * 1.8) + 32
return f
f_temp = temp_converter(celsius)
print(f'You entered a temperature of {celsius} degrees celsius.')
print(f'This converts to {f_temp} degrees Fahrenheit.')