Python. Lesson 2: Running Python Programs

by Alex
Python. Lesson 2: Running Python Programs
In this lesson we will look at two basic approaches to working with the Python interpreter: interpreting lines of code directly from the keyboard in interactive mode and executing source code files in batch mode. We will also touch on some features of working with Python in Linux and MS Windows. The Python language is an interpreted language. This means that besides the program itself you need a special tool to run it. Let me remind you that there are compiled and interpreted programming languages. In the first case, the program is translated from a high-level language into machine code for a particular platform. Further, it is usually distributed to users as a binary file. No additional software is needed to run such a program (except for the necessary libraries, but these subtleties are beyond the scope of our discussion). The most common languages are C++ and C.
Programs in interpreted languages are executed by an interpreter and distributed as source code. To date, among the interpreted languages, the most popular is Python. If you haven’t yet installed the Python interpreter, it is time to do so. You can read more about it here. Python can run in two modes
  • interactive;
  • batch.

Interactive mode can be accessed by typing

> python

or

> python3

This will start Python in interactive mode and wait for the user to enter commands. Python в интерактивном режиме If you have a Python source code file and you want to run it, you must call the Python interpreter on the command line and pass your file as an argument. For example, for a file named test.py the launch procedure would look like this:

> python test.py

Interactive mode

Open Python in interactive mode and type in the following:

print(“Hello, World!”)
print(“Hello, World!”)
print("Hello, World!")

And press ENTER. In response, the interpreter will execute the given string and display the result of its work one line below. Пример работы в интерактивном режиме Python can be used as a calculator for various calculations, and if you add the necessary math libraries, it becomes almost equal to such packages as Matlab, Octave, etc. Various examples of calculations are listed below. The arithmetic operations will be explained in more detail in the following lessons. Примеры простых задач To exit interactive mode, type the command exit() and press ENTER. Included with the Python interpreter is IDLE (integrated development environment). It is essentially like an interpreter running in interactive mode with an extended set of features (syntax highlighting, object browsing, debugging, etc.). To run IDLE in Windows, go to the Python folder in the Start menu and find there a shortcut named “IDLE (Python 3.5 XX-bit)”. Python IDLE в Windows In Linux, there is no IDLE shell by default, so you need to install it beforehand. To do this, if you have Ubuntu, type at the command line (for Python 3.4):

> sudo apt-get install idle-python3.4

This will install IDLE on your computer. To start the shell, type:

> idle-python3.4

Below is the appearance of IDLE on Linux. Python IDLE в Linux

Batch mode

Now let’s run Python in source file interpretation mode (batch mode). Create a file named test.py, open it with any text editor, and type the following code:

a = int(input())
print(a**2)
a = int(input()) print(a**2)
a = int(input())
print(a**2)
This program takes an integer as input and prints the square of it. To run it, at the command line, type
> python test.py

An example of how the program works is shown in the window below. Работа Python в пакетном режиме

Summary

To run Python in interactive mode, you must type the name of the interpreter executable (python or python3) at the command line or run IDLE (integrated development environment). To run Python in batch mode, type the name of the interpreter and the file name of the Python program on the command line, separated by a space:

> python test.py

Related Posts

LEAVE A COMMENT