Page 18 - MSDN Magazine, September 2019
P. 18
The Working Programmer TED NEWARD An Introduction to Python
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 interest- ing language to study.
With the combination of its relatively simple syntax and pro- gramming 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 nec- essary 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 exis- tence. 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.
In fact, the Python appellation derives from the other famous Python: Monty.
Additionally, I would like to clear up a popular misconception: Despite the Python community’s insistence on using snake or rep- tile 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 Fly- ing 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
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 envi- ronment, 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
14 msdn magazine