First Python file
Write and run your first Python program
Create a new file
With your python-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
When you save a file with .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 your hello.py file, type:
print("Hello, World!")
print("I'm learning Python for AI")The print() function displays text or values on your screen — we’ll use it throughout this course to see what our code is doing.
Notice how VS Code colors your code:
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”
Method 2: Right-click menu
- Right-click anywhere in your code
- Select “Run Python File in Terminal”
Method 3: Keyboard shortcut
- 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):
cd /path/to/your/project
/path/to/python /path/to/your/project/hello.py
Hello, World!
I'm learning Python for AIWhat’s happening here:
- 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!
Don’t worry about the long paths - VS Code handles all of this automatically. The important part is seeing your output: “Hello, World!”
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 hello.pyThis tells your computer:
- 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 run python 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!
print("Hello, World!") # Line 1: Python reads this
# Python converts it to machine instructions
# Python executes: display "Hello, World!"
# You see: Hello, World!
print("I'm learning Python") # Line 2: Same processThis execution order means:
- 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:
# Make sure you're in the same folder as your file
python hello.pyOr with the full path:
# You can run from anywhere if you use the full path
python /Users/yourname/python-for-ai/hello.pyVS 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 use python3 instead of python:
python3 hello.pyThis ensures you’re using Python 3.x instead of the older Python 2.x. VS Code usually handles this automatically.
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
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
Experiment
Try changing your code and running it again:
print("Hello, World!")
print("I'm learning Python for AI")
print("My name is [Your Name]")
print("Today is a great day to code!")Each 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
VS Code shows a dot next to unsaved files in the tab.
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
This is the foundation for everything else you’ll learn in Python.