Sashkin - Fotolia
In the age of DevOps, organizations expect system administrators to have some knowledge of programming. If you don't, you should learn. An administrator who can write code is more valuable and productive than one who can't.
Why use Python?
Put simply: Python is easy to use.
Most vendors support some kind of Python interface for their product. And if they don't, they support representational state transfer, which Python can do, as well. Whatever you can do with cURL, you can do with Python.
Python is an interpreted language. That means, although you can compile it, there's no need to do so. It interprets code one line at a time. When you run the shell, it works as a read-evaluate-print-loop interactive interpreter. It responds immediately when you type commands, which is the easiest way to write code.
The other reason to use Python is there's an enormous repository of free APIs you can use to interact with everything, from OpenStack to Amazon Web Services to Twitter.
Skills needed to use Python
To use Python, you need deductive reasoning and to understand math, as all programming languages are built on those. You also need to understand abstract concepts like logic, data structures, arrays and lists. At a minimum, you need to understand what integers, decimals and character strings are.
What resources are there?
If you have the time and are comfortable teaching yourself how to program through reading manuals and doing tutorials, you probably don't need to take a class to learn Python.
In many cases, you don't need to write original code to do many tasks, as someone has already done it. Just search the web, copy the code and change it to fit what you are doing -- unless it has a copyright.
You can also find a list of learning resources on the Python website.
One challenging aspect of Python is Linux distributions ship with Python version 2, which is reaching end of life. However, some Linux utilities require Python 2, and changing to version 3 could affect your system. Using Python 3 will ensure your code works in the future, but be sure you understand the implications of upgrading.
Below, I have installed both and have created an alias -- python3 -- to point to /usr/bin/python3.
This dual-version system can cause some troublesome bugs if you get the two environments mixed up. Depending on which version of Python and pip -- used to install Python APIs -- you're using, they can be put in two different places:
ls -d /usr/bin/python*
/usr/bin/python /usr/bin/python2-config /usr/bin/python3m
/usr/bin/python2 /usr/bin/python3 /usr/bin/python-config
There are several ways around this problem, like setting the environment variable to PYTHONPATH. And you can type pip3 and python3 when you want to use version 3.
With Ubuntu 16+, you probably won't have any issues, but you might with CentOS 6.8 and earlier.
Some simple Python examples
Python isn't a typed language. That means when you use Python, you can give a variable a value without declaring its type. That makes programming simple.
In the example below, "a" is an integer. In the first line, I define the value of "a." In the second line, I request the value of "a," and the systems responds with the value I set in the earlier line.
>>> a = 2
See how to declare a function below. The key thing to note here is four blank spaces indicate the indentation level. So, don't use brackets or a semicolon.
>>> def twice(a):
... return a * 2
Notice above that we made "a" equal two. Then, in the function twice, we define that the function should double the value sent of the integer. There, we also use the letter "a" as the symbol for the parameter passed into the function. Here is where you need to understand one programming concept called scope. The "a" inside the function isn't the same as "a" outside the function. If you aren't aware of scope, you could spend all day trying to find a bug in your code.
Earlier, we spoke of the power of frameworks. For example, to interact with the OS, you can just write:
Then, you can take other actions, such as list the directory contents:
Or, you could use a variable to define a new command:
>>> dir = "/tmp"
Python also supports objects, but you won't need to use them for most tasks. Python has a lot of features that a beginner would find complicated, like inline functions called lambda and dictionary objects, which are key-value pairs.
When you use Python, start simple and build your knowledge over time. You can find editors for Python code that will help you catch errors at code time versus runtime, which is far easier than using vi and Bash. And the interactive Python shell is really the best way to write code, as you can get one small paragraph working at a time.
Run SQL Server Python applications
Avoid FTP injection attacks on Java and Python