Create your first Python application

Completed

With Python and Python tools installed, you can create your first Python application! In this exercise, you'll create an empty folder, open the folder in Visual Studio Code, and then create your first application.

Step 1 - Start VS Code in a project folder

Many projects start with an empty folder, which is how you'll start yours.

  1. In Visual Studio Code, open a new Terminal window by selecting Terminal > New Terminal.

  2. Create an empty folder called "hello-world", navigate into it, and open VS Code (code) in that folder (.) by entering the following commands:

    1. Create a new folder called hello-world:

      md hello-world
      
    2. Navigate to the hello-world folder:

      cd hello-world
      
    3. Open Visual Studio Code in that folder:

      code .
      
    1. Create a new folder called hello-world:

      mkdir hello-world
      
    2. Navigate to the hello-world folder:

      cd hello-world
      
    3. Open Visual Studio Code in that folder:

      code .
      
    1. Create a new folder called hello-world:

      mkdir hello-world
      
    2. Navigate to the hello-world folder:

      cd hello-world
      
    3. Open Visual Studio Code in that folder:

      code .
      

    Tip

    Open the command prompt or terminal as administrator to run code .

    Alternatively, you can run VS Code through the operating system UI, then use File > Open Folder to open the project folder.

Step 2 - Create a new Python file and add code

With Visual Studio Code open to your empty folder, you'll now create a Python file to display the message Hello, World.

  1. In the Explorer view, HELLO_WORLD panel, hover over the title bar, and then select New File.

    Screenshot of the Visual Studio Code Explorer window with New File highlighted.

  2. Name the new file hello.py by entering it into the new textbox, and pressing Enter.

    Screenshot of Explorer window with hello.py entered for new file.

    By using the .py file extension, you tell VS Code to interpret this file as a Python program, so that it evaluates the contents with the Python extension.

  3. Enter the following Python code in the editor panel. This command uses the print function to display the text Hello, World! when your application is run.

    print('Hello, World!')
    
  4. Save the file by selecting File and Save (or Ctrl+S).

    Screenshot of file menu with Save highlighted.

Step 3 - Run your application

Since it's a single line program, you can actually run your application from inside Visual Studio Code.

  1. Open the built-in terminal in Visual Studio Code by selecting View and Terminal.

    Screenshot of view menu with Terminal highlighted.

  2. In the new terminal window, run the following command to run your Python code.

    python hello.py
    
    python3 hello.py
    
    python3 hello.py
    

    Screenshot of running the Python code.

    Hello, World! appears in the terminal window. Congratulations! You've created a Python application!