Monday 29 April 2019

2. Coding Introduction

2.   Python coding introduction   -     10 min read
  • Installing Python on Windows OS
  • Ways to write a python program
  • Python program execution steps
  • Understanding first python program
  • First python program executing by using python IDLE
  • Python program internal flow: Short answer
  • Python program internal flow: Long answer
  • Python Virtual Machine
  • JIT Compiler
  • Programs
2. Python coding introduction

2.1 Ways to write a python program
  • We can write the python programs in two ways.
    • By using any text editor like Notepad++, Edit plus.
    • We can also write python programs by using python IDLE(Integrated Development Environment)
2.2 Python program execution steps
  • We need to write python programs in notepad (good approach for practice).
  • We can save the program with .py or .python extension.
  • Run or execute the program.
  • Finally, we will get output.

Program       Print Welcome to python world message
Name          demo.py

print(“Welcome to python world”)               


Run             python demo.py
or
Run             py demo.py


output
Welcome to python world


2.7 Understanding first python program
  • print("Welcome to python world”),
    • print() is a predefined function.
    • print() function takes string values as parameters.
  • This function prints the output on the console.
2.6 Execute first Python program by python IDLE or command prompt
  • Open python IDLE
  • ü  Then write directly like, print(“Welcome to python world”)
Example
>>>print (“Welcome to python world”)
Welcome to python world

2.3 Python program flow
Short answer
  • Python program (source code), we need to save with .py (dot py) or .python (dot python) extension.
    • Example      :        demo.py
  • We can run this program by using python command.
    • While running the program, internally automatic compilation will be done.
    • If everything perfect as per compiler checking then goes to next level to execute the program.
    •  Example      :        python demo.py
  • We can see the output on the console.
2.3 Python program flow
  • Long answer but more helpful for internal understandings…
  • This answer mainly contains three parts,
    1. Write and Run the program
    2. Compiled python file
    3. Python Virtual machine
1. Write and run the program
  • Python program (source code), we need to save with .py (dot py) or .python (dot python) extension.
    • Example      :        demo.py
  • Next step is, we need to run this program (demo.py).
2. Compiled python file
  • While running the program, the first step is, internally python compiler takes this source code and creates corresponding compiled python file to the source code.
    • This compiled python file is not visible.
    • It is a hidden file, stored in cache memory.
    • If we want to see this file then run this below command,
    • python -m py_compile demo.py
  • -m means module here py_compile is module name.
  • This module creates a compiled python file.
  • So, python creates a separate folder in the current directory by the name _pycache_ to store compiled python file.
3. Python Virtual Machine
  • This compiled python file contains byte code instructions.
  • These byte code instructions are not understandable by the microprocessor to generate output.
  • While running the program, Python Virtual Machine takes responsible to convert these byte code instructions into machine understandable format
    •  Example      :        python demo.py
  • Finally, we will see the output
JIT compiler (Just In Time)
  • Python Virtual Machine internally uses interpreter which converts line by line, so it is very slow.
  • To resolve this problem few python flavours (PyPy) uses compiler which converts very faster than an interpreter.
  • This compiler is called JIT (Just In Time) compiler.
  • It improves the speed and execution of a python program