For more information on concepts covered in this lesson, you can check out:
Write a Python Script
00:13 It has two main windows, and you’ll spend some time in both of them. The first is called the interactive window, and I’ll share that in just a second. Then the editor window, which is where you actually write scripts and programs that you’ll save and run later. All right, let’s take a look at the interactive window.
00:31 If you don’t already have IDLE open, then go ahead and open it. You can find it in the folder that the application was installed in. So in my case, I have several versions of Python here, and I’m going to choose Python 3.10 and double-click on the IDLE app. IDLE’s interactive window contains a Python shell. You can see this name up here, IDLE Shell.
00:55 It’s a textual user interface used to interact with the Python language. You can type a bit of Python code into the interactive window and press Enter to immediately see the result, hence the name interactive.
01:08 When the window opens up, it’ll display this same text at the beginning. The text shows the version of Python that IDLE is running and some of the commands you can use to get help or view information about Python. This spot over here, this is called the prompt, and it’s where you’re going to be able to type code.
So right in front of this cursor prompt, go ahead and type in
1 + 1 and then hit Enter. And you can see that it output
2. Python evaluates this expression of
1 + 1 and displays
2, and then it displays yet another prompt just below that. Every time that you run code in the interactive window, a new prompt is going to appear directly below the result.
When using the interactive window, it works as a loop and includes these three different steps. Python reads the code that you’ve entered at the prompt—that’s, again, the thing with the three different little greater than (
>) symbols—and then it evaluates the code.
02:09 Python will print the result and wait for more input. This is referred to as a Read-Evaluate-Print Loop. We usually abbreviate it as REPL, R-E-P-L. Python programmers typically refer to this Python shell as the Python REPL, or just REPL for short. Okay, let’s try something a little more interesting than just adding numbers.
A rite of passage for every programmer is writing the program that prints the phrase
Hello, World on the screen. At the prompt, go ahead and type in
02:49 So I’m opening the parentheses, and you can see it’s giving me a little bit of additional instruction here, which is kind of nice. And we’ll learn all about this stuff as we go throughout the courses.
If I just type
So I’ll do it again. Great. Those parentheses also enclose everything that gets sent to the function, goes into it as an input. And the quotation marks (
"") indicate that
Hello, World is really text and not something else that we might want to print. Inside of IDLE, you can kind of see some interesting highlighting going on with different colors.
04:20 With the current setup that I have, you can see that a function is different than the output, and you can see that something that’s inside of quotation marks looks different than, say, these numbers up here. By default, functions are highlighted in purple and text is highlighted in green.
04:37 This interactive window is really useful for trying out small code examples and exploring the Python language, but it has a big limitation. You have to enter the code one line at a time. So alternatively, you can save Python code in the text file and execute all that code in order to run an entire program.
05:01 You’ll write your own Python files using IDLE’s editor window. You can open the editor window by selecting File and New File from the menu at the top of the interactive window. Let me show you that.
05:14 The interactive window is going to stay open when you open the editor window. It displays the output generated by code in the editor window. So you’ll want to arrange the two windows so that you can see them both at the same time.
Inside the editor window, type in the same code you used to print
Hello, World in the interactive window. Note that IDLE highlights code typed into the editor window just like it did in the interactive window.
You don’t have to add the prompt as it was in the interactive window. It’s not required. Before you can run the program, you need to save it. So select File and Save from the menu, and save the file as
05:56 On some systems, it’ll use a default directory of where Python’s installed, the same place that IDLE is. Make sure that you don’t save your files in that directory. Instead save them to your desktop or to a folder in your user’s home directory.
.py extension indicates the file contains Python code. In fact, saving your file with any other extension is going to remove the code highlighting. IDLE only will highlight Python code when it’s stored in a
.py file. To run the program, you can select Run
So the program is going to output to the interactive window. You’ll see
RESTART and then the directory and the name of the file that’s being run and its output. IDLE restarts the Python interpreter each time, which is the computer program that actually executes your code when you run a file.
If you wanted to open a file, you can go to File and Open. And in my case, I’m reopening the file that we just saved under my home directory,
Become a Member to join the conversation.