Python – Hello World example

Python - basics

Python – Hello World example

Hello world example is the perfect way of starting any programming language. Let’s write the most basic program and understand what actually happens inside. 

Create a new python file

Earlier we installed the python tool for development which is PyCharm. Run the IDE and create a new project. After creating a new project , create a new python file. You should be able to see the window as below.

Python - environment setup and tools

Writing Program

When you are done with the above step, you can proceed with the hello world program. Write the program as described below.

print("Welcome to python programming")

Run the program

Running python program in this tool is easy. Just right click anywhere on the file and click on run file name, which will produce the output as below.

C:\Users\ashchd\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/ashchd/PycharmProjects/Basics/
Welcome to python programming

Process finished with exit code 0

What’s happening above

Python is an interpreted language. Python interpreter actually has the structure of a classic compiler.

We can write a python program using some editor or with the help of an Integrated Development Environment like PyCharm.

When we click on run program, python interpreter comes into the picture. Below steps are taken in order to execute a python program :

Lexical Analysis

Lexical analysis is the process of breaking the input source code into a series of “tokens” which might be keywords, literals, operators etc. as it is much easier to parse tokens instead of raw text.

This process of converting input source code into tokens is usually known as “lexical analysis“.

Code Parsing

Lexical analysis converts the source code into a token stream which must be logically structured or should have some meaning. Python’s parser takes this responsibility. It takes token stream as input and based on predefined rules declared in python grammar produces an Abstract Syntax Tree (AST).

The output of the parser is a structured tree defined by the grammar description.

Generating the code

Code generator takes the AST and produces the PyCodeObject as the output. You can say that PyCodeObject contains all the data and code required for the execution by the Python bytecode interpreter.

Code generation is the process of generating the python bytecode for the interpreter.

Executing the code

Code execution is basically the process of executing the python bytecode.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *