Python Compilation and Working !!

Table of Content

  1. Understanding the difference between high level and low level language.
  2. Interpreted v/s Compiled Language
  3. Introduction to Python
  4. How Python Works ?
  5. Python Interpreter
  6. Python Standard Library
  7. Python Implementations
  8. Python Installation
    • Python Installation on Linux Machine
    • Python Installation on Windows Machine
    • Python Installation on MacOS
  9. Conclusion

Understanding the difference between High & Low-level Languages

High-Level Language: High-level language is easier to understand than is it is human readable. It is either compiled or interpreted. It consumes way more memory and is slow in execution. It is portable. It requires a compiler or interpreter for a translation.

The fastest translator that converts high level language is .

Low-Level Language: Low-level Languages are machine-friendly that is machines can read the code but not humans. It consumes less memory and is fast to execute. It cannot be ported. It requires an assembler for translation.

Interpreted v/s Compiled Language

Compiled Language: Compiled language is first compiled and then expressed in the instruction of the target machine that is machine code. For example – C, C++, C# , COBOL

Interpreted Language: An interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program and these kinds of languages are known as interpreter languages. For example JavaScript, Perl, Python, BASIC

Introduction to Python

Python is a high-level language, which is used in designing, deploying, and testing at lots of places. It is consistently ranked among today’s most popular programming languages. It is also dynamic and object-oriented language but also works on procedural styles as well, and runs on all major hardware platforms. Python is an interpreted language.

How does Python Works?

Bytecode, also termed p-code, is a form of instruction set designed for efficient execution by a software interpreter

  • First step is to write a Python program such as test.py
  • Then using Python interpreter program is in built compiled and gets converted into byte code that is test.pyc.
  • Python saves byte code like this as a startup speed optimization. The next time you run your program, Python will load the .pyc files and skip the compilation step, as long as you haven’t changed your source code since the byte code was last saved.
  • Once your program has been compiled to byte code (or the byte code has been loaded from existing .pyc files), it is shipped off for execution to something generally known as the Python Virtual Machine
  • Now byte code that is test.pyc is further converted into machine code using virtual machine such as (10101010100010101010)
  • Finally Program is executed and output is displayed.
How Python runs? – Indian Pythonista

Python Interpreter

Python includes both interpreter and compiler which is implicitly invoked.

  • In case of Python version 2, the Python interpreter compiles the source file such as file.py and keep it in same directory with an extension file.pyc
  • In case of Python version 3 : the Python interpreter compiles the source file such as file.py and keep it in subdirectory __pycache__
  • Python does not save the compiled bytecode when you run the script directly; rather, Python recompiles the script each time you run it.
  • Python saves bytecode files only for modules you import however running Python command with -B flag avoids saving compiled bytecode to disk.
  • You can also directly execute Python script in the Unix operating system if you add shebang inside your script.
#! /usr/bin/env python

Python Standard Library

Python standard library contains several well-designed Python modules for convenient reuse like representing data, processing text, processing data, interacting with operating systems and filesystems, and web programming. Python modules are basically Python Programs like a file (abc.py) that are imported.

There are some extension modules that allows Python code to access functionalities supplied by underlying OS or other software’s components such as GUI, database and network, XML parsing. You can also wrap existing C/C++ libraries into python extension modules.

Python Implementations

Python is more than a language, you can utilize the implementation of Python in many ways such as :

  • CPython: CPython is an interpreter, compiler, set of built in and optional extension modules all coded in C language. Python code is converted into bytecode before interpreting it.
  • IronPython: Python implementation for the Microsoft-designed Common Language Runtime (CLR), most commonly known as .NET, which is now open source and ported to Linux and macOS
  • PyPy: PyPy is a fast and flexible implementation of Python, coded in a subset of Python itself, able to target several lower-level languages and virtual machines using advanced techniques such as type inferencing
  • Jython: Python implementation for any Java Virtual Machine (JVM) compliant with Java 7 or better. With Jython, you can use all Java libraries and framework and it supports only v2 as of now.
  • IPython: Enhances standard CPython to make it more powerful and convenient for interactive use. IPython extends the interpreter’s capabilities by allowing abbreviated function call syntax, using question mark to query an objects documentation etc.

Python Installation

Python Installation on Linux Machine

If you are working on the Latest platforms you will find Python already installed in the systems. At times Python is not installed but binaries are available in the system which you can install using RPM tool or using APT in Linux machines and for Windows use MSI( Microsoft Installer ) .

Ubuntu 16 server
Ubuntu 18 server

Python Installation on Windows Machine

Python can be installed in Windows with a few steps, and to install Python steps can be found here.

Python Installation on macOS

Python V2 comes installed on macOS but you should install the latest Python version always. The popular third-party macOS open-source package manager Homebrew offers, among many other packages, excellent versions of Python, both v2 and v3

  • To install Homebrew, open Terminal or your favorite OS X terminal emulator and run
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
  • Add homebrew directory at the top of your PATH environment variable.
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
  • Now install Python3 using the following commands.
brew install python3
  • Verify the installation of Python using below command

Conclusion

In this tutorial, you learned a basic introduction to python, why it is interpreted, and high-level language. Also, you learned lots of details of python data types, keywords, and how python works. There were a handful of examples that you learned. Hope this tutorial will help you and if you like please share it.

One thought on “Python Compilation and Working !!

Leave a comment