Lesson 3: Running Your First Python Program
Learn how to create, save, and execute your first Python script using VS Code.
Lesson 3: Running Your First Python Program
It’s time for the "Hello, World!" moment! In this lesson, we will move past the theory and actually run our first independent Python script. By the end of this page, you’ll know how to create a file, write code in it, and see that code come to life in the terminal.
Lesson Overview
In this lesson, we will:
- Create a Python File: Learning the
.pyextension. - Write Your Code: Using VS Code to write instructions.
- Run via Terminal: Executing your script like a pro.
- Fixing Mistakes: Understanding what happens when things go wrong.
Step 1: Create a Project Folder
Before we write code, we need a place to store it.
- Create a folder on your computer named
PythonBasics. - Open VS Code.
- Go to File > Open Folder... and select your
PythonBasicsfolder. - You should now see the folder name in the sidebar on the left.
Step 2: Create Your First File
All Python files must end with the extension .py. This tells the computer: "Hey, I'm a Python script!"
- Click the New File icon next to your folder name in the VS Code sidebar.
- Name the file
hello.py. - The file will open in the main editor window.
Step 3: Write Your Code
Type the following code into hello.py:
# This is my first Python program!
print("Success! I am now a Python programmer.")
What do those lines mean?
#: This is a Comment. Anything after a#is ignored by Python. It’s a note for you (the human).print(): The command that shows text on the screen."Success!...": The text we want to display.
Don't forget to save! (Press Ctrl+S on Windows or Cmd+S on Mac).
Step 4: Run Your Program
There are two ways to run Python in VS Code:
The "Easy Way" (Play Button):
Look at the top-right corner of your editor. Click the Play Icon (a small triangle).
The "Pro Way" (The Terminal):
- Go to Terminal > New Terminal.
- In the terminal window at the bottom, type:
(Orpython hello.pypython3 hello.pyon macOS).
Output:
Success! I am now a Python programmer.
If you see that text, congratulations! You just successfully executed a Python program.
Step 5: What If it Breaks? (Debugging)
Let's try to break it on purpose. Remove the closing ) at the end of your print statement and try to run it.
# Broken Code
print("Hello"
Result: You will see a SyntaxError. This is Python’s way of saying: "I don't understand your grammar!"
How to fix it:
- Read the error message—it usually points to the line number.
- Add the
)back. - Save and run again.
Practice Exercise
- Create a new file named
about_me.py. - Write three separate
printstatements in it:- One for your name.
- One for your city.
- One for the year you started learning Python.
- Run the file and ensure all three lines appear in the terminal.
Quick Knowledge Check
- What file extension do all Python files use?
- What does the
#symbol represent in Python? - Where do you see the output of your program in VS Code?
- What is a
SyntaxError?
Key Takeaways
- Python files end in
.py. - Comments are used to explain code and are ignored by the computer.
- Saving your file is required before you can run the latest changes.
- Errors are normal—they are just clues to help you fix your logic.
What’s Next?
Now that you can run programs, let's learn how to make them "smart." In Lesson 4, we’ll explore Variables and Data Types—the way programs store and remember information!