Your Preferences

  • OS:
  • Level:

Change Preferences


Tutorial Navigation

Viewing a single segment.


Related Content

Create and Run Your First Python Program

Description: Learn how to write, save, and execute a simple Python program ('Hello, world!') using a text editor and the command prompt.

Skill Level: Beginner

Author: AI FullTutorialGen (beginner) - Topic: Guide users through creating a very simple Python program (e.g., printing 'Hello, world!') using a text editor like Notepad. Show them how to save the file with a '.py' extension and run it from the command prompt us

Published: 01 Jun 2025

Last Updated: 01 Jun 2025

0
1
0
0
Tags: python beginner command prompt first program hello world script execution

Creating Your First Python Program

This segment will guide you through creating and running your very first Python program. We'll use a simple "Hello, world!" example to get you started.

Writing the Code

  1. Open a basic text editor like Notepad (Windows) or TextEdit (macOS). Avoid using word processors like Microsoft Word, as they add formatting that Python can't understand.

  2. Type the following line of code into the text editor:

    print("Hello, world!")

    This single line of code tells Python to display the text "Hello, world!" on your screen. print() is a built-in Python function that outputs text.

Saving the File

  1. Go to File > Save As... in your text editor.
  2. Choose a location on your computer where you want to save the file.
  3. In the "File name" field, type a name for your file, such as hello.py. Crucially, make sure to add the .py extension to the end of the filename. This tells your computer that the file contains Python code.
  4. In the "Save as type" dropdown (if available), select "All Files (*.*)". This ensures that the .py extension is saved correctly.
  5. Click Save.

Running the Program from the Command Prompt

Now that you've saved your Python file, you can run it from the command prompt (Windows) or terminal (macOS/Linux).

  1. Open the Command Prompt:

    • Windows: Press the Windows key, type cmd, and press Enter.
    • macOS: Open the "Terminal" application (found in /Applications/Utilities/).
    • Linux: Open your distribution's terminal application.
  2. Navigate to the Directory: Use the cd command to navigate to the directory where you saved hello.py. For example, if you saved it in your "Documents" folder, you might type:

    cd Documents

    If you saved it directly in your user directory, you might use:

    cd ~

    (The ~ symbol represents your home directory.)

  3. Run the Program: Type one of the following commands and press Enter:

    python hello.py

    or

    py hello.py

    The python or py command tells your computer to execute the Python interpreter on the hello.py file.

  4. See the Output: If everything is set up correctly, you should see the following output in the command prompt:

    Hello, world!

Congratulations! You've successfully created and run your first Python program.

Troubleshooting

  • "python" or "py" is not recognized as an internal or external command: This usually means that Python is not added to your system's PATH environment variable. Refer to the previous segment in this tutorial for instructions on how to add Python to your PATH. https://docs.python.org/3/using/windows.html#environment-variables
  • SyntaxError: Double-check your code for typos or incorrect syntax. Make sure you've typed print("Hello, world!") exactly as shown.
  • FileNotFoundError: Ensure you are in the correct directory in the command prompt and that the filename hello.py is spelled correctly.

Expected Outcome for this step:

User will be able to create a basic Python script, save it with the '.py' extension, and run it successfully from the command line.


Segment Comments

Comments for "Create and Run Your First Python Program" will be displayed here.

Overall comments for single segments are displayed with the segment content above. This section can be used for a separate comment form if desired.

(Form to add overall tutorial comments coming soon)