Your First Python Program

Running Your First Python Program in Detail

Creating and running your first Python program involves a few simple steps. Below, we’ll walk through setting up a basic Python file, executing it from the command line, and understanding the output.

1. Create a New Python File

Open your text editor or IDE and create a new file named `hello_world.py`. This file will contain your first Python program.
- Save the file: Make sure to save it with a `.py` extension to indicate it is a Python file.

2. Write Your First Python Code

In the `hello_world.py` file, type the following code. This code prints a simple message to the console:
print("Hello, World!")
This `print()` function tells Python to display the text "Hello, World!" on the screen. It's a common first step in any programming language, allowing you to confirm that the environment is set up correctly.

3. Open the Terminal or Command Prompt

To run your program, you’ll need to use the command line. Open the terminal (Linux/macOS) or command prompt (Windows) and navigate to the directory where `hello_world.py` is saved.

- Navigate to your file:
- Linux/macOS:
$ cd path/to/your/directory
    

- Windows:
> cd path\to\your\directory
    

Replace `path/to/your/directory` or `path\to\your\directory` with the actual path where your `hello_world.py` file is saved.

4. Run Your Python Program

Now that you are in the correct directory, you can run your Python program. Type the following command and press Enter:
$ python hello_world.py

Output:
Hello, World!

This command instructs Python to execute the code in `hello_world.py`. If everything is set up correctly, you should see the text “Hello, World!” displayed in the terminal.

5. Verify the Output

If you see the message "Hello, World!" printed in the terminal, congratulations! You have successfully run your first Python program.

- Troubleshooting:
- Command not found: If you get an error saying `python: command not found`, ensure Python is installed and added to your PATH.
- Use `python3`: On some systems, you may need to type `python3` instead of `python`:
$ python3 hello_world.py
    

6. Understanding the Code

The code in `hello_world.py` is simple, but it contains a few key concepts:

- `print()` function: This built-in function sends output to the console.
- Text in quotes: The words inside quotes (`"Hello, World!"`) are known as a string, which is a sequence of characters.
- Syntax: Python’s syntax is designed to be simple and readable. Each statement is on a new line, and no semicolons are needed to end statements.

7. Additional Tips

- Comments: Add comments to your code using the `#` symbol for explanations.
# This line prints a greeting to the console
print("Hello, World!")
- Running Code in an IDE: Most IDEs, like VS Code, also allow you to run the code directly by pressing a "Run" button, simplifying the process.

Summary

By following these steps, you've created and run your first Python program! This process provides the foundation for executing more complex code as you progress in Python.

Previous: Python Development Environment | Next: Python Basic Concepts

<
>