Concepts / Choosing and Setting Up a Python Editor

Choosing and Setting Up a Python Editor

A Python source file must be saved with the .py extension (for example, hello.py) so that the operating system and Python interpreter recognize it as a Python program.

  • Programming

From Editor to Saved Program

Writing code in an editor is only the beginning. If you want to keep the program, reopen it, edit it, run it later, or share it with someone else, you must save it as a file on your computer. The essential choices are the editor, the filename, and the folder where the file will live.

A Python source file is a permanent record of a program. The editor displays and lets you change the code, while the saved file stores that code on your computer.

The .py Filename Extension

A Python source filename has two essential parts: a name you choose and the .py extension that you must include. In hello.py, hello is the chosen name and .py is the required extension. The extension tells the operating system and text editors that the file contains Python code, and it allows the Python interpreter to recognize the file as a Python program.

followed byfollowed byhellochosen name.separatorpyPython extension
What changes when a filename ends in .py instead of another extension?
Filename partPurpose
helloA name chosen by the programmer
.Separates the name from the extension
pyIdentifies the file as Python source code

The three visible parts of the filename hello.py

Saving Code into a File

write codeuse .pyomit or change extensionPlain-text editorcode is writtenSavechoose name and folderPython source filename.pyWrong filename formatnot recognized as intended
How does code move from the editor into a saved .py file?

The saving workflow is straightforward. Open a plain-text editor, write your Python code, choose a meaningful filename, select a location, and save the file with the .py extension. When you click Save, the code in the editor is written to computer storage as the file you specified.

Saving a Greeting Program

You have written a small greeting program in a plain-text editor and want to keep it for later use.

Choose a name: Select a meaningful name such as greet_user.py. The name describes the program, and the .py extension identifies it as Python source code.

Choose a folder: Select the dedicated folder where you keep your Python programs.

Save the file: Use the editor's Save action. The code displayed in the editor is written to storage as the file named greet_user.py.

Verify the result: Check that the file exists in the intended folder and that its filename ends in .py.

The program is now stored as a Python source file that can be opened, edited, and run later.

File Contents and Editor Display

showssaved aslocated inText editordisplays codegreet_user.pystores codePython codetext written by learnerComputer storagechosen folder
What does a .py file contain, and how is the code shown in the editor related to the stored file?

Before saving, the code exists in the editor while you are working on it. Saving writes that code to computer storage as a file. The file is independent of the editor: after saving, you can close the editor or turn off the computer, and the file remains in the selected folder exactly as it was saved.

The editor is a tool for writing and viewing code. The .py file is the stored program. Saving creates the lasting connection between the code you wrote and the file you can reopen later.

Organizing Python Programs

A Python file can be saved in any folder as long as you know where that folder is. However, a dedicated folder makes your work easier to organize as you create more programs. Create one folder specifically for Python programs, such as Python_Programs or My_Python_Code, on your Desktop or in your Documents folder. Save your Python files there so they remain together and are easier to find and run later.

  • Create one folder dedicated to your Python programs.
  • Choose a location you can find again, such as the Desktop or Documents folder.
  • Use meaningful filenames for individual programs.
  • Use underscores instead of spaces in filenames.
  • Avoid special characters in filenames.
  • After saving, verify that the file is in the intended folder and that the name ends in .py.

Filename and Saving Mistakes

  • Leaving out the .py extension

    The operating system and Python interpreter rely on the extension to recognize the file as a Python source file.

    Fix: Include .py at the end of the filename.

  • Using a word processor instead of a plain-text editor

    The added formatting cannot be interpreted as Python code.

    Fix: Use a plain-text editor for writing and saving Python programs.

  • Using spaces or special characters in the filename

    The recommended filename format uses underscores instead of spaces and avoids special characters.

    Fix: Choose a meaningful name that uses underscores where needed and ends in .py.

  • Saving programs in unrelated locations

    The files become harder to find and manage as the number of programs grows.

    Fix: Create one dedicated folder and save your Python programs there.

  • Assuming code remains after closing an interactive shell or notebook

    Code entered interactively runs immediately but disappears when the program is closed.

    Fix: Save code as a .py file when you need to keep, reuse, or share it.

File Setup Practice

EASY

Plan how you would save a Python program that calculates a total. State the folder you would create or use, choose a meaningful filename, and explain why the filename must end in .py.

Hints
  • Use a dedicated folder for Python programs.
  • Use underscores instead of spaces in the filename.
  • The extension is .py because it identifies the file as Python source code.

What do you think happens?

You write code in an interactive shell and close the program without saving it. What happens to that code?

  • It remains automatically as a Python source file
  • It disappears when the program closes
  • It is placed automatically in a dedicated folder
Reveal answer

Answer: It disappears when the program closes.

Code entered in an interactive shell or notebook runs immediately, but saving it as a .py file is necessary when you want to keep, reuse, or share it.

Essential Saving Rules

  1. Use a plain-text editor rather than a word processor.
  2. Save every Python program with the .py extension.
  3. Choose a meaningful filename, use underscores instead of spaces, and avoid special characters.
  4. Keep your programs in one dedicated folder so they are easy to find.
  5. Remember that saving writes the code from the editor to a lasting file on computer storage.
  6. Verify both the filename and the folder after saving.

Key Takeaways

  • A Python source file needs a name and the .py extension.
  • The .py extension helps the operating system and Python interpreter recognize the file as Python code.
  • A plain-text editor is appropriate for Python; word processors add formatting that Python cannot interpret.
  • A dedicated folder keeps Python programs organized and easy to find.
  • Saving writes the code displayed in the editor to a persistent file that can be reopened, edited, and run later.