Hello world with Visual Studio Code¶
Try me¶
Introduction¶
You are already familiar with the execution of code cells in Jupyter Notebooks. In this tutorial, we will see how to execute code cells locally in your computer, and how to further develop your code in Visual Studio Code. We will also see how to create a new project and execute your code in this IDE.
Downloading Python code from a Jupyter Notebook¶
Ok, we have covered a lot of ground on Jupyter Notebook, now that we are going to use another environment, how can we leverage all the code in our Notebooks. Do not worry! Google Colabs has a very useful feature that allows you to download the code in your Notebook as a Python file. To do so, click on File->Download as -> *.py. You should see a file with the same name as your Notebook, but with the extension .py. This is a Python file, which you can open with any text editor, such as Notepad,
or any IDE, such as Visual Studio Code. For instance, if you download the code in this Notebook, you should see a file named Hello World.py that contains the code in the following code cell:
[2]:
print("Hello World")
Hello World
Let us use this code to see how to execute Python code in Visual Studio Code.
Working in Visual Studio¶
Creating a new project¶
Ok, now that you have seen how to execute a code cell, let us see how to work with Visual Studio. First, create a workspace folder in your system. This will be the folder where you will store all the files for this course. For instance, you can name it Computer Science. Then, open Visual Studio and select the option to open a folder. Select the folder you have just created. You should see a screen similar to the one below.
Now, you can either move the ‘Hello World.py’ file you downloaded from Colabs or create a new file and name it hello-world.py. You can do this by clicking on the file menu and selecting the option to create a new file, or with (CTRL+N), and then entering the name of the file. Then, copy the code in the cell above in the file and save it (CTRL+S) or File->Save. You should see the file in the explorer on the left side of the screen.
print("Hello World")
Executing your code¶
Now, to execute your code, you can either click on the Run button in the top right corner of the screen, or press F5. You should see the message “Hello World” in the output. You can also try to edit the message to a different message and execute the code cell again to see what happens, but please, mind your language!
You can also execute your code by opening a terminal and typing:
python hello-world.py
This is how others could also execute your code, so it is a good idea to get familiar with this way of executing your programs. To open a terminal, click on the Terminal menu of Visual Code and select New Terminal. You will see a terminal window in the bottom panel, which you can use just as Power-Shell (Windows) or Terminal (MacOS). Now, just enter the command `python hello-world.py´, you should see the message “Hello World” in the output. You can also look for PowerShell or Terminal in your computer, CD (change directory) to the folder where you have your code and execute the same command.
Extra¶
Accessing documentation¶
You can access the documentation of any function by placing the cursor on top of the function. You should see the documentation of the function in a pop-up window on top the function. If you want to learn more about the syntax of the documentation, head to our next tutorial on built-in functions.
Debugging your code¶
You can also debug your code in Visual Studio. To do so, first you need to enter what is called a Breakpoint. Debugging provides you extra control