In the previous blog, we've learn about history of Python, features and it's application. In this blog, we'll dive deeper into the world of Python to know more about it's features, language features, python versions and how code is converted into executable code and how it internally works. So let's get started....
Python is a high-level, general-purpose programming language that has gained immense popularity since its creation in 1991 by Guido van Rossum. Python's versatility allows it to be used in different domains, making it a favorite among developers.
Python's features include-
- Easy-to-learn - Python has few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language quickly.
- Easy-to-read - Python code is more clearly defined and visible to the eyes.
- Easy-to-maintain - Python's source code is fairly easy to maintain.
- A broad standard library - Python's bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.
- Interactive Mode - Python has support for an interactive mode which allows interactive testing and debugging of snippets of code.
- Portable - Python can run on a wide variety of hardware platforms and has the same interface on all platforms.
- Extendable - You can add low-level modules to the Python interpreter. These modules enables programmers to add to or customize their tools to be more efficient.
- Databases - Python provides interfaces to all major commercial databases.
- Scalable - Python provides a better structure and support for large programs than shell scripting.
Apart from the above-mentioned features, Python has a big list of good features, few are listed below -
- It supports functional and structured programming methods as well as OOP.
- It can be used as a scripting language or can be compiled to byte-code for building large applications.
- It provides very high-level dynamic data types and supports dynamic type checking.
- It supports automatic garbage collection.
- It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
- It was designed with an emphasis on code readability, and its syntax allows programmers to express their concepts in fewer lines of code.
Python is a programming language that let's you work quickly and integrate systems more efficiently.
There are two major Python versions: Python 2 and Python 3. Both are quite different.
Reasons of increasing popularity
- Emphasis on code readability, shorter codes, ease of writing.
- Programmers can express logical concepts in fewer lines of code in comparison to languages such as C++ or Java.
- Python supports multiple programming paradigms, like object-oriented, imperative and functional programming or procedural.
- There exists inbuilt functions for almost all of the frequently used concepts. Philosophy is "Simplicity is the best".
Language Features
Interpreted
- There are no separate compilation and execution steps like C and C++.
- Directly run the program from the source code.
- Internally, Python converts the source code into an intermediate form called bytecodes which is then translated into native language of specific computer to run it.
- No need to worry about linking and loading with libraries, etc.
Platform Independent
- Python programs can be developed and executed on multiple operating system platforms.
- Python can be used on Linux, Windows, Macintosh, Solaris and many more.
Free and Open Source
- Redistributable
High-level language
- In Python, no need to take care about low-level details such as managing the memory used by the program.
Simple
- Closer to English language, easy to learn.
- More emphasis on the solution to the problem rather than the syntax
Embeddable
- Python can be used within C/C++ program to give scripting capabilities for the program's users.
Robust
- Exception handling features
- Memory management techniques in built.
Rich Library Support
- The Python Standard Library is very vast.
- Known as the "batteries included" philosophy of Python, it can help do various things involving regular expressions, documentation generation, unit testing, threading, databases, web browsers, CGL, email, XML, HTML, WAV files, cryptography, GUI and many more.
- Besides the standard library, there are various other high-quality libraries such as the Python Imaging Library which is an amazingly simple image manipulation library.
How is Python Source Code Converted into Executable Code
The Python source code goes through the following to generate an executable code
- Step 1: The Python compiler reads a Python source code or instruction in the code editor. In this first stage, the execution of the code starts.
- Step 2: After writing Python code it is then saved as a .py file in our system. In this, there are instructions written by a Python script for the system.
- Step 3: In this the compilation stage comes in which source code is converted into a byte code. Python compiler also checks the syntax error in this step and generates a .pyc file.
- Step 4: Byte code that is .pyc file is then sent to the Python Virtual Machine(PVM) which is the Python interpreter. PVM converts the Python byte code into machine-executable code and in this interpreter reads and executes the given file line by line. If an error occurs during this interpretation then the conversion is halted with an error message.
- Step 5: Within the PVM the bytecode is converted into machine code that is the binary language consisting of 0's and 1's. This binary language is only understandable by the CPU of the system as it is highly optimized for the machine code.
- Step 6: In the last step, the final execution occurs where the CPU executes the machine code and the final desired output will come as according to your program.
- Code Editor: Code Editor is the stage of programs where we write our source code. This is human-readable code written according to Python's syntax rules. It is where the execution of the program starts first.
- Source code: The code written by a programmer in the code editor is then saved as a .py file in a system. This file of Python is written in human-readable language that contains the instructions for the computer.
- Compilation Stage: The compilation stage of Python is different from any other programming language. Rather than compiling a source code directly into machine code. Python compiles a source code into a byte code. In the compilation stage Python compiler also checks for syntax errors. After checking all the syntax errors, if no such is found then it generates a .pyc file that contains bytecode.
- Python Virtual Machine(PVM): The bytecode then goes into the main part of the conversion is the Python Virtual Machine (PVM). The PVM is the main runtime engine of Python. It is an Interpreter that reads and executes the bytecode file, line by line. Here in the Python Virtual Machine translate the byte code into machine code which is the binary language consisting of 0s and 1s. The machine code is highly optimized for the machine it is running on. This binary language is only understandable by the CPU of a system.
- Running Program: At last, the CPU executes the given machine code and the main outcome of the program comes as performing task and computation you scripted at the beginning of the stage in your code editor.
Now, let's greet the programming world with a classic:
#to display Hello, world on the screen
print("Hello, World!")
#Output
Hello, World!
Comments
Post a Comment