To run python on your local environment, you must first install python on your computer. Python installation is easy. For this purpose, you can follow these steps:
Most distributions already include Python. If not installed, enter this command in the terminal:
sudo apt install python3
You can Install via:
brew install python
Open terminal/command prompt and enter command below to check if python installed successfully or not.
python --version
or
python3 --version
If version appears, its means installation is successful.
In the previous article we read about rule of compilers and interpreters in translating high-level languages into machine language. Python is considered an interpreter language, meaning programs are executed through an interpreter.
There are two ways to use the python interpreter:
In this mode, you type Python code, and the interpreter executes it immediately and shows the result.
The prompt >>> indicates that the interpreter is ready. For example, if you type:
1 + 1
the interpreter will respond with:
2
In this mode, you save the source code or a sequence of instructions in a file and then use the interpreter to execute it. These files are called python scripts.
Working in interactive mode is useful for testing small pieces of code because you can execute them immediately. However, for anything longer than a few lines, you should store the code in a file so that you can modify it and run it again later.
You can save files and, after some time, you can run and read them yourself or have others read it.
It is strongly recommended to clarify your scripts as much as possible. They should include plenty of comments. The real challenge in developing programs lies in correct algorithms, ensuring they can be verified, debugged, and modified under the best conditions. Therefore, it is essential that the programmer describes their code very clearly and as explicitly as possible.
A good programmer always includes a large number of comments in their scripts, not only to make the algorithms easier to understand for potential future readers but also to make their own script more transparent. The best place for these explanations is within the script itself so they don’t get lost.
We can insert comments almost anywhere in a program. Simply place the # symbol before the comment. The interpreter recognizes this symbol as an instruction to ignore everything that follows it until the end of the line.
It is crucial to understand that you should add comments as you progress in your programming. Do not wait until the end of the script. Keep in mind that programmers spend a significant amount of time reading their own code (checking variable states, debugging errors, etc.), and this task becomes easier with clear comments and explanations.
While not strictly necessary, it is recommended to save text files containing Python scripts with the .py extension. This makes them easier to identify and allows graphical file managers (such as Windows Explorer, Nautilus, or Konqueror) to recognize them and display an appropriate icon.
It is also advised to avoid naming your script after existing Python modules, such as math.py or tkinter.py.
To run the program, you must tell Python the file name. Suppose the name of script is myScript.py. If you are working in a text-based environment on Linux or in a Windows command prompt, you can execute this script using the command:
python3 myScript.py
If you are using a graphical environment in Linux, open a terminal and enter the same command.
In graphical file managers, you can execute the script by clicking on run icon—provided that Python 3 is set as the default interpreter for .py files. However, issues may arise if multiple versions of Python are installed.
If you are using IDLE, you can open your script in IDLE. Then you can run your script directly using Ctrl + F5. In other specialized environments such as Geany, you will find icons or keyboard shortcuts (usually F5) to execute your program.
If you wrote your script using an old text editor (such as the ones used for reports in older systems), the script might not work correctly with Python 3. If it was encoded incorrectly, you may see an error message like this:
SyntaxError: Non-UTF-8 code starting with '\xe0' in file helloworld.py, but no
encoding declared;
This message indicates that the script contains incorrectly encoded characters according to older standards (e.g., ISO-8859-1 or Latin-1). If you encounter this issue, you need to:
# -*- coding: Latin-1 -*-
This tells Python to interpret the script using Latin-1 encoding, which supports Western European languages (such as French, Spanish, and Portuguese) and is often referred to as ISO-8859-1.
Python can handle different character encodings appropriately. To help it do so, you should specify the encoding in your script. Without such an indication, Python assumes the script is encoded in UTF-8.
UTF-8 is the most commonly used encoding in modern computing systems. It is a Unicode-based standard developed for the digital representation of characters from various world languages, along with symbols used in mathematics and science.
In the next article, we are going to learn about How to print “Hello word” and perform basic arithmetic operations using variables.
No Comments