Python Coding Instructions
In this article, I am going to discuss the Python Coding Instructions with Examples. Please read our previous article where we give an overview of Python. As part of this article, we are going to discuss the following pointers.
- Installation procedure
- Different ways to write a python program
- Execution steps and process
- Understanding a simple program
- Program execution flow
- Python Virtual Machine
Installation procedure For Windows:
You can download a python installer from the https://www.python.org/downloads/ depending upon the operating system and the version (32-bit or 64-bit version) your system has. Download the latest stable version. (The current version is 3.x.x.x)
After downloading the installer from the above site you have to run the installer and follow the instructions in order to get the Python installed in your system. During the setup window make sure to click on “Add Python 3.6 to PATH” or ‘Add Python to your environment variables” checkbox before clicking on Install Now.
Note: Don’t forget to check the box as mentioned above. If not checked then you have to add the Python to your environment variables.
Installation procedure For For Linux:
For linux, the python will come pre-installed. You can check the version of it with the command “python3 –version”. If you don’t have the python installed then you can follow depending on the Linux distribution.
- For Ubuntu: sudo apt install python3
- For Fedora: sudo dnf install python3
- For SUSE: sudo zypper install python3
Ways to write python program
All the programs written in python should be saved using the ‘.py’ or ‘.python’ extension. Files can be executed using command prompt or terminal (based on OS). For writing the python programs we can use the following things
- By using a text editor like notepad++, vi editor etc…
- You can use IDEs available online like VS Code, Atom, PyCharm etc..
Python has a special feature of interactive mode where the python code can be executed line by line. In the command prompt/terminal if you type ‘python’ then the interactive mode comes as shown below
Python 3.5.2 (default, Apr 16 2020, 17:47:17)
[GCC 5.4.0 20160609] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>> print(“Hello World”)
Note: In interactive mode, we type the instructions line by line. exit() is used to quit from the interactive mode.
An IDLE is automatically installed while installing the python. If you have python installed on the system then search for it using ‘IDLE’. You can use that IDLE for both interactive mode as well as normal mode of executing the python file using command. For this tutorial, it is suggested to follow the below steps for execution.
Program execution steps
- First write the code in notepad or IDE (Initially, it would be better to write the code in notepad if you are new to IDEs)
- Save the file with .py or .python extension.
- Run or execute the program (For windows execute the file from command prompt and for linux from terminal)
- Command for execution – “python filename.py” or “py filename.py”
Program to print hello world
Output: HELLO WORLD
print(“HELLO WORLD”): print() is a predefined function/method in python. It takes strings values as input parameters. It prints the output on the console.
Python program flow
The below flow is followed during execution
First we need to write a code and save the file with .py extension. Then we need to run the program. When the program is running, the Internal python compiler takes the source code and creates the corresponding compiled python file, which is not visible to the user
The compiled file is saved in the computer cache. If you want to see the file, then you can run the below command
python -m py_compile demo.py
Here, -m argument states that its a module and the module name is py_complile. This is the module which creates the compiled file which is stored as __pycache__ in the same directory.
Now the compiled file contains byte code instructions which are not understandable by the microprocessor to generate output. So, the Python Virtual Machine comes into picture and takes the responsibility of converting the byte code instructions to machine understandable format and it does this line by line. Finally, we will see the output
PVM uses an interpreter which converts line by line, which is very slow. To resolve this, some flavors like PyPy use compilers like JIT(Just in Time) which convert very fast.
In the next article, I am going to discuss Naming Convention in Python. Here, in this article, I try to give an overview of Python Coding Instructions. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.