Using the IDLE Code Editor
Welcome back! This time we are going to use Python’s built-in code editor to write a simple application that greets us and tells us how many days we’ve lived so far. It won’t read our minds, so we’ll have to provide our name and age, but the code will do the rest of the job for us.
Click Windows’ Start button, or launch the macOS Finder application. Type “idle” (without using the quotation marks) to launch IDLE, the code editor that comes with the Python installation package. Here’s what you should see when searching for IDLE on a Windows-based computer:
The displayed Python version may be different compared to the one in the image above. New versions are released periodically, adding new features and fixing bugs; however, the core Python functionality will not change.
Here’s how the IDLE editor looks:
We can use “print” instructions to turn Python into a calculator, the way we did in our previous tutorial.
However, one of the key advantages of using a dedicated Python editor is the ability to save files with our work and reuse them later, when we want to run the application again without having to type in the code over and over.
Let’s do that right now! Click the “File” menu at the top of the screen, and then choose “New File” to create a new Python
code file.
Type in the following lines of code, one after the other, exactly as they are:
name = input ("What is your name? ")
age = input ("What is your age? ")
print ("Hi", name)
print ("You are", int(age) * 365, "days old")
This is how the code is supposed to look in the IDLE editor:
Click the “Run” menu option at the top of the screen, and then choose “Run Module”. The editor will prompt you to save the newly created file, so pick a name and do that. IDLE will ask you to save the file anytime you change the source code; otherwise, the Python interpreter wouldn’t be able to run the most recent version of it.
Hopefully, you have typed in the code without errors, and you will be greeted by the application window:
If you run into trouble, you can simply copy/paste the code above. However, we recommend typing the code in, because the potential errors you’re going to make (and fix) will help you become a better programmer.
That’s all for now. Don’t worry about understanding how the code works; we cover that in the following tutorial.