Python – Hello World exampleHello 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.
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 programRunning 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/Test.py Welcome to python programming Process finished with exit code 0
What’s happening abovePython 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 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“.
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.