Volume 34 Number 9
[The Working Programmer]
An Introduction to Python
By Ted Neward | September 2019
In the last few years, Python has “made the mainstream” in terms of popular use, rated by some surveys and polls as the new most popular language. While it’s doubtful that Python will ever take over the world, as they say, it does seem to have a relatively strong grip on the data science and artificial intelligence/machine learning world, and for that reason alone, Python is an interesting language to study.
With the combination of its relatively simple syntax and programming model, and the extensible modules connecting it to the underlying OS, Python makes for a timely and useful switch to a new topic for this column. Over the next few issues, I’ll explore Python’s syntax, semantics, a number of the more interesting libraries in the Python ecosystem, and then … Who knows?
First Things First
Before I dive into this exploration, however, I must present a necessary disclaimer: As is the case for almost all of the columns I write, I’m not going to suggest for even a half-moment that Python should replace your C# (or Visual Basic) code currently in existence. Python is a useful tool to have on your belt, just as so many other things are, and if you find yourself in a situation where it seems appropriate, use it. But there’s nothing I can think of that you can do with Python that can’t also be done with C#—you just use them differently, that’s all.
Additionally, I would like to clear up a popular misconception: Despite the Python community’s insistence on using snake or reptile puns for their various packages and libraries, the language was not, in fact, named after the large constrictor populating various jungles all over the world. In fact, the Python appellation derives from the other famous Python: Monty. As in, Monty Python’s Flying Circus, the cult classic comedy troupe from Britain from a few decades ago. It is the height of Python culture to use quotes and examples from any of the movies or episodes in any sample code, and good Pythonistas will never pass up the opportunity to toss a quip or two in passing. The Web site even insists on such.
You’ve been warned.
Getting started with Python is much easier than it might seem at first, for two basic reasons. One, a version of Python is available for install right out of the box with the Visual Studio installer. This is a packaged version of Python and environment management tools called Anaconda, and I’ll talk about what that means and implies in a second. The second reason is that now, thanks to the Windows Subsystem for Linux (WSL), Python is just a simple bash-shell command-line command away, using the package manager of your choice. For example, if you’ve installed Ubuntu into the WSL environment, Python is easily obtained by opening an Ubuntu bash-shell prompt and typing “sudo apt-get install python3.” Additionally, as of May, if you’re running Windows 10, Python is available in the Windows Store; see bit.ly/2JMxC3A for details.
Numerous other options are also available, of course, if neither of these strikes your fancy. The Python Web site has a Windows-based installer available, for those who prefer their Python installation to come directly from the source. Or, for those who really want to get the source and nothing but the source, Python is (as most languages now are nowadays) an open source project hosted on GitHub and can be built from scratch. While either of these might have been the preferred approach a decade ago, the relatively recent success of bundling package managers as a core part of the language (starting with Ruby and gems, then migrating into Node and npm, not to mention Java and Maven, .NET and NuGet, and more) means that a modern Python installation needs to be accompanied by a package management tool, which in the core Python world is called “pip.” As interest in Python has continued to grow, other deployment systems have emerged, as well, including the aforementioned Anaconda and its “conda” utility. There’s no real need to stress when deciding between these, as they both get you to the same place. For the most part, if you use the Anaconda installation that comes with Visual Studio, you’ll use conda, and if you choose the installer from the Python Web site, you’ll use pip.
The other factor to be aware of is that Python comes in two sets of two major flavors. The first is that Python is available in both 64-bit and 32-bit versions; the differences in this case are pretty obvious. The second issue is that Python is officially making a rather tricky transition, that of moving from Python 2 to Python 3. For reasons that are largely unimportant to us, Guido von Rossum, the creator of Python, decided he had some breaking changes he needed to make to the language, and officially bumped the version number up when he implemented them. It was commonly expected that everybody would transition fairly quickly, but … that hasn’t happened. Or rather, it’s still happening. I’ll be using Python 3 for the duration of this series, as any new Python projects should start with Python3, but you may run across the odd package or library that still requires Python 2.
Once Python is installed, it’s time to try it out. If you’ve chosen to install Anaconda, fire up the installed Start Menu item that reads “Anaconda Prompt.” (Visual Studio 2019 actually installs this as “Python Prompt.”) This brings up a command prompt that has the environment set for Anaconda Python, and it will look a little odd, as it will have something like “(base)” in front of the normal directory-based prompt (“C:\Users\Ted>” on my machine). This is because Anaconda is managing different “environments” for you, something I’ll talk more about in a later column. For now, just type “python” to bring up the Python Read-Evaluate-Print Loop (REPL), the interactive environment, which will give you a “>>>” prompt after printing a version banner.
At this point, it’s time to honor the Gods of Computer Science, by offering up the customary greeting:
print("Hello, Python world")
If you type this into the REPL, it will immediately print the greeting and provide another “>>>” prompt; if you wish, you can put this into a file (hello.py) and run it from the Anaconda prompt (“python hello.py”). Either way, you will have done your duty, and you can now list “Python programmer” on your resume.
If you want the message to be captured into a variable (perhaps for later reuse), you’d write it like so:
message = "Hello again, " message += "Python world" print(message)
# By the way, this is a comment message = "Hello again, " message += "Python world" print(message) message = 5 print(message)
However, this also brings up a critical point to take note of: Whenever a variable is introduced for the first time, it must have a value assigned to it, or Python will think you’re trying to reference a previously defined variable. This is a small price to pay, though, considering that you can always assign zero or empty strings or whatever seems reasonable as a default value.
Last quick-hit note: As the first line of the example implies, the hash symbol (more properly known as the “octothorpe” to those who care about such things) is the end-of-line comment character in Python. Unlike many other languages, however, Python doesn’t have a multi-line comment symbol, which means that commenting out large sections of code is a little less simple. However, this is entirely in keeping with the driving mantra of Python: “There’s only one way to do it”; in this case, there’s only one way to comment.
You can run this code in a variety of ways.
First, from a Windows command line, assuming Python is on the PATH, scripts can be executed by simply passing the name of the Python file to the interpreter, a la “python hello.py.” This is likely to be the means for running Python applications for at least half of the Python applications you write, particularly if you use the tool to build graphs or run Web servers.
An interesting variation on this theme, though, just for Windows, is that if you use the Windows-based installers to put Python on your machine (as opposed to building from source), Python will register itself in the Windows Registry, and claim the “.py” file extension. Additionally, if “.PY” is appended to the “PATHEXT” environment variable in a Windows command prompt, you’ll be allowed to run Python scripts as if they were directly executable (because they will be—this environment variable contains the extensions of all files that are intended to be directly executable, such as COM, EXE, BAT, CMD and others). This particular behavior is entirely specific to Windows, mind you, and not restricted to Python whatsoever—any file extension can be registered in the PATHEXT environment variable and be “directly executable,” so long as the application-to-file-extension registration is set up in the Registry.
On a Unix system, however, you can always set up a “she-bang” line at the top of the script; for those unfamiliar with this aspect of Unix behavior, when a script is “directly executed” (such as by typing “hello.py” at the command-line), Unix will examine the first line of the script, and if it reads something like “#!/usr/bin/env python3,” the OS will assume that it’s a command line to execute in order to run this particular script.
This is obviously just the first article of several on Python, so of course there are a few more topics to cover before you really get to understand Python and know how to use it. In the next piece, I’ll cover Python’s flow-control constructs, which will also bring me face-to-face with the next-most-interesting facet of Python: significant whitespace. Happy coding!
Ted Neward is a Seattle-based polytechnology consultant, speaker, and mentor. He has written a ton of articles, authored and co-authored a dozen books, and speaks all over the world. Reach him at firstname.lastname@example.org or read his blog at blogs.tedneward.com.
Thanks to the following technical expert for reviewing this article: Harry Pierson