In this article, we will write our first Python program. We will write a very simple Python program using the Visual Studio Code. In order to start Python programming, the first step is to choose the code editor.
There are several very good code editors available like,
-
PyCharm
-
Spyder
-
Visual Studio Code
-
PyDev
-
Idle
-
Wing
-
Vim
-
Sublime Text 3
-
Thonny
We can choose any editor of our choice, each IDE has its own PROS and CONS. We will not go into comparisons here. There are very nice comparison articles available; a couple of articles are referenced in the "References" section.
Since I am SharePoint/Office 365 developer, I'll go with my all-time favorite IDE, i.e., Visual Studio Code.
Python with Visual Studio Code
If you also go with Visual Studio Code, then please download and install it from
here.
Once Visual Studio Code is installed successfully, to start with the first program, we need to install "Python Extension for Visual Studio Code". Python extension can be installed from multiple locations.
Fig1: Python - Support for Python in Visual Studio Code
Here, as shown in the above Fig1, from the "Tools and languages" section, we can install support for various languages. Here, in my case, I already have Python extension installed so it is showing the "Python support is already installed" message.
Writing the first program
-
Create the new file in Visual Studio Code, save it as "My First Python Program", file will be saved with the extension ".py". All Python files have extension ".py" as,
Fig2: Python - Python file with extension ".py" in Visual Studio Code
-
Here if Python extension is not installed then at the bottom it asks for it as shown in below figure,
Fig3: Python - Recommendation for Python extension installation from the editor if it is not already installed
-
Let's write our first line of actual code. We will just print the line "Hello World - This is my first Python program" as shown in the below figure and we are done. We are using print() to print the line.
Fig4: Python - First line of Python code with print()
To run this program, just right-click in Editor and click on "Run Python File in Terminal".
Fig5: Python - Running Python program from Visual Studio Code editor
Once we click on this command, we will see the output in the Terminal window.
Fig6: Python - Python program output in Terminal Window of Visual Studio Code
Summary
So this is our first Python program. In upcoming articles, we will go into more depth :)