In this chapter, we are going to setup Python and other required tools that will help to execute Python code on our machine. In the end, we will write a "Hello World" program in Python to make sure everything is up and running to get started for upcoming chapters.
Python Setup
To install Python compiler and corresponding files on your machine, you need to visit the Python's official website to download Python for your OS. Please follow the steps provided(if any) in the downloads page mentioned below.
Code Editor Setup
When its a matter of choosing a right code editor, everyone have their own favourites in terms of their pros and cons or usability or comfortability. The same way, If you ask me, I would recommend to install VS Code to start coding in python. VS Code is very popular code editor and provide hundreds of extensions and plugins to install. It is light and very fast in terms of performance and is available for many Windows, Mac and Unix. However, feel free to choose your own code editors which you use to work but check once if it supports extensions/plugins for latest version of Python.
Here is the official download link for VSCode :
Download VSCode
Install this in your machine as per the instructions provided in the download page.
Post VSCode installation
After installation finishes, open VS Code and follow the below steps:
- Click on the extension icon on the left side bar of VS Code. Type "Python" in Search extensions input.
- Look for the extension name "Python" provided by Microsoft. Click on "install" to install the same.
- Now restart VS Code.
"Hello World" in Python
In this section, you are going to learn - how to write a "Hello World" program in Python with all your code setup done till now. Follow the instructions below:
- Open a new file and save it as "helloworld.py". Note that python programs should be saved with ".py" extension.
- Copy and paste the following code
print("Hello World")
- Save the file and press Alt+Ctrl+N to execute the program. You can see the output in "Output" window of VS Code.
Congratulations, you have successfully setup Python in your machine. Now we are ready to start programming in Python.
Note: Python framework provides you with a package manager called - "pip". Just like you use #include<..> in a C program to include external libraries. Similarly, pip help you to download any external library from its common online repository.
Optional Reading
This is an optional reading related to some more information on Python. You can skip this part.
As Python gets installed in the system, it registers its working directory path to global environment. So its runtime is available in shell or terminal. You can start terminal and write the following command:
python -c command [arg] ...
This will execute a python command. You can also open Python interactive shell from the installation folder (different OS will have different installation path): \Python\Python37\python.exe. Here you can directly start writing Python code. Type print("Hello World") to see the output "Hello World" on terminal.
You can also write the following statements to see how this python terminal works:
1+2
a=2
b=3
a+b*5
print("Hello","World","!")