How to Write a Python Program
To write a Python program, I use the free IDLE editor provided by the Python Foundation. It's the easiest way to get started.
How to Install the Python Interpreter on Your PC. You can download it for free from the official Python website. The package includes the IDLE editor and is available on all major operating systems (Microsoft Windows, Mac OS, Linux).
Writing a Python Program
The first step is to open the Python editor.
The language shell with the inline interpreter will appear.
To open the editor, click on File and select New File.
Now, the editor opens, and I can start writing the program's source code.
Once the program is developed, I need to save the source file.
Click on File in the top menu, then select Save.
If this is the first time saving the code, type the file name and click the Save button.
The source file is saved on your computer as a text file with a .PY extension.
Note. If the file has been saved before, changes are saved automatically without needing to retype the file name or click Save.
Now, I can compile the program to run it.
Running a Python Program
To compile and run the program, click on Run in the top menu.
Then select Run Module.
Note. Alternatively, you can quickly start the compilation and execution of the program by pressing the F5 key on your keyboard.
The compiler will start the compilation process and execute the program.
The output will be displayed in the Python shell.
Running a Python Program from the Command Line
Alternatively, you can run the program from the command line of your operating system.
In this case, you run the Python interpreter followed by the name of the program to execute.
The result will be displayed in the command line interface.
Compiling a Python Program
Python is an interpreted language, so you can't compile the source code into a directly executable object file.
To run a program, the Python interpreter must read the source code.
A Practical Example
On my PC, I have a Python program saved in the file example.py.
To run it, I type the source file name at the operating system prompt (e.g., DOS).
The operating system recognizes the PY extension, opens the Python interpreter, and executes the program's code.
It is essential that the Python interpreter is already installed on the PC. Otherwise, it won't work.
Is it possible to compile it? It's not necessary because the interpreter is sufficient. However, you can find unofficial compilers online that allow you to compile a Python program's source code into an executable object file, enabling it to run on a PC without needing the interpreter. For example, Py2exe creates object files for the Windows environment, and PyInstaller does the same for Mac.
Using Python from the Command Line
In Python, you can also run a program from the command line.
The command line is integrated with the Python interpreter.
What is the command line for?
With the command line, you don't edit the program's source code in a file.
However, it is still useful for testing the program's functionality.
A Practical Example
Open the command line and type the instruction name="Andrea"
to assign the string "Andrea" to the variable name.
>>> name="Andrea"
Then type the instruction print(name)
to display the content of the variable name.
>>> print(name)
The output of the instruction will be displayed on the next line.
Andrea
The previously typed instruction on the command line modified the data in memory and influenced the execution of the next instruction.
Note. In this case, I didn't edit the program's source code. I was still able to see the result of executing the two instructions in sequence.