Page 22 - MSDN Magazine, October 2019
P. 22

The Working Programmer TED NEWARD Python: Flow Control
In the last article (msdn.com/magazine/mt833510), I took a start down the path toward Python, getting it installed and paying homage to the Gods of Computer Science. Obviously, there’s not much you can do at that point, so it’s time to dive deeper into Python by examining its flow control structures, like if/then statements, loops and exceptions.
One thing before I begin, though: I never really touched on the subject of editors. While most readers of this magazine will be accustomed to using Visual Studio for all editing purposes, build- ing solutions and projects and such, Python doesn’t really fit that model. And certainly, while you could use Visual Studio simply as a text editor, it’s generally pretty much a heavyweight for that sort of use. (As my videogaming sons like to put it, “There’s no kill like OVERkill.”) As a result, Visual Studio Code is likely to be the edi- tor of choice for most folks. However, the Anaconda distribution that I discussed last time (which ships as part of the Visual Studio installer) comes with another option, a more IDE-ish experience called Spyder. It’s certainly not as extensive as Visual Studio is, but it makes for a nice “halfway point” between the Visual Studio Code editor-first experience and the Visual Studio IDE-everything experience. (For those who are fans of the JetBrains suite of tools, it also makes PyCharm, which has a certain amount of popularity among the Python crowd.) As always, the choice is up to you— most Python projects have no dependencies on any particular editor or environment.
Armed thusly with your editor of choice, let’s proceed.
Branching
(Note that I may reduce the indentation in some code listings here to two characters for spacing in the magazine; but stylisti- cally, Python is super-sensitive to whitespace and prefers four, so you should stick with that.) Take particular care to note the lack of parentheses around the expression, and the colon at the end of each test expression—the colon will be an indicator to you that Python expects the next line or sequence of lines to be indented. As a matter of fact, if you remove the “print” line from either of those blocks, and leave nothing in their place, Python will complain quite vigorously. If you don’t want code in an expected indented block, Python provides the “pass” statement, or a standalone “...,” to indicate a no-op statement:
# If-then
name = “Zoot”
if name == “Zoot”:
pass
elif name == “Sir Robin”:
... else:
print(“I have no idea who you are”)
When this is executed, nothing will be printed, because it matches on the first expression but then passes on actually doing anything.
Toloop,youhavetwo constructs, while and for.
It’s also important to note that the Python mantra—“There’s only one way to do it”—holds here, as well, with respect to this sort of sequential testing of a value. Where other languages, like C#, Java, Ruby or F#, have some form of multiple-choice decision-making construct (usually denoted by the keyword “switch”), Python chooses instead to stick with the conceptually simpler “if-then-elif ” collec- tion. In other words, Python has no “switch” because a whole list of “elif”s can serve the same purpose. (Whether this is an advan- tage or drawback is open to debate and interpretation, of course.)
Looping
To loop, you have two constructs, while and for. While will continue to loop and execute a block until a test condition is determined to be false, and for iterates across a sequence, which is a generic way of referring to a variety of “iterable things.” The syntax for a while loop is pretty straightforward, with one interesting quirk that also applies to for. Have a look:
The most basic branching construct is the classic if/then statement, which does exactly the same thing as in pretty much every other programming language ever invented: Test an expression and if the test yields a true result, execute a block of code; if the result is false, either bail entirely or execute an alternative “else” branch. Python adds only one wrinkle to this, which brings me to an important distinction of Python, that of significant whitespace. In Python, you don’t denote scope blocks using punctuation characters (like the “\{“ and “\}” in C-family languages such as C#), you simply indent a number of characters, typically four, like so:
# If-then
name = “Zoot”
if name == “Zoot”:
print(“Naughty, naughty Zoot!”) elif name == “Sir Robin”:
print(“Brave, brave Sir Robin...”) else:
print(“I have no idea who you are”)
18 msdn magazine









































































   20   21   22   23   24