Languages become popular for all sorts of reasons—sometimes simply because there aren’t other options for a particular niche. In my view, Python’s success is well deserved: it’s easy to learn, pleasant to use, and you can start building useful programs fairly quickly.
What’s the secret to its success? It has an easy-to-learn, logical, and concise syntax. Many languages are full of gotchas like the legendary print
, which, contrary to expectations, prints the string foo (the correct version is print
).
In Python, expressions are almost always straightforward, and errors surface as exceptions that make it easy to understand what went wrong. For example, if you try to write to a list at an index that doesn’t exist, you’ll immediately get an error with a clear, descriptive message.
>>> arr = [1,2]
>>> arr[5] = 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
For example, in Ruby or Lua, similar code would turn the array into [
. And if that’s not what you expected, spotting and fixing the bug may not be easy.
When choosing a language for a new project, you should consider not just the language itself but also its ecosystem: libraries, development tools, and the user community. Python excels here. Many popular IDEs support it, and some are built specifically for it, such as PyCharm. There are plenty of profiling and debugging tools—for example, py-spy, a profiler that lets you attach to a running program and see in real time which functions are executing and how long they take.
The number of libraries for all kinds of purposes is enormous as well. Some have become so popular—like TensorFlow, pandas, and NumPy—that people choose Python for machine learning and data analysis specifically to take advantage of them. In DevOps, Ansible and SaltStack are just as popular.
Combined with its solid performance, this makes Python suitable for everything from short scripts and one-off tasks to large applications. Like any other language, Python isn’t a silver bullet and isn’t always the best choice for a specific job, but it covers a broad enough set of problems and performance needs to serve as the primary—or even sole—language for many projects. When performance is insufficient, you can compile a subset of Python to native code using Cython, or rewrite hotspots in another language and interface with it via FFI.
The key point: with the right libraries, solving many tasks is simple. Even for a novice developer, Python will quickly become a go-to tool for everyday tasks.
To showcase Python’s power, I picked a set of libraries that don’t require complex dependency installs or datasets and let you build something useful in just a few lines of code.
Virtual Environments
For development and testing, Python users often rely on virtual environments. In Python 2.x, support was provided by an optional third-party module, but in Python 3 it’s built in, so you don’t need to install anything to use them.
Each environment lives in its own directory, so you can install modules into it as a regular user. If you irreparably break your environment, you can simply delete the directory and recreate it. It’s also useful to spin up a clean environment to make sure your library or application installs cleanly from source and works.
Creating and activating a virtual environment is straightforward. On UNIX-like systems, do it like this:
$ python3 -m venv test-env
$ source ./test-env/bin/activate
To exit the environment, run deactivate
.
To avoid cluttering your main system with unnecessary modules, it’s best to test all the examples in a virtual environment.
An Alternative to Shell Scripts
Bourne shell scripts are a staple of system administration and automation, but as a programming language the shell is still stuck in the ’70s. Its built-in debugging and error handling are rudimentary, and if you want a cross-platform script you have to be careful to avoid “bashisms” and anything outside the POSIX standard. Tools like ShellCheck can help, but there’s another option—don’t use the shell at all.
The Python standard library already includes a number of modules that will do half the work for you. For example, you can copy a file in a single line using a function from the shutil module.
$ touch myfile
$ python3
>>>import shutil
>>>shutil.copy('myfile', 'myfile.bak')
>>>exit()
It also includes chown
, rmtree
, and functions for packaging files into archives; moreover, these functions support a follow_symlinks
option.
Executing a command and obtaining its exit code and output is no harder.
>>> import subprocess
>>> subprocess.run(["ls -l /dev/null"], shell=True, capture_output=True)
CompletedProcess(args=['ls -l /dev/null'], returncode=0, stdout=b'crw-rw-rw-. 1 root root 1, 3 Feb 9 01:36 /dev/null\n', stderr=b'')
Sometimes you can’t avoid passing user input to an external command. That opens the door to shell injection vulnerabilities and demands careful handling. Fortunately, the standard library already has a function that will add quotes and escape dangerous characters.
>>>import shlex
>>> shlex.quote("myfile; rm -rf ~")
"'myfile; rm -rf ~'"
Web Scraping
Web scraping is used for many purposes—from working around services that don’t offer an API to building search engines.
For demonstration, we’ll extract news headlines from the magazine’s homepage. Using the requests library and the BeautifulSoup HTML parser, we can do this in just a few lines.
Install the libraries: pip3
. Now open hackmag.com in your browser’s DevTools and you’ll see that the news headlines live in <
elements, but not directly—they’re nested inside <
and <
. Luckily, BeautifulSoup supports CSS3 selectors, and in that syntax tag1
means “a <
nested inside <
.” So our selector for the headlines will be h3.
.
import requests
from bs4 import BeautifulSoup
response = requests.get("https://hackmag.com")
page = response.text
soup = BeautifulSoup(page, 'html.parser')
headings = map(lambda e: e.text, soup.select("h3.entry-title a span"))
for h in headings:
print(h)
Save it to a file like hm-headings.
and run python3
, or just paste it into the interpreter, and you’ll see all the latest news.
Web Applications
Python offers full-fledged MVC frameworks like the popular Django. But there are also lightweight libraries that let you turn any function into a web service with just a couple of lines of code.
One of the most popular microframeworks is Flask. For demonstration, we’ll write a service that responds to the request /
with the sum of the numbers x and y.
Install Flask with the command pip3
and save the following code to a file named myapp.
:
from flask import Flask, escape, request
app = Flask(__name__)
@app.route('/add/<int:x>/<int:y>')
def show_post(x, y):
return str(x + y)
Now you can run it with the command env
.
$ curl http://localhost:5000/add/3/2
5
If Flask wasn’t your thing, consider Bottle instead—it’s just as easy to use.
Natural Language Processing
Natural language processing has traditionally been viewed as a complex, specialized field. The libraries for it are quite mature, but they’re not especially easy to use.
However, the TextBlob module provides a simple interface to the NLTK library. Before using it, we need to install the library itself and download its datasets:
$ pip3 install textblob
$ python3
>>> import nltk
>>> nltk.download('punkt')
>>> nltk.download('brown')
Now let’s write a function that returns a noun in its plural form.
from textblob import TextBlob
def pluralize(word, count):
if count == 1:
return word
else:
blob = TextBlob(word)
return blob.words[0].pluralize()
print(pluralize('mouse', 1))
print(pluralize('mouse', 9))
Copy all of this into the interpreter and try it out.
It’s just as easy to split text into sentences:
>>> text = TextBlob("Python is a cool language. I like using it.")
>>> text.sentences
[Sentence("Python is a cool language."), Sentence("I like using it.")]
TextBlob offers plenty of other features—for example, part‑of‑speech tagging—which is very handy if you want to build a text adventure or a chatbot.
Conclusion
These are just a few examples; the PyPI package repository is huge, and there’s something there for everyone. When you’re just learning the language, there’s nothing wrong with writing your own solution to a problem that’s already been solved. But if you need a quick, practical fix—say, working with a file format or integrating with a service—check the repository first; chances are it’s already there.