Learning Objectives
- Organize your code by setting up a dedicated project folder.
- Create your very first Python file in VS Code.
- Understand why the
.pyfile extension is essential for your code to work.
Setting Up Your Workspace
Before we write a single line of code, we need a clean workspace. Think of this like clearing off your physical desk before starting a big project. If you just scatter loose files everywhere on your computer, you will quickly lose track of them.
Let's create a dedicated home for your Python journey:
- Create a new folder somewhere easy to find on your computer (like your Desktop or Documents folder) and name it
PythonCourse. - Open Visual Studio Code.
- Go to the top menu and click File > Open Folder... (or Open... on Mac).
- Select the
PythonCoursefolder you just created.
Your VS Code sidebar (on the left) will now show the name of your folder. Right now, it is empty. Let's fix that!
Creating Your First Python File
Now that our desk is organized, let's pull out a fresh sheet of paper.
- Hover your mouse over your folder name in the VS Code sidebar.
- Click the New File icon (it looks like a tiny piece of paper with a plus sign).
- Name the file exactly this:
hello.pyand press Enter.
The Magic of .py
That .py at the end of the file name is incredibly important. It is called a file extension.
Just like .jpg tells your computer a file is an image, and .docx tells it a file is a Word document, .py tells VS Code, "Hey, this file contains Python instructions!"
The moment you press Enter, you should see the Python logo appear next to your file name, and your Python Extension will wake up, ready to help you write your first program.
Writing Your First Command
Now that you have your hello.py file open, you are staring at a blank screen. This is where you write your instructions for the Python translator.
Type the following line of code exactly as you see it. Pay close attention to the punctuation every single character matters!
print("Hello, World!")
Breaking Down the Code
Let's dissect what you just wrote. Even though it is only one short line, there are three distinct pieces of anatomy at work here:
print: This is a built-in Python function. Think of a function like an action verb or a specialized tool. Theprintfunction has one specific job: it takes whatever you give it and displays (or "prints") it out on the screen for you to see.()(Parentheses): Parentheses act like the delivery box for the function. You put the information you want the function to work on inside these parentheses."Hello, World!"(Quotation Marks): The quotation marks tell Python, "Treat everything inside here as standard, human-readable text." In programming, we call a sequence of text characters a String. If you left the quotation marks out, Python would get very confused and try to interpret the words "Hello" and "World" as computer commands instead of just plain text.
Congratulations! You have just written your first official line of Python code.
Making It Happen
You have written the instructions, but right now, they are just sitting on the page. We need to hand this file over to the Python translator and tell it, "Execute this!"
Because we are using VS Code and the official Python extension, doing this is incredibly simple.
- Make sure you have saved your file. (Press
Ctrl + Son Windows orCmd + Son Mac. A white dot next to your file name at the top means it has unsaved changes). - Look at the top right corner of your VS Code window.
- You should see a small triangle that looks like a "Play" button (▷).
- Click it!
The Integrated Terminal
When you click that button, a new panel will automatically pop open at the bottom of your screen. This is the Integrated Terminal. It is exactly like the command prompt you used when you first installed Python, but conveniently built directly into your workspace.
You will see a line of computer jargon (the file path showing where Python lives on your machine and where your script is located), but look closely right below that. You should see:
Hello, World!
If you see that text, your program worked perfectly! The Python translator read your file from top to bottom, saw the print function, grabbed the text inside the quotation marks, and displayed it on the screen.
Under the Hood
Clicking the Play button is incredibly convenient, but as a future developer, it is important to understand what that button is actually doing behind the scenes.
The Play button is just a handy shortcut. It automatically opens the terminal and types the command to run your script for you. Let's learn how to do it manually, which is how most professional developers run their code.
Doing It Manually
- If your terminal panel at the bottom is closed, you can open it by going to the top menu and clicking Terminal > New Terminal.
- Look at the text prompt in the terminal. It should show the path to your current location and end with your folder name (e.g.,
.../PythonCourse>). - Type the following command and press Enter:
python hello.py
(Note: Mac users might need to type python3 hello.py)
You should see Hello, World! printed out once again!
What just happened?
You directly told your computer two distinct things:
1. "Grab the python translator."
2. "Use it to read and execute the specific file named hello.py."
Whether you use the Play button or type the command manually in the terminal, the result is exactly the same. Knowing how to use the terminal manually gives you much more power and flexibility as your projects get larger!
Lesson Summary
You are officially a programmer! Let's review what you accomplished in this lesson:
- Workspace Organization: You created a dedicated folder to keep your code organized.
- The
.pyExtension: You learned that naming a file with.pytells VS Code to activate the Python translator. - The
print()Function: You wrote your first line of code using a built-in function to display text on the screen. - Execution: You learned how to run your program using both the convenient "Play" button and the manual terminal command (
python hello.py).
You have successfully connected your digital notebook to your translator. From here on out, it is just a matter of learning new words and grammar to tell the computer to do increasingly complex things!