Skip to main content
Engineering LibreTexts

1.8: Writing a Program

Typing commands into the Python interpreter is a great way to experiment with Python's features, but it is not recommended for solving more complex problems.

When we want to write a program, we use a text editor to write the Python instructions into a file, which is called a script. By convention, Python scripts have names that end with .py.


To execute the script, you have to tell the Python interpreter the name of the file. In a Unix or Windows command window, you would type python as follows:

    csev$ cat
    print('Hello world!')
    csev$ python
    Hello world!

The "csev$" is the operating system prompt, and the "cat" is showing us that the file "" has a one-line Python program to print a string.

We call the Python interpreter and tell it to read its source code from the file "" instead of prompting us for lines of Python code interactively.

You will notice that there was no need to have quit() at the end of the Python program in the file. When Python is reading your source code from a file, it knows to stop when it reaches the end of the file.