***Welcome to ashrafedu.blogspot.com ***This website is maintained by ASHRAF***
***Use tabs for Content***

POSTS

    Python Code Execution

    Python’s traditional runtime execution model: 

    Source code you type is translated to byte code, which is then run by the Python Virtual Machine (PVM). Your code is automatically compiled, but then it is interpreted.


    There are two modes for using the Python interpreter:

    • Interactive Mode

    • Script Mode

    1. Running Python in interactive mode:

    Without passing python script file to the interpreter, directly execute code to Python prompt.

    Once you’re inside the python interpreter, then you can start.

    The chevron at the beginning i.e., the symbol >>> is a prompt the python interpreter uses to indicate that it is ready.

    >>> print("hello world")

    hello world    # Relevant output is displayed on subsequent lines without the >>> symbol

    >>> x=[0,1,2]    # Quantities stored in memory are not displayed by default.

    >>> x     #If a quantity is stored in memory, typing its name will display it.

    [0, 1, 2]

    >>> 2+3

    5


    2. Running Python in script mode:

    Alternatively, programmers can store Python script source code in a file with the .py extension, and use the interpreter to execute the contents of the file. To execute the script by the interpreter, you have to tell the interpreter the name of the file.

    To run the script you have to type:

    python MyFile.py

    Working with the interactive mode is better when Python programmers deal with small pieces of code, but when the code is more than 2-4 lines, using the script for coding can help to modify and use the code in future.