Conditional Branching

Python provides an if statement with the same semantics as languages like C++ and Java, although with its own sparse syntax:

if expressionl:

suitel elif expression2:

suite2 else: suite3

The first thing that stands out to programmers used to C++ or Java is that there are no parentheses and no braces. The other thing to notice is the colon: This is part of the syntax and is easy to forget when starting out. Colons are used with else, elif, and in many other places to indicate that a block of code (a suite in Python-speak) is to follow. As we would expect, there can be any number of elifs (including none), and optionally, there can be a single else at the end.

Unlike most other programming languages, Python uses indentation to signify its block structure. Some programmers don't like this, at least at first, and some get quite emotional about the issue. But it takes just a few days to get used to, and after a few months, brace-free code seems much nicer and less cluttered to read than code that uses braces.

Since suites are indicated using indentation, the question that naturally arises is, "What kind of indentation?". The Python style guidelines recommend four spaces per level of indentation, and only spaces (no tabs). Most modern text editors can be set up to handle this automatically (IDLE's editor does, of course). Python will work fine with any number of spaces or with tabs, providing that the indentation used is consistent. In this book, we will follow the official Python guidelines.

Let's begin with a very simple example:

Table 2.1 Logical Operations

Group

Comparison Identity Membership Logical

Operators

Description

The <> operator is also permitted as a synonym for != but is deprecated

These are used to determine if two object references refer to the same underlying object

These are used on lists, dictionaries, and strings, as we saw in Chapter 1

Both and and or short-circuit; the bit-wise equivalents are: ~ (not), & (and), | (or), and ~ (xor)

In this case, the suite is just one statement (print x). In general, a suite is a single statement, or an indented block of statements (which themselves may contain nested suites), or the keyword pass which does absolutely nothing. The reason we need pass is because Python's syntax requires a suite, so if we want to put in a stub, or indicate that we are handling a "do nothing" case, we must use something, so Python provides pass; for example:

pass # do nothing in this case

In general, whenever Python's syntax has a colon followed by a suite, the suite can be on the same line if it is just a single statement. For example:

If the suite is more than a single statement, it must begin on the following line at the next level of indentation.

Python supports the standard comparison operators, and for logical operations it uses names (not, and, and or) rather than symbols. It is also possible to combine comparison expressions in a way that is familiar to mathematicians:

Here, we print x if it is between 1 and 10. If x is an expression with no side effects, the above statement is equivalent to:

Was this article helpful?

0 0
YouTube Tactics

YouTube Tactics

Report Reveals 50 Easy-To-Implement YouTube Tactics For Better Videos, More Video Views and Traffic To Your Website.

Get My Free Ebook


Post a comment