Writing Your First Python Program

(This article on Writing Your First Python Program is part of a text on Python, thus is a continuation of a process.)

Start IDLE (or whatever IDE you are using). Open a new window. (You can choose New Window under the File menu in the top bar.)

Our first program is going to be a simple Celsius To Fahrenheit Converter. The code for it is below.

temp = eval((input(‘Enter a temperature in Celsius: ‘)))
print(‘In Fahrenheit, that is’, 9/5*temp+32)

Once you have input the code, you will have to save the file, or else it won’t run. Make sure that your file ends with a .py file type. Then, run the program. Either navigate to the Run menu and click on Run Module (or use the shortkey F5). This will open a new window where your program will run, and you will be prompted to input a Celsius value to convert.

Test it out. If you input $23 \deg C$ — just the number ’23’ is all you should input — your Python program will $73.4 \deg F$ is you have input everything correctly.

Analysing The Code

First we define the value with a name, which is what the temp = means. This is done so that the program remembers the input value and uses it in the second line. In this case, we are going to call it temp (we could also be more precise and call it something like tempcelsius if we are going to create another converter that goes in the opposite direction from Fahrenheit to Celsius later) and use the equal sign to assign our users input value to that specific name.

The first line of the code asks for a value from the user, specifically a temperature in Celsius. This is a way of capturing data. The string in quotes ‘Enter a temperature in Celsius: ,’ is displayed to the user. (You can type in whatever you want, phrase the data you are looking for in whatever way you want like ‘Celsius: ,’ or ‘Convert the Celsius Temperature: ,’ any of which will work.)

These values inside the single quotations ‘…’ are called a string. These appear to the user which, in this case, give an input instruction to the user.

The reason why we use eval will become clear later on, but for now, we use it whenever we require a numerical input (such as a temperature in this case).

The second line is the print function. The purpose of this second line is to take the input of the first line, perform a conversion, and print it for the user. The string in quotations ‘In Fahrenheit, that is’ is displayed to the user.

The second argument within the print function is the calculation, the equation that is used to actually convert our Celsius value into Fahrenheit: 9/5*temp+32. Python performs the calculation for us, and prints the value for the user.

Notes

<— Notes import sys if not sys.version_info.major == 3 and sys.version_info.minor >= 6:
print(‘Python 3.6 or higher is required.’)
print(‘You are using Python {}.{}.’.format(sys.version_info.major, sys.version_info.minor))
sys.exit() —>

Resources

[latexpage]

Cite This Article

MLA

West, Brandon. "Writing Your First Python Program". Projeda, November 28, 2024, https://www.projeda.com/writing-first-python-program/. Accessed May 2, 2025.

  • Categories