Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Write a Python Script

Give Feedback

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.

Become a Member to join the conversation.