Python belongs to the high-level programming languages class which was first time developed by Guido van Rossum in 1991. The Python is similar to C++, C# or Java programming languages. It is very useable with a clean syntax and easy to learn even for programming beginners.
Raspberry Raspbian OS is shipped with pre installed two versions of Python language: Python2 and Python3 which are available from the Raspbian Menu.
Choosing the Python version from the menu, the command window with cursor opens.
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 20 2018, 15:12:02) [MSC v.1900 64 bit (AMD64)] on Win32 Type "copyright", "credits" or "license()" for more information. >>>
To test the simply program “Hello World!” User just can entry:
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 20 2018, 15:12:02) [MSC v.1900 64 bit (AMD64)] on Win32 Type "copyright", "credits" or "license()" for more information. >>>printf("Hello World") Hello World! >>>
Writing the same code in C language will look following:
#include <stdio.h> int main() { printf ("Hello World!"); return 0; }
Python can automate tasks using the batch commands renaming and move large amounts of files like shell scripts. It can be used as a command line with IDLE, Python’s REPL (read, eval, print, loop) functions. However, there are more useful tasks which can be done with Python. For example, Python can be used to program things like:
Because Python stays very popular, it has a large collection of libraries, which speeds up the development process. There exist libraries for – game programming, rendering graphics, GUI interfaces, web frameworks, and scientific computing.
Many of the programmings stuff in C language can also be programmed in Python. Python is generally slower at computations regarding the C compiler, but its ease for use, which makes Python a very popular tool for prototyping programs and designing applications which are not computationally intensive. One of the best Python tutorials can be found in the book: “Learning Python, 5th Edition by Mark Lutz”.
Python 2 and Python 3 come pre-installed on Raspbian OS systems, but if necessary to install Python on another Linux OS or to update it, the simple commands can be executed at the command prompt:
sudo apt-get install python3
Installs or updates Python 3.
sudo apt-get install python
Installs or updates Python 2.
To access the Python REPL (where the user can type Python commands just like the command line) the user can enter python or python3 commands depending on which version of Raspbian to use in the command prompt.
Pressing (CTRL-D) exits the REPL.
To run the program without making it executable, the user must navigate to the location where the file exists and enter the command:
python hello-world.py
Making a Python program executable allows to run the program without entering python before the file name. User can make a file executable executing the following commands in the command prompt:
chmod +x file-name.py
Now to run the program:
./file-name.py