Page 19 - MSDN Magazine, September 2019
P. 19

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)
Notice that the local variable, message, doesn’t need to be declared before first use, and there’s no “declaring keyword” like JavaScript’s “var” (or “let” or “const”). Python supports strings, obviously, as well as Booleans, numeric values, lists, tuples, sets (lists that dis- allow duplication) and dictionaries (lists of tuples, or key-value pairs if you prefer) as built-in types. (You’ll see in future columns how to build out custom object types, and explore what each of these primitive types means in more detail as we go.) Variables are themselves entirely untyped, which means your local variable can be assigned any kind of value at any time, like so:
# 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. msdnmagazine.com
Executing Python
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.
Variables are themselves entirely untyped, which means your local variable can be assigned any kind of value at any time.
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.
Wrapping Up
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! n
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 ted@tedneward.com or read his blog at blogs.tedneward.com.
ThaNks to the following technical expert for reviewing this article: Harry Pierson
September 2019 15


































































































   17   18   19   20   21