Chapter 14. Using Python as a Bash Scripting Alternative

Python is another scripting language and the newest that we have looked at so far. Similar to bash and Perl, Python is an interpreted language and makes use of the shebang. Although, it does not have a shell interface, we can access a console called the REPL where we can type Python codes to interact with the system. In this chapter we will cover the following topics:

  • What is Python?
  • Saying Hello the Python way
  • Pythonic arguments
  • Significant whitespace
  • Read user input
  • Using Python to write to files

What is Python?

Python is an object-oriented interpreted language that is designed to be easy to use and to aid Rapid Application Development. This is achieved by the use of simplified semantics in the language.

Python was born at the end of the 1980's, towards the very end of December 1989 by the Dutch developer, Guido van Rossum. A majority of the design of the language is aimed at clarity and simplicity and one of the main rules of the Zen of Python is:

"There should be one, and preferable only one, obvious way to do it."

Often systems will have both Python 2 and Python 3 installed; however, all newer distributions are switching to Python 3. We will be working with Python 3, it being the latest version installed on the Raspberry Pi.

Although, there is no shell, we can interact with Python using REPL: read, evaluate, print, and loop. We can access this by typing python3 in the command line. You should see something similar to the following screenshot:

What is Python?

We can see that we are presented with the >>> prompt and this is known as the REPL console. We should emphasize that this is a scripting language and like bash and Perl, we will normally execute a code through the text files that we create. Those text files will normally be expected to have a .py suffix to their name.

While working with REPL, we can print the version independently by importing a module. In Perl, we will use the keyword, in bash we will use the command source and in Python we use import:

>>>import sys

With the module loaded, we can now investigate the object-oriented nature of Python by printing the version:

>>> sys.version

We will navigate to the sys object within our namespace and call the version method from that object.

Combining these two commands together, we should be able to see the following output:

What is Python?

To close this section about describing Python, we should take a look at the Zen of Python. From REPL, we can type import this, as shown in the following screenshot:

What is Python?

This is far more than just the Zen of Python; it actually makes up a good rule for all programming languages and a guide for developers.

Finally, to close the REPL, we will use Ctrl + d in Linux or Ctrl + z in Windows.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.183.138