Python Hello World Program in Visual Studio Code

If you are a beginner in Python, you should start with a Hello World program as the starting point. Visual Studio Code (VS Code) is a powerful, open-source code editor that Python developers widely use. In this tutorial, I will explain how to write a Hello World program in Python using the Visual Studio Code editor.

Visual Studio Code (VS Code) is a free, open-source code editor developed by Microsoft. It is available for Windows, macOS, and Linux. VS Code supports a wide range of programming languages and comes with built-in support for JavaScript, TypeScript, and Node.js. It also has a rich ecosystem of extensions that support Python, C++, Java, and many other languages.

Before writing the Python program, you need to set up Visual Studio Code for Python development.

Set Up Visual Studio Code for Python Development

Follow the steps below to set up Visual Studio Code for Python development.

Install Visual Studio Code

First, you need to download and install Visual Studio Code. You can download the installer from the official Visual Studio Code website.

  1. Go to the Visual Studio Code website.
  2. Click on the download button for your operating system.
  3. Follow the installation instructions.

The steps are very simple; you can just follow the UI to install VS Code.

Install Python

If you haven’t already, you need to install Python. You can download the latest version of Python from the official Python website.

  1. Go to the Python website.
  2. Click on the download button for your operating system.
  3. Follow the installation instructions.

Like the above, these steps are also simple.

Install the Python Extension for Visual Studio Code

To write and run Python code in Visual Studio Code, you need to install the Python extension.

  1. Open Visual Studio Code.
  2. Go to the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+X.
  3. In the search box, type “Python”.
  4. Click on the Python extension by Microsoft.
  5. Click the Install button.

Here is a screenshot for your reference; I installed the Python extension in VS Code.

Install the Python Extension for Visual Studio Code

Set Up a Python Environment

Using a virtual environment for your Python projects is a good practice. A virtual environment is an isolated environment that allows you to manage dependencies for your project without interfering with other projects.

  1. Open a terminal in Visual Studio Code by selecting Terminal > New Terminal from the top menu.
  2. Navigate to your project directory.
  3. Create a virtual environment by running the following command:python -m venv venv
  4. Activate the virtual environment:
    • On Windows:.\venv\Scripts\activate
    • On macOS and Linux:source venv/bin/activate

This is how to set up a virtual environment for Python development.

Write Your First Python Program

Next, let me show you how to write your first Python program using VS Code.

Create a New Python File

  1. Open Visual Studio Code.
  2. Go to File > New File or press Ctrl+N.
  3. Save the file with a .py extension, for example, hello_world.py.

Write the “Hello World” Program

In your new Python file, type the following code:

print("Hello, World!")

This simple program uses the print function to output the text “Hello, World!” to the console.

Understand the Syntax

  • print: This is a built-in function in Python that outputs text to the console.
  • "Hello, World!": This is a string, a sequence of characters enclosed in quotation marks. In this case, the string is “Hello, World!”.

Run the Python Program

Now, since our Python code is ready, let me show you how to run the Program using VS Code.

Run the Program in Visual Studio Code

To run your Python program in Visual Studio Code, follow these steps:

  1. Open the terminal in Visual Studio Code by selecting Terminal > New Terminal.
  2. Make sure your virtual environment is activated (refer to the previous section).
  3. Navigate to the directory where your Python file is located.
  4. Run the program by typing the following command in the terminal:python hello_world.py

You should see the output “Hello, World!” in the terminal.

Run the Program Using the Run Button

Visual Studio Code also provides a convenient way to run your code using the Run button.

  1. Open your Python file in Visual Studio Code.
  2. Click on the green Run button in the top-right corner of the editor.

The output will appear in the terminal.

I executed the above code in my local VS Code, and you can see the exact output in the screenshot below:

python hello world program visual studio

Debug Python Code in Visual Studio Code

As a Python developer, you should know how to debug your Python program. Let me show you this.

Set Up the Debugger

Visual Studio Code has a built-in debugger that you can use to debug your Python code.

  1. Open your Python file in Visual Studio Code.
  2. Go to the Run view by clicking on the Run icon in the Activity Bar on the side of the window or by pressing Ctrl+Shift+D.
  3. Click on the create a launch.json file link to create a launch.json configuration file.
  4. Select Python from the list of environments.

Add Breakpoints

A breakpoint is a marker that you can set on a line of code where you want the debugger to pause execution.

  1. Open your Python file in Visual Studio Code.
  2. Click in the gutter to the left of the line number where you want to set a breakpoint. A red dot will appear, indicating that a breakpoint has been set.

Start the Debugger

  1. Open the Run view.
  2. Click on the green Start Debugging button or press F5.

The debugger will start, and execution will pause at the breakpoint you set. You can then inspect variables, step through code, and evaluate expressions.

This is the complete process of debugging a Python program.

Additional Tips and Tricks

Here are some additional tips and tricks for working with your Python program using VS Code.

Using IntelliSense

IntelliSense is a powerful code-completion tool that helps you write code faster and with fewer errors. It provides suggestions for variables, functions, and modules as you type.

  1. Start typing a variable, function, or module name in your Python file.
  2. Press Ctrl+Space to see a list of suggestions.

Formatting Your Code

Visual Studio Code supports code formatting to help you keep your code clean and readable.

  1. Open your Python file in Visual Studio Code.
  2. Press Shift+Alt+F to format your code.

Using Linting

Linting is a process that analyzes your code for potential errors and style issues. The Python extension for Visual Studio Code includes several linters, such as Pylint and Flake8.

  1. Open the Command Palette by pressing Ctrl+Shift+P.
  2. Type Python: Select Linter and press Enter.
  3. Select a linter from the list.

Conclusion

I hope now you can set up Visual Studio Code for Python development, write and run a “Hello World” program, and use some of the powerful features of Visual Studio Code to enhance your development experience.

You can also check:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.