Python Part 2 — Writing Our First Python Program in VS Code — “Hello World!”
In this article we’ll install Visual Studio code, write a Python program and run it as an application.
In part one of the series, we installed python and run the interactive mode where we wrote our first code. We used Python interpreter in the interactive mode in the command prompt. Now we are going to use an editor to write our code.
To write Python code and compile it to an application, we need to write it inside a file. An editor can help us in many ways like highlighting the keywords, detecting our syntax errors which are the typing mistakes we make, and a lot of other features that we’ll see later.
From the many existing editors, we will choose for this Python series the Visual studio code (VS Code) which is not only used for Python but for most programming languages. It can be installed on almost all platforms from Linux to Windows or Mac.
Download Visual Studio Code and its Python Extension
So, let’s download and install VS Code, go to VS Code website and follow the instructions to download it, it’s straightforward.
Next, from the Visual Studio Marketplace, we’ll install the Python extension for VS Code, this will make VS Code a complete IDE (Integrated Development Environment) where we will be able to edit, run, and debug Python code.
We are now ready to create our first application. We will create a folder to hold the files of the application and open VS Code inside the folder. We can use the command prompt to do that or the VS Code GUI.
Open VS Code in a folder
In the command prompt write the following commands:
- The first command (mkdir) makes a directory called “helloWorld” which is our application folder.
- The second command (cd) changes our directory to “helloWorld”
- And the third (code) followed by a dot opens VS Code inside this folder.
So, a new folder “helloWorld” was created and VS Code was started inside it, this will become our workspace.
The other way is to manually create the folder anywhere you want then start VS code and click on Open Folder, browse for your created and folder and open it, this will make the folder your workspace as you can see in the left pane of VS Code in the File Explorer toolbar.
Adding the Python Source Code File to the project
Create a new file by clicking on the plus icon in the File Explorer toolbar or clock on the New file link in the right “Welcome” page, a file will be created inside the folder and is shown in the File Explorer. Name it “main.py”, you can call it any name you want and the “.py” extension indicates that it is a Python file. The file will automatically open to editing it on the right as shown below:
Now, inside the main.py file we created, write the following code:
Click on the Run Python File in Terminal play button in the top-right side of the editor (the green) to compile it and run it at the same time.
As its name, it will open a terminal at the bottom where our program was compiled/ translated by the Python interpreter (which is python.exe in Windows). The main.py file that includes our source code was given as a parameter and the output was the “Hello World” sentence that was printed on the screen.
Great job! you have written your first complete python application now it’s time to dive into more serious programming stuff.
In the next tutorial we’ll see how to read inputs from the users of our app and learn about variables and comments, see you there.