Writing and Running Your First Python Program

Writing and Running Your First Python Program

Now that you have Python and a code editor set up, let's dive into writing your first Python program. We'll start with a classic "Hello, World!" program, a simple script that prints a message to the screen.

1. Open Your Code Editor

Open your code editor, which in this tutorial is Visual Studio Code. Ensure you have the Python extension installed.

2. Create a New Python File

  1. Click on "File" in the top menu.
  2. Select "New File" to create a new, empty file.
  3. Save the file with a .py extension, such as hello.py. The .py extension indicates that this is a Python script.

3. Write Your Python Code

In the new file, type the following code:

print("Hello, World!")

This simple code consists of a single line that uses the print function to display the text "Hello, World!" on the screen.

4. Run Your Python Program

Now that you've written your Python code, it's time to run it:

  1. Save the file to ensure your changes are saved.

  2. Open the terminal or command prompt on your computer.

  3. Navigate to the directory where you saved your hello.py file. You can use the cd command to change directories.

  4. To run the Python program, use the following command:

    python hello.py
    

    Replace hello.py with the name of your Python script if you used a different filename.

5. View the Output

After running the program, you'll see the output in the terminal, which should be:

Hello, World!

Congratulations! You've just written and executed your first Python program. You've officially begun your Python programming journey.

A Few Notes

  • Python is case-sensitive, so make sure you type the code exactly as shown.
  • The print function is used to display output. You can replace the text inside the quotes with anything you'd like.