Write a Python Script
For more information on concepts covered in this lesson, you can check out:
00:00 Okay. It’s time to get started writing your Python script.
00:05 You’re going to be using a program that comes with your installation of Python. It’s called IDLE, which stands for Integrated Development and Learning Environment.
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.
01:27
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.
01:55
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.
02:35
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 print
, and then you can create a set of parentheses (()
) here.
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.
02:59
In this case, we’re going to print some text. So we want "Hello, World"
. And if I hit Return,
03:11
you can see that it outputs Hello, World
. This bit of code that you typed in here, print()
, is a function. A function is code that performs some task and can be invoked by its name.
03:23
The above code invokes, or calls, the print()
function with the text Hello, World
inside of it and outputs it. The parentheses tell Python to call the print()
function.
03:35
If I just type print
by itself, it won’t know that I want to call it. It’ll just tell me about it. It tells me that it’s a built-in function named print
. In order to call that function, I need to put the parentheses and then tell it something, a value, as you see it’s prompting me here to put in there. And last time we typed the same thing.
03:57
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.
04:57 That’s where we’re going to switch into using the editor window.
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.
05:27
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.
05:38
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 hello_world.py
.
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.
06:11
The .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
06:31 from the pull-down menu and Run Module. You also might notice some key commands being highlighted here. In this case, it’s the function key F5.
06:44
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.
07:03 This makes sure that programs are executed the same way each time.
07:07
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, python_basics/
, your_first_program/
, and hello_world.py
.
07:25 You can also open a file from the file manager, such as Windows Explorer or the macOS Finder, by right-clicking on them and say Open with.
07:35 All right, you’ve made your first program. Next up, it’s time to make some mistakes.
Bartosz Zaczyński RP Team on Sept. 29, 2023
For others who might be reading this, I think that @Sanjay meant PythonAnywhere. There are a bunch of other options available, including GitHub Codespaces.
Martin Breuss RP Team on Sept. 29, 2023
Yes, there are a lot of good alternatives to using IDLE. Also check out our guide to IDEs and code editors. The associated video course also covers an online coding environment called repl.it
However, the advantage of IDLE—and why we chose to use it in the Python Basics courses—is that it comes included with Python.
That means that after installing Python, you’re ready to go :)
OneDay on March 10, 2024
Hello, I downloaded IDLE the latest version is 12 will learning this 10 version make a difference? updates are needed maybe..
Martin Breuss RP Team on March 10, 2024
@Cardinal T there haven’t been any major changes in IDLE for a long time, and from quickly looking at the changelog, it doesn’t seem like anything big has shifted since the IDLE version that was shipped with Python 3.10 either.
IDLE is just one of many ways to run a Python interpreter and write Python scripts. You can check out other editors and work with those if you prefer, but the team decided to stick with Python’s IDLE throughout the Python Basics course because it ships with all Python installations so it reduces the hassle of installing another program. Also, it hasn’t changed significantly for a long time (and likely won’t into the future) so it’ll give you a consistent experience across versions.
Hope that’s helpful. If you still end up discovering any confusing differences, then please let us know!
derekd64 on July 4, 2024
thanks all, yes I realized my problem as to why I was not getting my prompts....I never properly saved any previous python files or python installs correctly…my bad , but here is to learning…thanks I will square myself to first cleaning up previous messes I made in the past months before using your program a long while ago as I first tried to learn....appreciate....
derekd64 on July 4, 2024
interestingly as a newbie, I attempted to clean up old versions of python, and I installed the latest 3.12…so my quandary is that when I attempt to open a new file, I get different behavior, it opens up on right top corner of desktop with an untitled white space and no prompts.....so I guess I am not still getting something…I will keep you posted, as I try to trouble shoot my dilemma....
Martin Breuss RP Team on July 5, 2024
@derekd64 what program is your system opening the file with? It sounds like maybe that’s a text editor and not IDLE?
derekd64 on July 8, 2024
my apologies just seeing this now, i think I got it down now, it appears the latest version you can just scroll to the menus of run and in the list box I see you can pick python shell.....so that works for me now..... Although I do not see the capability to see the new file attach in the same window its separate , but I do not want to look at the new process with horse blinders either, its not a need for me in light of the bigger picture of understanding the pythonamin dynamics :-)
Martin Breuss RP Team on July 9, 2024
I’m glad you got it to work! Yeah, some of this setup and configuration details can be annoying when they’re not quite as expected, but I think you’re right in the approach of not letting it distract you too much from the bigger Python picture :)
Become a Member to join the conversation.
Sanjay Raisoni on Sept. 29, 2023
I found using pythoneverywhere and google colab easier than using IDLE.