Python is a powerful high-level scripting language. It is amongst one of the most popular languages that is widely used in many organizations, like Google, Yahoo, NASA, etc. Besides commercial applications, Python is exhaustively used for research-based projects as well.
Python allows us to write programs quickly since it is dynamically typed, has a simpler syntax, and is interpreted. This makes Python a good programming language for software that have aggressive development timelines (which is true for most of the software!) or for writing software prototypes.
Python has a special emphasis on making the code readable. As an example, it uses space indentation to identify blocks instead of a pair of parenthesis (as in C or C++). Often software is handled by multiple programmers -- it is written by one set of developers and is maintained/enhanced by another set of programmers. Python's clear syntax enables new programmers to read, maintain, and enhance the existing code.
However, since Python is interpreted at run time, it runs slower than C or C++. Hence, this factor must be considered when using the same. Fortunately, Python has the ability to integrate C code well and so one can always design a software such that performance-critical components of the software can be written in C, where as the rest of the software can be written in Python.
There are two ways to run a Python program. The first way is to simply invoke the Python executable in an interactive mode and input statements. The second way and certainly the popular style, is to keep the Python code in a file and then pass the file as an argument to the Python executable. Since Python is run using an interpreter, there is no need to compile these programs!
Now, let us take a look at the first way. On a Linux or macOS system, we can usually start the Python 3 interpreter by running "python3" and then entering Python instructions interactively. The exact Python 3 version depends on the version installed on the system. We can use quit() or Ctrl-D (i.e. EOF) to exit from the interpreter.
[user@codingbison]$ python3
Python 3.x.x
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> print("Hello World")
Hello World
>>>
>>> quit()
[user@codingbison]$
In the second (and the most common) style, we can keep the Python program in a file and pass the file as an argument to the Python executable. If there are errors, then the Python interpreter will detect them during the run-time. We can keep Python statements in a file (let us say, "hello.py") and pass the file as an argument to the Python 3 executable, "python3". Here is an example:
[user@codingbison]$ cat hello.py
#This is a simple "Hello World" program.
print("Hello World")
[user@codingbison]$
[user@codingbison]$ python3 hello.py
Hello World
[user@codingbison]$
The above style is common, since except for some quick reference, it is cumbersome to type a Python program in the interpreter every time. For complex programs, the only (acceptable) way is to keep the code in a file and then run it as and when needed.
The first line in the above example starts with a "#" sign; such lines are Python comments. It is a good idea to provide useful comments that explain the software logic.
This module uses Python 3. Python 2 is obsolete and no longer receives official security updates or bug fixes. For new programs, use Python 3 and install libraries for the same Python 3 interpreter that runs your programs.
On many systems, the command to start Python 3 is "python3". You can check which version is installed by running:
[user@codingbison]$ python3 --version Python 3.x.x [user@codingbison]$
Python 3 uses print() as a function. Put the text to display inside parentheses:
[user@codingbison]$ python3
>>> print("Hello World")
Hello World
>>> quit()
[user@codingbison]$
The following Python 2 syntax is not valid in Python 3:
>>> print "Hello World"
File "<stdin>", line 1
print "Hello World"
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
Use the Python 3 form instead:
>>> print("Hello World")
Hello World
When installing a third-party package, use "python3 -m pip" so that pip installs the package for the Python 3 interpreter you use to run your program. For example:
[user@codingbison]$ python3 -m pip install validators [user@codingbison]$