Your first Python script

Up to this point, we have focused on how Python runs and how programs are executed from the command line. This lesson exists to close the loop by having us create a Python program ourselves, even if it is very small. Writing and running a first script turns Python from something we observe into something we actively use, which is essential for building real programs later on.

Creating a Python source file

A Python program starts as a plain text file. Any text editor will do, as long as it saves raw text rather than adding formatting.

We create a new file and give it a name that ends with the .py extension. This extension signals that the file contains Python source code and should be run by the Python interpreter.

At this stage, the file can be completely empty. What matters is that it exists and is saved in a known location.

Writing a minimal Python program

A minimal Python program can be just a single line of code. There is no required boilerplate and no special structure to begin with.

For example, we can write a program that produces a small piece of output:

print("Hello, Python")

This is a complete Python program. When the interpreter runs this file, it will execute the line exactly as written.

Producing output with a built-in function

The print function is a built-in part of Python. It writes text to standard output, which usually means the terminal window.

Using print is often the simplest way to observe what a program is doing. It gives us immediate feedback that the code has executed and produced a result.

In this lesson, print serves one purpose: to make program execution visible.

Saving changes to the source file

After writing or modifying code, the file must be saved. The Python interpreter only sees what is currently stored on disk, not what is unsaved in an editor.

Saving the file updates the source code that will be executed the next time the program is run.

Running the script and observing its output

Once the file is saved, we run it using the Python interpreter from the command line.

python.exe first_script.py

When the command runs, Python reads the file from top to bottom and executes each line in order. Any output produced by print appears in the terminal.

Seeing the expected output confirms that the script was written correctly, saved properly, and executed by the interpreter.

Conclusion

We have now written a Python script, saved it as a source file, and executed it to produce visible output. This is the basic cycle we will repeat throughout the course: write code, save it, run it, and observe what happens. With this foundation in place, we are oriented and ready to build programs that do more interesting work.