Skip to content

01-05: Writing and Executing Python Code in VS Code


Part 1: Creating / Opening a Python File

🔹 Case 1: Create a New File

Step 1: Create file in VS Code

File → New File

👉 Select file type: Python (if prompted)

Step 2: Write the code

print("Hello, Python World!")

Step 3: Save the file

File → Save As

✔ Save with .py extension

Example: 01-first-code.py


🔹 Case 2: Open an Existing File

Way 1:

Right Click file → Open with VS Code

Way 2:

In VS Code → File → Open File → Select the .py file from its folder


Part 2: Running a Python File

🔹 Method 1: Using Terminal

Step 1: Open terminal

Terminal → New Terminal

Step 2: Go to file directory

cd <file_directory>

Example:

cd C:\Users\jahid\github-projects\python-fundamentals\02-exercises

Step 3: Run the file

python filename.py

OR (recommended on Windows):

py filename.py

Example:

py 01-first-code.py

👉 Output:

Hello, Python World!

run_python_code

🔹 Method 2: Using Run Button

Way 1:

Click the ▶️ Run button (top right)

Way 2:

Right Click on the code file → Run Python → Run Python File in Terminal


⚠️ Important Notes

In VS Code Terminal, Run from correct directory:

cd <folder_where_file_exists>

⬅️ Previous: 01-04: Environment Setup ➡️ Next: 02-01: Basic Syntax