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.

click on File and then New File

Now, the editor opens, and I can start writing the program's source code.

begin editing the program's source code using Python

Once the program is developed, I need to save the source file.

Click on File in the top menu, then select Save.

click on File and then Save

If this is the first time saving the code, type the file name and click the Save button.

enter the name and click SAVE

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.

how to compile and run the Python program

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.

the result of running the compiled executable file

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.

an example of running a Python program from MS-DOS

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.

An example of interpreted 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.

where to find the command line

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.

the Python command line

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.

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Python

  1. The Python Language
  2. How to Install Python on Your PC
  3. How to Write a Program in Python
  4. How to Use Python in Interactive Mode
  5. Variables
  6. Numbers
  7. Logical Operators
  8. Iterative Structures (or Loops)
  9. Conditional Structures
  10. Exceptions
  11. Files in Python
  12. Classes
  13. Modules

Miscellaneous

Source