Python is an interpreted programming language, allowing a programmer to write and execute a python file or module directly, without any requirement to compile the program
Python (.py) files are written and then passed into the python interpreter to be executed
The interpreter is the program required to run Python code and scripts
The python
command (which is a reference to the python executable that comes with a Python installation) invokes the interpreter
Technically, the interpreter is a layer of software that works between a program and computer hardware to make the program run
In the core implementation of the language, interpreter is a program written in C, like CPython
The interpreter is able to run Python code in two different ways:
Python code can be run through an interactive session
To start a Python interactive session, open a command-line or terminal and then type
python
or for Python 3 type
python3
and then press Enter
>>>
) Typically, by mentioning interpreter
people refer to running interpreter in interactive session
The standard prompt for the interactive mode is >>>
Websites or books having >>>
with Python code denote that it is being run on an interactive interpreter session
While defining an indented code block(or continuation lines) the prompt changes to ...
(three dots or ellipsis, also called secondary prompt)
Code entered in a secondary prompt has to be indented or else error is thrown
Secondary prompt appears after a statement which requires an indented block, is entered, like conditional
statements (if
, for
) or function definition (def
) statements
>>> the_world_is_round = True
>>> the_world_is_round
True
>>> def indentrospect():
... if the_world_is_round:
... print("What is flat?")
...
>>> indentrospect()
What is flat?
As shown, nested blocks require additional indentation
Every expression and statement entered in an interactive session is evaluated and executed immediately
An interactive session allow to test built-in methods, keywords or any piece of code of a Python module or file
It can be handy in running scripts or experimenting with the language
It allows to import modules
>>>import math
>>>math.sqrt(81)
9.0
To quit the interactive mode use either of
quit()
and enter To run a python file, the name of the file is entered along with python
command
The file should have ".py" extension
cd
to go to the directory containing the python file(if not already in) python
followed by filename and press EnterThat should execute the file (assuming there is no error in its code)
Consider a file named nc_time.py
contains following code:
from datetime import datetime
while True:
t = datetime.time(datetime.now())
print("Current time is:", t.isoformat())
input()
Follow above steps to go to directory containing nc_test.py
and execute the file with command
python nc_time.py
It should show something like
This program runs in a continuous loop and prints time whenever user presses a key in terminal (except ctrl keys etc)
Press Ctrl + d
to quit program