> ## Documentation Index
> Fetch the complete documentation index at: https://fastapi2day.codewithsiva.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Writing and Running Your First Python Script in VS Code

> Create your first .py file in VS Code, write a print statement, select a Python interpreter, and run your program from the terminal.

With your workspace open in VS Code, you are ready to write your first Python program. This page walks you through creating a `.py` file, understanding what happens when Python runs it, and using VS Code's integrated terminal. These are the foundational habits you will use every single day as a Python developer.

## Create a new file

<Steps>
  <Step title="Add a file to your project">
    In the Explorer panel on the left, click the **New File** icon (a page with a `+` symbol) and name the file `hello.py`. Press `Enter` to confirm.
  </Step>

  <Step title="Confirm the .py extension">
    Make sure the filename ends in `.py`. This extension tells VS Code and Python that the file contains Python code, which activates syntax highlighting and all the Python extension features.
  </Step>
</Steps>

<Warning>
  Always include the `.py` extension when creating Python files. Without it, VS Code treats the file as plain text and none of the Python-specific features will work.
</Warning>

## Write your first program

Click inside the editor and type the following:

```python theme={null}
print("Hello, World!")
print("I'm learning Python for AI")
```

Notice how VS Code immediately colours your code — `print` is highlighted as a built-in function, and the text inside the quotes appears in a distinct colour. This is syntax highlighting, and it helps you spot typos and errors at a glance.

<Note>
  The `print()` function is one of the most frequently used tools in Python. It displays any value or text you pass to it in the terminal. You will use it throughout this course to inspect what your code is doing.
</Note>

## Select a Python interpreter

Before running the file, confirm VS Code knows which Python to use.

<Steps>
  <Step title="Open the Command Palette">
    Press `Ctrl + Shift + P` (Windows/Linux) or `Cmd + Shift + P` (macOS).
  </Step>

  <Step title="Select an interpreter">
    Type **Python: Select Interpreter** and press `Enter`. Choose the Python version you installed. It will appear in the status bar at the bottom of VS Code once selected.
  </Step>
</Steps>

<Note>
  For now you are selecting your system-wide Python installation. Later in the course you will learn to use virtual environments, where each project gets its own isolated Python interpreter.
</Note>

## Run your program

There are three equivalent ways to run your file:

**Method 1 — Run button:**
Click the ▶ triangle in the top-right corner of the editor, or click the dropdown arrow and choose **Run Python File**.

**Method 2 — Right-click menu:**
Right-click anywhere in your code and select **Run Python File in Terminal**.

**Method 3 — Keyboard shortcut:**
Press `Ctrl + F5` (Windows/Linux) or `Cmd + F5` (macOS).

The terminal panel opens at the bottom and shows your output:

```text theme={null}
Hello, World!
I'm learning Python for AI
```

## What is happening behind the scenes?

When you click **Run**, VS Code executes this command in the terminal on your behalf:

```bash theme={null}
python hello.py
```

That single command tells your computer two things:

1. **`python`** — launch the Python interpreter
2. **`hello.py`** — read this file and execute the code inside it

You can run this command yourself directly in the terminal and get identical results. VS Code's Run button is simply a shortcut around this fundamental operation.

### How Python executes your code

Understanding Python's execution model will save you many hours of debugging confusion later:

1. **Syntax check** — Python reads the entire file first and checks for syntax errors. If it finds any, it stops immediately and prints an error message. No code runs at all.
2. **Interpretation** — If the file passes the syntax check, Python converts your code into bytecode instructions.
3. **Line-by-line execution** — Python runs those instructions from top to bottom, left to right.

```python theme={null}
# Python checks the whole file first, then executes top to bottom:

print("Hello, World!")        # Runs first
print("I'm learning Python")  # Runs second
```

<Note>
  Python always executes code **top to bottom**. The order of your statements matters — what you write first happens first.
</Note>

### What a syntax error looks like

Consider this broken file:

```python theme={null}
print("This line is correct")
print("This line is also correct")
print("This line has a syntax error   # missing closing quote and parenthesis
```

Python's syntax check catches the error on line 3 before any code runs. You will see a `SyntaxError` in the terminal, and **none** of the three lines will produce output — not even the correct ones. This surprises many beginners, but it is intentional: Python will not run a file it cannot fully parse.

### python vs python3

On macOS and Linux, you may need to use `python3` instead of `python` to ensure you are running Python 3:

```bash theme={null}
python3 hello.py
```

VS Code's interpreter selector handles this automatically once you have chosen your interpreter.

## Using the integrated terminal

The terminal panel at the bottom of VS Code is a fully functional shell — identical to opening a separate Terminal or Command Prompt window. You can type commands directly in it.

```bash theme={null}
# Check which Python version is active
python --version

# Run any Python file manually
python hello.py

# Clear the terminal screen
clear        # macOS / Linux
cls          # Windows
```

<Tip>
  Press `` Ctrl + ` `` (Windows/Linux) or `` Cmd + ` `` (macOS) to toggle the terminal panel open and closed without lifting your hands from the keyboard.
</Tip>

## Experiment

Try modifying your file and running it again. Add more `print()` lines:

```python theme={null}
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()` call produces one line of output. This is the feedback loop you will use constantly as you build more complex programs.

## Save your work

Press `Ctrl + S` (Windows/Linux) or `Cmd + S` (macOS) to save. A dot next to the filename in the tab indicates unsaved changes — save frequently so you never lose your work.

<Card title="Course resources" icon="download" href="/getting-started/course-resources">
  Download all practice notebooks, exercise files, and cheat sheets.
</Card>
