With the developments in Artificial Intelligence and data gaining more and more power, Python is becoming increasingly relevant. It won’t be wrong to say that Python is the most popular programming language in current times. If you google “most in-demand programming language”, you will find that almost every blog post lists Python as either number 1 or number 2 in rank.
In short, if you are looking to develop a valuable skill in 2022 then Python is definitely a safe option.
We’ll be discussing the following details:
- Installing Python on Windows.
- Installing a suitable IDE.
- Running your first python file.
- Running a ‘.py’ file
- Indentation of the text
Let’s get on with step 1
You can download Python from its official website here. The latest version of Python is 3.10.1 (at the time of writing this article). It’s not the most popular one but we will be using it for the purpose of this article.
Click on the highlighted button to start the download. Once the EXE file is downloaded, run the setup and you will see the following window.
Installing Python is as easy as simply pressing the “Install Now” button (Highlighted with the Green box) but before that, it is important to check the “Add Python 3.10 to PATH” check box at the bottom (Highlighted in Red). Adding to PATH means you are telling the windows command line where to look when running a python file, we’ll see the advantage of this in the next section.
Since you are just beginning your python journey you don’t need to worry about any of the other options given in this installer and you can start working immediately after the installation is done.
Checking if Python is installed correctly
The easiest way to do this is by going to your windows start menu and searching for “Python 3.10“. You will find it as an installed program. Another way of checking for Python installation is by going to the command prompt (search cmd in your start menu and click the command prompt app) and then typing ‘python‘ on the command line interface that appears and pressing enter.
Windows was able to identify ‘python‘ by simply typing its name because we added the python installation path to the PATH environment variables during installation.
What you see in the command prompt above is a python interpreter, all you need to do is type a line of python code and hit ‘Enter’ and it will return the output in the same window. Unlike C or C++, you do not need to import any header files or libraries to get started with python (that is why Python is considered a very simple language). Just start coding. Let’s print out first hello world in the command prompt.
The syntax for a print statement in Python is shown below:
Code language: PHP (php)
Installing an IDE
Even though you can run your Python code on the command line, as I demonstrated above, that is not how development is done. Programmers use IDEs to write and edit code. IDE stands for Integrated Development Environment and it is a software that is basically a high-level code editor. You can think of it as ‘notepad’ but with perhaps a gazillion more features. Some of the most popular IDEs for Python are:
I personally use the ‘Visual Studio Code‘ editor since it is lightweight, has most of the useful features of other IDEs, and supports tons of additional plugins. You can download VS code here.
The installation of this is quite straightforward (just like any other software) so I will not be showing that. Once it is installed just search for ‘Visual Studio Code’ in your search bar and click on the app displayed.
This is what the application looks like, the welcome screen may look different for you if you are starting it for the first time or are using a different version. All you need to do is click on File->New File to open a new document to write your code.
You will see a text editor like this. VS Code is not specifically a Python editor, you can use it for other languages as well. In order to let it know that you are writing Python code, you need to save your file with the appropriate extension. Python files end with ‘.py‘. So let’s save this empty file first with the name ‘test.py’. File->”Save as” then browse to whichever folder you like and save with our decided filename.
Once saved as a ‘.py’ file VS Code automatically detects it as a Python file and you can continue coding.
If you look at the bottom left of the toolbar you can see that python has been auto-detected by VS Code. You click on this to change the language manually. If you look at the right you can see that the interpreter is currently using Python 3.10.0 (Since that is which I have installed), you can click on this and select a different version of Python if you have multiple installed.
Running your first code
Let’s try the ‘Hello World!’ code in this file.
You can see that the IDE has color-coded the code. This is one of the advantages of using an IDE as this makes the code much easier to read.
‘print’ is a built-in function in Python and you can call it by using the ‘print’ keyword as above and passing in your string.
You can read more about Python functions here.
Now to run the code above you need to once again go to the command prompt by searching for ‘cmd‘ in the start menu and then clicking on ‘Command Prompt‘. When the command prompt opens up, you will be in the ‘users’ folder by default. To run this ‘test.py‘ file you need to navigate to the appropriate folder in the command line. Since my file is placed on the ‘Desktop’ I can simply navigate to the desktop by using the following command.
If your file is placed in some other directory you will have to navigate accordingly.
Now in order to run the file, all I need to do is type ‘Python’ followed by a space and then the name of the file. Then simple press enter and the code should be executed.
Code language: CSS (css)
and just like that, the code was executed and we have our output on the console.
Now let’s write a little more complex code.
Code language: PHP (php)
for i in range(6): for j in range(i): print('*', end ='') print('')
The code above prints out a triangle of ‘*’ on the console.
The above code introduces you to 2 Important concepts in Python programming.
Discussing loops is beyond the scope of this article but Indentation is something that we must address.
Python identifies code blocks by how the text is indented.
In our code, the second for loop is indented which means that it will run as part of the outer loop. The first print statement is indented twice which means it will be executed as part of the second for loop. This concept may be a little confusing to grasp straight away but the only way to clear this out is by practicing more code.
And that is it for this tutorial. You now have python installed and up and running on your computer. Practice more coding to enhance your skills and for more Python tutorials, look at the following articles.