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
- Click on "File" in the top menu.
- Select "New File" to create a new, empty file.
- Save the file with a
.py
extension, such ashello.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:
-
Save the file to ensure your changes are saved.
-
Open the terminal or command prompt on your computer.
-
Navigate to the directory where you saved your
hello.py
file. You can use thecd
command to change directories. -
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.