It has been said (and often required by recruitment agencies) that system administrators need to be proficient in a scripting language. While most of us may be comfortable using Bash (or other shell of our choice) to run command-line scripts, a powerful language such as Python can add several benefits.
To begin with, Python allows us to access the tools of the command-line environment and to make use of Object Oriented Programming features (more on this later in this article).
On top of it, learning Python can boost your career in the fields of desktop applications and data science.
Being so easy to learn, so vastly used, and having a plethora of ready-to-use modules (external files that contain Python statements), no wonder Python is the preferred language to teach programming to first-year computer science students in the United States.
In this 2-article series we will review the fundamentals of Python in hopes that you will find it useful as a springboard to get you started with programming and as a quick-reference guide afterwards. That said, let’s get started.
Python in Linux
Python versions 2.x and 3.x are usually available in most modern Linux distributions out of the box. You can enter a Python shell by typing
python3 in your terminal emulator and exit with
$ which python $ which python3 $ python -v $ python3 -v $ python >>> quit() $ python3 >>> quit()
If you want to discard Python 2.x and use 3.x instead when you type python, you can modify the corresponding symbolic links as follows:
$ sudo rm /usr/bin/python $ cd /usr/bin $ ln -s python3.2 python # Choose the Python 3.x binary here
By the way, it is important to note that although versions 2.x are still used, they are not actively maintained. For that reason, you may want to consider switching to 3.x as indicated above. Since there are some syntax differences between 2.x and 3.x, we will focus on the latter in this series.
Another way you can use Python in Linux is through the IDLE (the Python Integrated Development Environment), a graphical user interface for writing Python code. Before installing it, it is a good idea to perform a search to find out what are the versions available for your distribution:
# aptitude search idle [Debian and derivatives] # yum search idle [CentOS and Fedora] # dnf search idle [Fedora 23+ version]
Then, you can install it as follows:
$ sudo aptitude install idle-python3.2 # I'm using Linux Mint 13
Once installed, you will see the following screen after launching the IDLE. While it resembles the Python shell, you can do more with the IDLE than with the shell.
For example, you can:
1. open external files easily (File ? Open).
(Ctrl + C) and paste
(Ctrl + V) text, 3) find and replace text, 4) show possible completions (a feature known as Intellisense or Autocompletion in other IDEs), 5) change the font type and size, and much more.
On top of this, you can use the IDLE to create desktop applications.
Since we will not be developing a desktop application in this 2-article series, feel free to choose between the IDLE and the Python shell to follow the examples.