What is python programming code

What is Python? How the Interpreter Works and How to Write «Hello World» in Python

Michael Para

Michael Para

What is Python? How the Interpreter Works and How to Write

In this article, I am going to explain what Python is and how the Python interpreter works. Then you’ll write your first «Hello World» program.

What is Python?

Python is a high-level programming language designed to do many tasks. It’s based on the CPython interpreter which translates the Python code into something the machine can read.

Python gives us the ability to use a lot of modules and packages with our code, which are standard libraries built in with the interpreter.

You can use Python to do many tasks such as:

  • Machine Learning
  • Artificial Intelligence
  • Data Visualization
  • Programming Applications
  • Web Applications
  • Language and Game Development
  • Data Analytics

Additionally, Python’s syntax is pretty simple and easy to learn – often it seems that you are just writing a message to someone else. Just make sure you know the indentation rules :).

We can compare Python with other interpreted programming languages such as Java, JavaScript, PHP, and others. But you might be wondering – what is CPython?

In the following section, I am going to focus on the history of the Python interpreter in-depth, then I’ll answer this question.

Python History Overview

The first appearance of the Python programming language was in late 1980s. It was created by Guido van Rossum.

Python was designed to replace the ABC programming language which worked with Amoeba operating system.

Rossum started the implementation in 1989 and he worked on Python alone until 2018.

He named it Python because the first version of it was able to read the BBC comedy script “Monty Python’s Flying Circus”.

The first release was in 1994 under version 1.0 (Python 1.0) and the second release was in 2000, named version 2.0.

In version 2.0, van Rossum added minor features such as collection systems and comprehensions.

The third version was released in 2008 and fixed a basic flaw of the language. They named this version “Py3K”, or Python 3.0.

How Does the Python Interpreter Work?

The Python interpreter is called “CPython” and it’s written in the C programming language. This is the default implementation for Python.

In the following sections, you will understand how the Python interpreter works behind the scenes.

Source Code Analysis

Actually, any translator starts with the source code analysis. Here the Python interpreter receives the source code and initializes some instructions to do the following things:

It follows the indentation rule and checks the Python syntax. Maybe there are some incorrect lines, so it will stop the program from the execution to show the error message.

This phase is called lexical analysis, which means dividing the source code files into a list of tokens

In the following step, the interpreter will generate byte codes. Let’s see how that works.

Byte Code Generation

Once the parser of the Python interpreter receives the tokens, it starts to manipulate the lexical tokens. It generates a big structure called the AST (Abstract Syntax Tree).

The interpreter converts this AST to byte code which means machine language. In Python, the byte code can be saved in a file ending with the “.pyc” extension.

In the following section, you will see how the python interpreter executes these byte codes.

The Python Virtual Machine (PVM)

The Python interpreter initializes its runtime engine called PVM which is the Python virtual machine.

The interpreter loads the machine language with the library modules and inputs it into the PVM. This converts the byte code into executable code such as 0s and 1s (binary).

And then it prints the results.

Note that if an error happens during the PVM process, the executor will terminate the operation immediately to display the error.

Now you will learn how to install Python on your operating system.

If you have no Python software or you’re using a mobile device, you can use any online Python compiler.

How to Install Python

To install Python on your Ubuntu Linux operating system, follow these instructions:

Open your terminal and run the following command to update the Ubuntu local system repository:

image-67

In the next section, you will learn how to write your first program with Python.

How to Write Your First Python Program

In this program, you’re going to print the classic “Hello World” message using the Python programming language.

First, create a folder and name it “CodedTag” then create a file inside and name it as a “page.py”.

Then copy and paste the following Python code:

image-68

Congratulations – you just wrote your first Python program.

Wrapping Up

In this article, you learned what Python is and a bit of its history. You also learned how the Python interpreter works.

Let’s summarize it in a few points:

  1. The interpreter checks and searches for syntax errors and verifies indentation rules. Then it converts the source code via tokenization.
  2. The parser receives the lexical tokens and generates an Abstract Syntax Tree.
  3. The interpreter converts the AST to Byte Code and initializes the Python Virtual machine to execute the byte code and send back the final result.

Thank you for reading, If you’d like to read more of my articles, you can find them on CodedTag. Stay tuned for my next articles.

Источник

Python Tutorial

This Python Tutorial is very well suited for Beginners, and also for experienced programmers with other programming languages like C++ and Java. This specially designed Python tutorial will help you learn Python Programming Language in the most efficient way, with topics from basics to advanced (like Web-scraping, Django, Deep-Learning, etc.) with examples.

Python Tutorial

What is Python?

Python is a high-level, general-purpose, and very popular programming language. Python programming language (latest Python 3) is being used in web development, Machine Learning applications, along with all cutting-edge technology in Software Industry.

Python language is being used by almost all tech-giant companies like – Google, Amazon, Facebook, Instagram, Dropbox, Uber… etc.

The biggest strength of Python is huge collection of standard library which can be used for the following:

  • Machine Learning
  • GUI Applications (like Kivy, Tkinter, PyQt etc. )
  • Web frameworks like Django (used by YouTube, Instagram, Dropbox)
  • Image processing (like OpenCV, Pillow)
  • Web scraping (like Scrapy, BeautifulSoup, Selenium)
  • Test frameworks
  • Multimedia
  • Scientific computing
  • Text processing and many more..

Why Learn Python?

Python is currently the most widely used multi-purpose, high-level programming language, which allows programming in Object-Oriented and Procedural paradigms. Python programs generally are smaller than other programming languages like Java. Programmers have to type relatively less and the indentation requirement of the language, makes them readable all the time.

Getting Started with Python Tutorial

Here are the important topics that come under Python. After completing all the important topics, you’ll have a basic understanding of the Python programming language:-

Python Basics

  • Python language introduction
  • Python 3 basics
  • Python The new generation language
  • Important difference between python 2.x and python 3.x with example
  • Keywords in Python | Set 1, Set 2
  • Namespaces and Scope in Python
  • Statement, Indentation and Comment in Python
  • Structuring Python Programs
  • How to check if a string is a valid keyword in Python?
  • How to assign values to variables in Python and other languages
  • How to print without newline in Python?
  • Decision making
  • Basic calculator program using Python
  • Python Language advantages and applications

Input/Output

Источник

Читайте также:  Java stream sort reverse
Оцените статью