Create a new file
With yourpython-for-ai project open in VS Code:
- Click the “New File” button in the Explorer (looks like a page with a +)
- Name it
hello.py - Press Enter
Make sure to include the
.py extension! This tells VS Code and Python that this is a Python file.Understanding file extensions
File extensions tell your computer and VS Code what type of file you’re working with. There are many files type, here are some examples:.txt- Plain text file, no special formatting.md- Markdown file, for documentation.py- Python file, contains Python code
.py, VS Code:
- Adds syntax highlighting (colors your code)
- Enables Python-specific features
- Shows Python in the bottom-right corner
Write your first program
In yourhello.py file, type:
The
print() function displays text or values on your screen - we’ll use it throughout this course to see what our code is doing.printis highlighted as a function- Text in quotes is shown in a different color
- This helps you spot errors quickly
Select Python interpreter
Before running your code, make sure VS Code is using the right Python:- Look at the bottom-right corner of VS Code
- You should see something like “Python 3.x.x”
- If not, click the area that says “Select Python Interpreter”
- Choose the Python version you installed earlier
We’re using the main Python installation for now. Later in the course, you’ll learn about virtual environments for more advanced project management.
Run your program
There are three ways to run your Python code: Method 1: Run button (easiest)- Click the triangle “Run” button in the top-right corner
- Or select the down arrow next to it and select “Run Python File”
- Right-click anywhere in your code
- Select “Run Python File in Terminal”
- Press
Ctrl + F5(Windows/Linux) orCmd + F5(macOS)
Understanding the output
When you run your code, the terminal opens at the bottom. You’ll see something like this (yours will look slightly different):- First line: VS Code navigates to your project folder
- Second line: VS Code runs Python with the full path to your file
- Following lines: Your program’s output!
What’s happening behind the scenes?
When you click the “Run” button in VS Code, it’s actually running a command in the terminal for you. Let’s understand what’s really happening:The basic command
At its core, running a Python file is simple:- python - Find the Python program
- hello.py - Open this file and execute the code inside
How Python executes your code
Here’s what happens when you runpython hello.py:
- Python reads your file - Opens
hello.pyand reads the text - Python interprets the code - Converts your code into instructions the computer understands
- Python executes line by line - Runs each instruction from top to bottom, left to right
- Output appears in terminal - Any
print()statements display their results
Python always executes code top to bottom (one line after another) and left to right (within each line). This means the order of your code matters!
- Line 2 can’t run before Line 1
- What you write first happens first
- Later in the course, you’ll see how to control this flow with conditions and loops
Running manually from the terminal
You can run Python files directly in the terminal without using VS Code’s button:VS Code’s “Run” button is just a convenient way to run
python your_file.py in the terminal. The underlying process is exactly the same!Python vs python3
On some systems (especially Mac/Linux), you might need to usepython3 instead of python:
Why this matters
Understanding this is important because:- You can run Python files from any terminal, not just VS Code
- You’ll see these commands in tutorials and documentation
- Later, you’ll pass arguments to your programs:
python script.py --option value - This is how Python works everywhere: on servers, in production, in automation scripts
Think of VS Code as a fancy wrapper around the terminal. When you click “Run,” you’re really just typing
python hello.py into the terminal. VS Code makes it easier, but the fundamentals stay the same.The integrated terminal
The terminal at the bottom is the same as your system’s terminal, but inside VS Code. Here’s how to work with it:Opening and closing the terminal
- Open: View > Terminal (or press
Ctrl + `on Windows/Linux,Cmd + `on macOS) - Hide: Click the X or press the same shortcut again
- New terminal: Click the + button or Terminal > New Terminal
Terminal basics
- You can type commands directly
- Try typing
python --versionand press Enter - Type
cls(Windows) orclear(Mac/Linux) to clear the screen - Use the up arrow to recall previous commands
The integrated terminal means you never have to leave VS Code. You can write code, run it, install packages, and manage files all in one place.
Experiment
Try changing your code and running it again:print() statement creates a new line of output.
Save your work
Remember to save your file:- Press
Ctrl + S(Windows/Linux) orCmd + S(macOS) - Or use File > Save
What you’ve learned
Congratulations! You’ve:- Created your first Python file
- Written Python code
- Selected the Python interpreter
- Run a Python program
- Understood the terminal output
Course resources
Download all templates, files, and cheat sheets